| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# This file is part of Ansible |
| 4 |
# |
| 5 |
# Ansible is free software: you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation, either version 3 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# Ansible is distributed in the hope that it will be useful, |
| 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
# |
| 18 |
from __future__ import absolute_import, division, print_function |
| 19 |
|
| 20 |
|
| 21 |
__metaclass__ = type |
| 22 |
|
| 23 |
|
| 24 |
DOCUMENTATION = """ |
| 25 |
module: nxos_overlay_global |
| 26 |
extends_documentation_fragment: |
| 27 |
- cisco.nxos.nxos |
| 28 |
short_description: Configures anycast gateway MAC of the switch. |
| 29 |
description: |
| 30 |
- Configures anycast gateway MAC of the switch. |
| 31 |
version_added: 1.0.0 |
| 32 |
author: Gabriele Gerbino (@GGabriele) |
| 33 |
notes: |
| 34 |
- Tested against NXOSv 7.3.(0)D1(1) on VIRL |
| 35 |
- Unsupported for Cisco MDS |
| 36 |
- Default restores params default value |
| 37 |
- Supported MAC address format are "E.E.E", "EE-EE-EE-EE-EE-EE", "EE:EE:EE:EE:EE:EE" |
| 38 |
and "EEEE.EEEE.EEEE" |
| 39 |
options: |
| 40 |
anycast_gateway_mac: |
| 41 |
description: |
| 42 |
- Anycast gateway mac of the switch. |
| 43 |
required: true |
| 44 |
type: str |
| 45 |
""" |
| 46 |
|
| 47 |
EXAMPLES = """ |
| 48 |
- cisco.nxos.nxos_overlay_global: |
| 49 |
anycast_gateway_mac: b.b.b |
| 50 |
""" |
| 51 |
|
| 52 |
RETURN = """ |
| 53 |
commands: |
| 54 |
description: commands sent to the device |
| 55 |
returned: always |
| 56 |
type: list |
| 57 |
sample: ["fabric forwarding anycast-gateway-mac 000B.000B.000B"] |
| 58 |
""" |
| 59 |
|
| 60 |
import re |
| 61 |
|
| 62 |
from ansible.module_utils.basic import AnsibleModule |
| 63 |
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.config import ( |
| 64 |
CustomNetworkConfig, |
| 65 |
) |
| 66 |
|
| 67 |
from ansible_collections.cisco.nxos.plugins.module_utils.network.nxos.nxos import ( |
| 68 |
get_config, |
| 69 |
load_config, |
| 70 |
) |
| 71 |
|
| 72 |
|
| 73 |
PARAM_TO_COMMAND_KEYMAP = {"anycast_gateway_mac": "fabric forwarding anycast-gateway-mac"} |
| 74 |
|
| 75 |
|
| 76 |
def get_existing(module, args): |
| 77 |
existing = {} |
| 78 |
config = str(get_config(module)) |
| 79 |
|
| 80 |
for arg in args: |
| 81 |
command = PARAM_TO_COMMAND_KEYMAP[arg] |
| 82 |
has_command = re.findall(r"(?:{0}\s)(?P<value>.*)$".format(command), config, re.M) |
| 83 |
value = "" |
| 84 |
if has_command: |
| 85 |
value = has_command[0] |
| 86 |
existing[arg] = value |
| 87 |
|
| 88 |
return existing |
| 89 |
|
| 90 |
|
| 91 |
def apply_key_map(key_map, table): |
| 92 |
new_dict = {} |
| 93 |
for key, value in table.items(): |
| 94 |
new_key = key_map.get(key) |
| 95 |
if value: |
| 96 |
new_dict[new_key] = value |
| 97 |
return new_dict |
| 98 |
|
| 99 |
|
| 100 |
def get_commands(module, existing, proposed, candidate): |
| 101 |
commands = list() |
| 102 |
proposed_commands = apply_key_map(PARAM_TO_COMMAND_KEYMAP, proposed) |
| 103 |
existing_commands = apply_key_map(PARAM_TO_COMMAND_KEYMAP, existing) |
| 104 |
|
| 105 |
for key, proposed in proposed_commands.items(): |
| 106 |
existing_value = existing_commands.get(key) |
| 107 |
if proposed == "default" and existing_value: |
| 108 |
commands.append("no {0} {1}".format(key, existing_value)) |
| 109 |
elif "anycast-gateway-mac" in key and proposed != "default": |
| 110 |
proposed = normalize_mac(proposed, module) |
| 111 |
existing_value = normalize_mac(existing_value, module) |
| 112 |
if proposed != existing_value: |
| 113 |
command = "{0} {1}".format(key, proposed) |
| 114 |
commands.append(command) |
| 115 |
if commands: |
| 116 |
candidate.add(commands, parents=[]) |
| 117 |
|
| 118 |
|
| 119 |
def normalize_mac(proposed_mac, module): |
| 120 |
if proposed_mac is None: |
| 121 |
return "" |
| 122 |
try: |
| 123 |
if "-" in proposed_mac: |
| 124 |
splitted_mac = proposed_mac.split("-") |
| 125 |
if len(splitted_mac) != 6: |
| 126 |
raise ValueError |
| 127 |
|
| 128 |
for octect in splitted_mac: |
| 129 |
if len(octect) != 2: |
| 130 |
raise ValueError |
| 131 |
|
| 132 |
elif "." in proposed_mac: |
| 133 |
splitted_mac = [] |
| 134 |
splitted_dot_mac = proposed_mac.split(".") |
| 135 |
if len(splitted_dot_mac) != 3: |
| 136 |
raise ValueError |
| 137 |
|
| 138 |
for octect in splitted_dot_mac: |
| 139 |
if len(octect) > 4: |
| 140 |
raise ValueError |
| 141 |
else: |
| 142 |
octect_len = len(octect) |
| 143 |
padding = 4 - octect_len |
| 144 |
splitted_mac.append(octect.zfill(padding + 1)) |
| 145 |
|
| 146 |
elif ":" in proposed_mac: |
| 147 |
splitted_mac = proposed_mac.split(":") |
| 148 |
if len(splitted_mac) != 6: |
| 149 |
raise ValueError |
| 150 |
|
| 151 |
for octect in splitted_mac: |
| 152 |
if len(octect) != 2: |
| 153 |
raise ValueError |
| 154 |
else: |
| 155 |
raise ValueError |
| 156 |
except ValueError: |
| 157 |
module.fail_json(msg="Invalid MAC address format", proposed_mac=proposed_mac) |
| 158 |
|
| 159 |
joined_mac = "".join(splitted_mac) |
| 160 |
# fmt: off |
| 161 |
mac = [joined_mac[i: i + 4] for i in range(0, len(joined_mac), 4)] |
| 162 |
# fmt: on |
| 163 |
return ".".join(mac).upper() |
| 164 |
|
| 165 |
|
| 166 |
def main(): |
| 167 |
argument_spec = dict(anycast_gateway_mac=dict(required=True, type="str")) |
| 168 |
|
| 169 |
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) |
| 170 |
|
| 171 |
warnings = list() |
| 172 |
result = {"changed": False, "commands": [], "warnings": warnings} |
| 173 |
|
| 174 |
args = PARAM_TO_COMMAND_KEYMAP.keys() |
| 175 |
|
| 176 |
existing = get_existing(module, args) |
| 177 |
proposed = dict((k, v) for k, v in module.params.items() if v is not None and k in args) |
| 178 |
|
| 179 |
candidate = CustomNetworkConfig(indent=3) |
| 180 |
get_commands(module, existing, proposed, candidate) |
| 181 |
|
| 182 |
if candidate: |
| 183 |
candidate = candidate.items_text() |
| 184 |
result["commands"] = candidate |
| 185 |
|
| 186 |
if not module.check_mode: |
| 187 |
load_config(module, candidate) |
| 188 |
result["changed"] = True |
| 189 |
|
| 190 |
module.exit_json(**result) |
| 191 |
|
| 192 |
|
| 193 |
if __name__ == "__main__": |
| 194 |
main() |