| 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_vxlan_vtep |
| 26 |
extends_documentation_fragment: |
| 27 |
- cisco.nxos.nxos |
| 28 |
short_description: Manages VXLAN Network Virtualization Endpoint (NVE). |
| 29 |
description: |
| 30 |
- Manages VXLAN Network Virtualization Endpoint (NVE) overlay interface that terminates |
| 31 |
VXLAN tunnels. |
| 32 |
version_added: 1.0.0 |
| 33 |
author: Gabriele Gerbino (@GGabriele) |
| 34 |
notes: |
| 35 |
- Tested against NXOSv 7.3.(0)D1(1) on VIRL |
| 36 |
- Unsupported for Cisco MDS |
| 37 |
- The module is used to manage NVE properties, not to create NVE interfaces. Use M(cisco.nxos.nxos_interfaces) |
| 38 |
if you wish to do so. |
| 39 |
- C(state=absent) removes the interface. |
| 40 |
- Default, where supported, restores params default value. |
| 41 |
options: |
| 42 |
interface: |
| 43 |
description: |
| 44 |
- Interface name for the VXLAN Network Virtualization Endpoint. |
| 45 |
required: true |
| 46 |
type: str |
| 47 |
description: |
| 48 |
description: |
| 49 |
- Description of the NVE interface. |
| 50 |
type: str |
| 51 |
host_reachability: |
| 52 |
description: |
| 53 |
- Specify mechanism for host reachability advertisement. A Boolean value of 'true' |
| 54 |
indicates that BGP will be used for host reachability advertisement. A Boolean |
| 55 |
value of 'false' indicates that no protocol is used for host reachability advertisement. |
| 56 |
Other host reachability advertisement protocols (e.g. OpenFlow, controller, etc.) are not |
| 57 |
supported. |
| 58 |
type: bool |
| 59 |
shutdown: |
| 60 |
description: |
| 61 |
- Administratively shutdown the NVE interface. |
| 62 |
type: bool |
| 63 |
source_interface: |
| 64 |
description: |
| 65 |
- Specify the loopback interface whose IP address should be used for the NVE interface. |
| 66 |
type: str |
| 67 |
source_interface_hold_down_time: |
| 68 |
description: |
| 69 |
- Suppresses advertisement of the NVE loopback address until the overlay has converged. |
| 70 |
type: str |
| 71 |
global_mcast_group_L3: |
| 72 |
description: |
| 73 |
- Global multicast IP prefix for L3 VNIs or the keyword 'default'. This is available on |
| 74 |
Nexus 9000 series switches running NX-OS software release 9.2(x) or higher. |
| 75 |
type: str |
| 76 |
global_mcast_group_L2: |
| 77 |
description: |
| 78 |
- Global multicast IP prefix for L2 VNIs or the keyword 'default'. This is available on |
| 79 |
Nexus 9000 series switches running NX-OS software release 9.2(x) or higher. |
| 80 |
type: str |
| 81 |
global_suppress_arp: |
| 82 |
description: |
| 83 |
- Enables ARP suppression for all VNIs. This is available on NX-OS 9K series running |
| 84 |
9.2.x or higher. |
| 85 |
type: bool |
| 86 |
global_ingress_replication_bgp: |
| 87 |
description: |
| 88 |
- Configures ingress replication protocol as bgp for all VNIs. This is available on Nexus |
| 89 |
9000 series switches running NX-OS software release 9.2(x) or higher. |
| 90 |
type: bool |
| 91 |
state: |
| 92 |
description: |
| 93 |
- Determines whether the config should be present or not on the device. |
| 94 |
default: present |
| 95 |
choices: |
| 96 |
- present |
| 97 |
- absent |
| 98 |
type: str |
| 99 |
multisite_border_gateway_interface: |
| 100 |
description: |
| 101 |
- Specify the loopback interface whose IP address should be used for the NVE |
| 102 |
Multisite Border-gateway Interface. This is available on specific Nexus 9000 |
| 103 |
series switches running NX-OS 7.0(3)I7(x) or higher. Specify "default" to remove |
| 104 |
an existing gateway config. |
| 105 |
type: str |
| 106 |
version_added: 1.1.0 |
| 107 |
advertise_virtual_rmac: |
| 108 |
description: |
| 109 |
- The advertise_virtual_rmac parameter lets BGP to use the VMAC with VIP as next-hop when |
| 110 |
advertising type-2 routes. Should be used together with advertise_pip parameter from |
| 111 |
cisco.nxos.nxos_bgp_address_family module. |
| 112 |
type: bool |
| 113 |
""" |
| 114 |
EXAMPLES = """ |
| 115 |
- cisco.nxos.nxos_vxlan_vtep: |
| 116 |
interface: nve1 |
| 117 |
description: default |
| 118 |
host_reachability: true |
| 119 |
source_interface: Loopback0 |
| 120 |
source_interface_hold_down_time: 30 |
| 121 |
shutdown: default |
| 122 |
multisite_border_gateway_interface: Loopback0 |
| 123 |
""" |
| 124 |
|
| 125 |
RETURN = """ |
| 126 |
commands: |
| 127 |
description: commands sent to the device |
| 128 |
returned: always |
| 129 |
type: list |
| 130 |
sample: ["interface nve1", "source-interface loopback0", |
| 131 |
"source-interface hold-down-time 30", "description simple description", |
| 132 |
"shutdown", "host-reachability protocol bgp", |
| 133 |
"multisite border-gateway interface loopback0"] |
| 134 |
""" |
| 135 |
|
| 136 |
import re |
| 137 |
|
| 138 |
from ansible.module_utils.basic import AnsibleModule |
| 139 |
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.config import ( |
| 140 |
CustomNetworkConfig, |
| 141 |
) |
| 142 |
|
| 143 |
from ansible_collections.cisco.nxos.plugins.module_utils.network.nxos.nxos import ( |
| 144 |
get_config, |
| 145 |
load_config, |
| 146 |
run_commands, |
| 147 |
) |
| 148 |
|
| 149 |
|
| 150 |
BOOL_PARAMS = [ |
| 151 |
"shutdown", |
| 152 |
"host_reachability", |
| 153 |
"global_ingress_replication_bgp", |
| 154 |
"global_suppress_arp", |
| 155 |
"advertise_virtual_rmac", |
| 156 |
] |
| 157 |
PARAM_TO_COMMAND_KEYMAP = { |
| 158 |
"description": "description", |
| 159 |
"global_suppress_arp": "global suppress-arp", |
| 160 |
"global_ingress_replication_bgp": "global ingress-replication protocol bgp", |
| 161 |
"global_mcast_group_L3": "global mcast-group L3", |
| 162 |
"global_mcast_group_L2": "global mcast-group L2", |
| 163 |
"host_reachability": "host-reachability protocol bgp", |
| 164 |
"interface": "interface", |
| 165 |
"shutdown": "shutdown", |
| 166 |
"source_interface": "source-interface", |
| 167 |
"source_interface_hold_down_time": "source-interface hold-down-time", |
| 168 |
"multisite_border_gateway_interface": "multisite border-gateway interface", |
| 169 |
"advertise_virtual_rmac": "advertise virtual-rmac", |
| 170 |
} |
| 171 |
PARAM_TO_DEFAULT_KEYMAP = { |
| 172 |
"description": False, |
| 173 |
"shutdown": True, |
| 174 |
"source_interface_hold_down_time": "180", |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
def get_value(arg, config, module): |
| 179 |
if arg in BOOL_PARAMS: |
| 180 |
REGEX = re.compile(r"\s+{0}\s*$".format(PARAM_TO_COMMAND_KEYMAP[arg]), re.M) |
| 181 |
NO_REGEX = re.compile(r"\s+no\s{0}\s*$".format(PARAM_TO_COMMAND_KEYMAP[arg]), re.M) |
| 182 |
NO_SHUT_REGEX = re.compile(r"\s+no shutdown\s*$", re.M) |
| 183 |
value = False |
| 184 |
if arg == "shutdown": |
| 185 |
try: |
| 186 |
if NO_SHUT_REGEX.search(config): |
| 187 |
value = False |
| 188 |
elif REGEX.search(config): |
| 189 |
value = True |
| 190 |
except TypeError: |
| 191 |
value = False |
| 192 |
elif arg == "advertise_virtual_rmac": |
| 193 |
try: |
| 194 |
if NO_REGEX.search(config): |
| 195 |
value = False |
| 196 |
elif REGEX.search(config): |
| 197 |
value = True |
| 198 |
except TypeError: |
| 199 |
value = False |
| 200 |
else: |
| 201 |
try: |
| 202 |
if REGEX.search(config): |
| 203 |
value = True |
| 204 |
except TypeError: |
| 205 |
value = False |
| 206 |
else: |
| 207 |
REGEX = re.compile( |
| 208 |
r"(?:{0}\s)(?P<value>.*)$".format(PARAM_TO_COMMAND_KEYMAP[arg]), |
| 209 |
re.M, |
| 210 |
) |
| 211 |
NO_DESC_REGEX = re.compile(r"\s+{0}\s*$".format("no description"), re.M) |
| 212 |
SOURCE_INTF_REGEX = re.compile( |
| 213 |
r"(?:{0}\s)(?P<value>\S+)$".format(PARAM_TO_COMMAND_KEYMAP[arg]), |
| 214 |
re.M, |
| 215 |
) |
| 216 |
value = "" |
| 217 |
if arg == "description": |
| 218 |
if NO_DESC_REGEX.search(config): |
| 219 |
value = False |
| 220 |
elif PARAM_TO_COMMAND_KEYMAP[arg] in config: |
| 221 |
value = REGEX.search(config).group("value").strip() |
| 222 |
elif arg == "source_interface": |
| 223 |
for line in config.splitlines(): |
| 224 |
try: |
| 225 |
if PARAM_TO_COMMAND_KEYMAP[arg] in config: |
| 226 |
value = SOURCE_INTF_REGEX.search(config).group("value").strip() |
| 227 |
break |
| 228 |
except AttributeError: |
| 229 |
value = "" |
| 230 |
elif arg == "global_mcast_group_L2": |
| 231 |
for line in config.splitlines(): |
| 232 |
try: |
| 233 |
if "global mcast-group" in line and "L2" in line: |
| 234 |
value = line.split()[2].strip() |
| 235 |
break |
| 236 |
except AttributeError: |
| 237 |
value = "" |
| 238 |
elif arg == "global_mcast_group_L3": |
| 239 |
for line in config.splitlines(): |
| 240 |
try: |
| 241 |
if "global mcast-group" in line and "L3" in line: |
| 242 |
value = line.split()[2].strip() |
| 243 |
break |
| 244 |
except AttributeError: |
| 245 |
value = "" |
| 246 |
elif arg == "multisite_border_gateway_interface": |
| 247 |
for line in config.splitlines(): |
| 248 |
try: |
| 249 |
if PARAM_TO_COMMAND_KEYMAP[arg] in config: |
| 250 |
value = SOURCE_INTF_REGEX.search(config).group("value").strip() |
| 251 |
break |
| 252 |
except AttributeError: |
| 253 |
value = "" |
| 254 |
else: |
| 255 |
if PARAM_TO_COMMAND_KEYMAP[arg] in config: |
| 256 |
value = REGEX.search(config).group("value").strip() |
| 257 |
return value |
| 258 |
|
| 259 |
|
| 260 |
def get_existing(module, args): |
| 261 |
existing = {} |
| 262 |
netcfg = CustomNetworkConfig(indent=2, contents=get_config(module, flags=["all"])) |
| 263 |
|
| 264 |
interface_string = "interface {0}".format(module.params["interface"].lower()) |
| 265 |
parents = [interface_string] |
| 266 |
config = netcfg.get_section(parents) |
| 267 |
|
| 268 |
if config: |
| 269 |
for arg in args: |
| 270 |
existing[arg] = get_value(arg, config, module) |
| 271 |
|
| 272 |
existing["interface"] = module.params["interface"].lower() |
| 273 |
else: |
| 274 |
if interface_string in str(netcfg): |
| 275 |
existing["interface"] = module.params["interface"].lower() |
| 276 |
for arg in args: |
| 277 |
existing[arg] = "" |
| 278 |
return existing |
| 279 |
|
| 280 |
|
| 281 |
def apply_key_map(key_map, table): |
| 282 |
new_dict = {} |
| 283 |
for key, value in table.items(): |
| 284 |
new_key = key_map.get(key) |
| 285 |
if new_key: |
| 286 |
value = table.get(key) |
| 287 |
if value: |
| 288 |
new_dict[new_key] = value |
| 289 |
else: |
| 290 |
new_dict[new_key] = value |
| 291 |
return new_dict |
| 292 |
|
| 293 |
|
| 294 |
def fix_commands(commands, module): |
| 295 |
source_interface_command = "" |
| 296 |
no_source_interface_command = "" |
| 297 |
no_host_reachability_command = "" |
| 298 |
host_reachability_command = "" |
| 299 |
|
| 300 |
for command in commands: |
| 301 |
if "no source-interface hold-down-time" in command: |
| 302 |
pass |
| 303 |
elif "source-interface hold-down-time" in command: |
| 304 |
pass |
| 305 |
elif "no source-interface" in command: |
| 306 |
no_source_interface_command = command |
| 307 |
elif "source-interface" in command: |
| 308 |
source_interface_command = command |
| 309 |
elif "no host-reachability" in command: |
| 310 |
no_host_reachability_command = command |
| 311 |
elif "host-reachability" in command: |
| 312 |
host_reachability_command = command |
| 313 |
|
| 314 |
if host_reachability_command: |
| 315 |
commands.pop(commands.index(host_reachability_command)) |
| 316 |
commands.insert(0, host_reachability_command) |
| 317 |
|
| 318 |
if source_interface_command: |
| 319 |
commands.pop(commands.index(source_interface_command)) |
| 320 |
commands.insert(0, source_interface_command) |
| 321 |
|
| 322 |
if no_host_reachability_command: |
| 323 |
commands.pop(commands.index(no_host_reachability_command)) |
| 324 |
commands.append(no_host_reachability_command) |
| 325 |
|
| 326 |
if no_source_interface_command: |
| 327 |
commands.pop(commands.index(no_source_interface_command)) |
| 328 |
commands.append(no_source_interface_command) |
| 329 |
|
| 330 |
commands.insert(0, "terminal dont-ask") |
| 331 |
return commands |
| 332 |
|
| 333 |
|
| 334 |
def gsa_tcam_check(module): |
| 335 |
""" |
| 336 |
global_suppress_arp is an N9k-only command that requires TCAM resources. |
| 337 |
This method checks the current TCAM allocation. |
| 338 |
Note that changing tcam_size requires a switch reboot to take effect. |
| 339 |
""" |
| 340 |
cmds = [{"command": "show hardware access-list tcam region", "output": "json"}] |
| 341 |
body = run_commands(module, cmds) |
| 342 |
if body: |
| 343 |
tcam_region = body[0]["TCAM_Region"]["TABLE_Sizes"]["ROW_Sizes"] |
| 344 |
if bool( |
| 345 |
[ |
| 346 |
i |
| 347 |
for i in tcam_region |
| 348 |
if i["type"].startswith("Ingress ARP-Ether ACL") and i["tcam_size"] == "0" |
| 349 |
], |
| 350 |
): |
| 351 |
msg = ( |
| 352 |
"'show hardware access-list tcam region' indicates 'ARP-Ether' tcam size is 0 (no allocated resources). " |
| 353 |
+ "'global_suppress_arp' will be rejected by device." |
| 354 |
) |
| 355 |
module.fail_json(msg=msg) |
| 356 |
|
| 357 |
|
| 358 |
def state_present(module, existing, proposed, candidate): |
| 359 |
commands = list() |
| 360 |
proposed_commands = apply_key_map(PARAM_TO_COMMAND_KEYMAP, proposed) |
| 361 |
existing_commands = apply_key_map(PARAM_TO_COMMAND_KEYMAP, existing) |
| 362 |
for key, value in proposed_commands.items(): |
| 363 |
if value is True: |
| 364 |
commands.append(key) |
| 365 |
|
| 366 |
elif value is False: |
| 367 |
commands.append("no {0}".format(key)) |
| 368 |
|
| 369 |
elif value == "default": |
| 370 |
if existing_commands.get(key): |
| 371 |
existing_value = existing_commands.get(key) |
| 372 |
if "global mcast-group" in key: |
| 373 |
commands.append("no {0}".format(key)) |
| 374 |
else: |
| 375 |
commands.append("no {0} {1}".format(key, existing_value)) |
| 376 |
else: |
| 377 |
if key.replace(" ", "_").replace("-", "_") in BOOL_PARAMS: |
| 378 |
commands.append("no {0}".format(key.lower())) |
| 379 |
module.exit_json(commands=commands) |
| 380 |
else: |
| 381 |
if "L2" in key: |
| 382 |
commands.append("global mcast-group " + value + " L2") |
| 383 |
elif "L3" in key: |
| 384 |
commands.append("global mcast-group " + value + " L3") |
| 385 |
else: |
| 386 |
command = "{0} {1}".format(key, value.lower()) |
| 387 |
commands.append(command) |
| 388 |
|
| 389 |
if commands: |
| 390 |
commands = fix_commands(commands, module) |
| 391 |
parents = ["interface {0}".format(module.params["interface"].lower())] |
| 392 |
candidate.add(commands, parents=parents) |
| 393 |
else: |
| 394 |
if not existing and module.params["interface"]: |
| 395 |
commands = ["interface {0}".format(module.params["interface"].lower())] |
| 396 |
candidate.add(commands, parents=[]) |
| 397 |
|
| 398 |
|
| 399 |
def state_absent(module, existing, proposed, candidate): |
| 400 |
commands = ["no interface {0}".format(module.params["interface"].lower())] |
| 401 |
candidate.add(commands, parents=[]) |
| 402 |
|
| 403 |
|
| 404 |
def main(): |
| 405 |
argument_spec = dict( |
| 406 |
interface=dict(required=True, type="str"), |
| 407 |
description=dict(required=False, type="str"), |
| 408 |
host_reachability=dict(required=False, type="bool"), |
| 409 |
global_ingress_replication_bgp=dict(required=False, type="bool"), |
| 410 |
global_suppress_arp=dict(required=False, type="bool"), |
| 411 |
global_mcast_group_L2=dict(required=False, type="str"), |
| 412 |
global_mcast_group_L3=dict(required=False, type="str"), |
| 413 |
shutdown=dict(required=False, type="bool"), |
| 414 |
source_interface=dict(required=False, type="str"), |
| 415 |
source_interface_hold_down_time=dict(required=False, type="str"), |
| 416 |
state=dict(choices=["present", "absent"], default="present", required=False), |
| 417 |
multisite_border_gateway_interface=dict(required=False, type="str"), |
| 418 |
advertise_virtual_rmac=dict(required=False, type="bool"), |
| 419 |
) |
| 420 |
|
| 421 |
mutually_exclusive = [("global_ingress_replication_bgp", "global_mcast_group_L2")] |
| 422 |
|
| 423 |
module = AnsibleModule( |
| 424 |
argument_spec=argument_spec, |
| 425 |
mutually_exclusive=mutually_exclusive, |
| 426 |
supports_check_mode=True, |
| 427 |
) |
| 428 |
|
| 429 |
warnings = list() |
| 430 |
result = {"changed": False, "commands": [], "warnings": warnings} |
| 431 |
|
| 432 |
state = module.params["state"] |
| 433 |
|
| 434 |
args = PARAM_TO_COMMAND_KEYMAP.keys() |
| 435 |
|
| 436 |
existing = get_existing(module, args) |
| 437 |
proposed_args = dict((k, v) for k, v in module.params.items() if v is not None and k in args) |
| 438 |
proposed = {} |
| 439 |
for key, value in proposed_args.items(): |
| 440 |
if key != "interface": |
| 441 |
if str(value).lower() == "default": |
| 442 |
value = PARAM_TO_DEFAULT_KEYMAP.get(key) |
| 443 |
if value is None: |
| 444 |
if key in BOOL_PARAMS: |
| 445 |
value = False |
| 446 |
else: |
| 447 |
value = "default" |
| 448 |
if str(existing.get(key)).lower() != str(value).lower(): |
| 449 |
proposed[key] = value |
| 450 |
|
| 451 |
candidate = CustomNetworkConfig(indent=3) |
| 452 |
|
| 453 |
if proposed.get("global_suppress_arp"): |
| 454 |
gsa_tcam_check(module) |
| 455 |
if state == "present": |
| 456 |
if not existing: |
| 457 |
warnings.append( |
| 458 |
"The proposed NVE interface did not exist. " |
| 459 |
"It's recommended to use nxos_interfaces to create " |
| 460 |
"all logical interfaces.", |
| 461 |
) |
| 462 |
state_present(module, existing, proposed, candidate) |
| 463 |
elif state == "absent" and existing: |
| 464 |
state_absent(module, existing, proposed, candidate) |
| 465 |
|
| 466 |
if candidate: |
| 467 |
candidate = candidate.items_text() |
| 468 |
result["commands"] = candidate |
| 469 |
result["changed"] = True |
| 470 |
load_config(module, candidate) |
| 471 |
|
| 472 |
module.exit_json(**result) |
| 473 |
|
| 474 |
|
| 475 |
if __name__ == "__main__": |
| 476 |
main() |