| 1 |
#!/usr/bin/python |
| 2 |
from __future__ import absolute_import, division, print_function |
| 3 |
|
| 4 |
|
| 5 |
__metaclass__ = type |
| 6 |
|
| 7 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 8 |
|
| 9 |
DOCUMENTATION = """ |
| 10 |
module: nxos_hsrp |
| 11 |
extends_documentation_fragment: |
| 12 |
- cisco.nxos.nxos |
| 13 |
short_description: (deprecated, removed after 2028-06-01) Manages HSRP configuration on NX-OS switches. |
| 14 |
description: |
| 15 |
- Manages HSRP configuration on NX-OS switches. |
| 16 |
version_added: 1.0.0 |
| 17 |
author: |
| 18 |
- Jason Edelman (@jedelman8) |
| 19 |
- Gabriele Gerbino (@GGabriele) |
| 20 |
notes: |
| 21 |
- Tested against NXOSv 7.3.(0)D1(1) on VIRL |
| 22 |
- Unsupported for Cisco MDS |
| 23 |
- HSRP feature needs to be enabled first on the system. |
| 24 |
- SVIs must exist before using this module. |
| 25 |
- Interface must be a L3 port before using this module. |
| 26 |
- HSRP cannot be configured on loopback interfaces. |
| 27 |
- MD5 authentication is only possible with HSRPv2 while it is ignored if HSRPv1 is |
| 28 |
used instead, while it will not raise any error. Here we allow MD5 authentication |
| 29 |
only with HSRPv2 in order to enforce better practice. |
| 30 |
options: |
| 31 |
group: |
| 32 |
description: |
| 33 |
- HSRP group number. |
| 34 |
required: true |
| 35 |
type: str |
| 36 |
interface: |
| 37 |
description: |
| 38 |
- Full name of interface that is being managed for HSRP. |
| 39 |
required: true |
| 40 |
type: str |
| 41 |
version: |
| 42 |
description: |
| 43 |
- HSRP version. |
| 44 |
default: '1' |
| 45 |
choices: |
| 46 |
- '1' |
| 47 |
- '2' |
| 48 |
type: str |
| 49 |
priority: |
| 50 |
description: |
| 51 |
- HSRP priority or keyword 'default'. |
| 52 |
type: str |
| 53 |
preempt: |
| 54 |
description: |
| 55 |
- Enable/Disable preempt. |
| 56 |
choices: |
| 57 |
- enabled |
| 58 |
- disabled |
| 59 |
type: str |
| 60 |
vip: |
| 61 |
description: |
| 62 |
- HSRP virtual IP address or keyword 'default' |
| 63 |
type: str |
| 64 |
auth_string: |
| 65 |
description: |
| 66 |
- Authentication string. If this needs to be hidden(for md5 type), the string |
| 67 |
should be 7 followed by the key string. Otherwise, it can be 0 followed by key |
| 68 |
string or just key string (for backward compatibility). For text type, this |
| 69 |
should be just be a key string. if this is 'default', authentication is removed. |
| 70 |
type: str |
| 71 |
auth_type: |
| 72 |
description: |
| 73 |
- Authentication type. |
| 74 |
choices: |
| 75 |
- text |
| 76 |
- md5 |
| 77 |
type: str |
| 78 |
state: |
| 79 |
description: |
| 80 |
- Specify desired state of the resource. |
| 81 |
choices: |
| 82 |
- present |
| 83 |
- absent |
| 84 |
default: present |
| 85 |
type: str |
| 86 |
""" |
| 87 |
|
| 88 |
EXAMPLES = """ |
| 89 |
- name: Ensure HSRP is configured with following params on a SVI |
| 90 |
cisco.nxos.nxos_hsrp: |
| 91 |
group: 10 |
| 92 |
vip: 10.1.1.1 |
| 93 |
priority: 150 |
| 94 |
interface: vlan10 |
| 95 |
preempt: enabled |
| 96 |
|
| 97 |
- name: Ensure HSRP is configured with following params on a SVI with clear text authentication |
| 98 |
cisco.nxos.nxos_hsrp: |
| 99 |
group: 10 |
| 100 |
vip: 10.1.1.1 |
| 101 |
priority: 150 |
| 102 |
interface: vlan10 |
| 103 |
preempt: enabled |
| 104 |
auth_type: text |
| 105 |
auth_string: CISCO |
| 106 |
|
| 107 |
- name: Ensure HSRP is configured with md5 authentication and clear authentication |
| 108 |
string |
| 109 |
cisco.nxos.nxos_hsrp: |
| 110 |
group: 10 |
| 111 |
vip: 10.1.1.1 |
| 112 |
priority: 150 |
| 113 |
interface: vlan10 |
| 114 |
preempt: enabled |
| 115 |
auth_type: md5 |
| 116 |
auth_string: 0 1234 |
| 117 |
|
| 118 |
- name: Ensure HSRP is configured with md5 authentication and hidden authentication |
| 119 |
string |
| 120 |
cisco.nxos.nxos_hsrp: |
| 121 |
group: 10 |
| 122 |
vip: 10.1.1.1 |
| 123 |
priority: 150 |
| 124 |
interface: vlan10 |
| 125 |
preempt: enabled |
| 126 |
auth_type: md5 |
| 127 |
auth_string: 7 1234 |
| 128 |
|
| 129 |
- name: Remove HSRP config for given interface, group, and VIP |
| 130 |
cisco.nxos.nxos_hsrp: |
| 131 |
group: 10 |
| 132 |
interface: vlan10 |
| 133 |
vip: 10.1.1.1 |
| 134 |
state: absent |
| 135 |
""" |
| 136 |
|
| 137 |
RETURN = r""" |
| 138 |
commands: |
| 139 |
description: commands sent to the device |
| 140 |
returned: always |
| 141 |
type: list |
| 142 |
sample: ["interface vlan10", "hsrp version 2", "hsrp 30", "ip 10.30.1.1"] |
| 143 |
""" |
| 144 |
|
| 145 |
from ansible.module_utils.basic import AnsibleModule |
| 146 |
|
| 147 |
from ansible_collections.cisco.nxos.plugins.module_utils.network.nxos.nxos import ( |
| 148 |
get_capabilities, |
| 149 |
get_interface_type, |
| 150 |
load_config, |
| 151 |
run_commands, |
| 152 |
) |
| 153 |
|
| 154 |
|
| 155 |
PARAM_TO_DEFAULT_KEYMAP = { |
| 156 |
"vip": None, |
| 157 |
"priority": "100", |
| 158 |
"auth_type": "text", |
| 159 |
"auth_string": "cisco", |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
def apply_key_map(key_map, table): |
| 164 |
new_dict = {} |
| 165 |
for key in table: |
| 166 |
new_key = key_map.get(key) |
| 167 |
if new_key: |
| 168 |
value = table.get(key) |
| 169 |
if value: |
| 170 |
new_dict[new_key] = str(value) |
| 171 |
else: |
| 172 |
new_dict[new_key] = value |
| 173 |
return new_dict |
| 174 |
|
| 175 |
|
| 176 |
def get_interface_mode(interface, intf_type, module): |
| 177 |
command = "show interface {0} | json".format(interface) |
| 178 |
interface = {} |
| 179 |
mode = "unknown" |
| 180 |
try: |
| 181 |
body = run_commands(module, [command])[0] |
| 182 |
except IndexError: |
| 183 |
return None |
| 184 |
|
| 185 |
if intf_type in ["ethernet", "portchannel"]: |
| 186 |
interface_table = body["TABLE_interface"]["ROW_interface"] |
| 187 |
mode = str(interface_table.get("eth_mode", "layer3")) |
| 188 |
if mode == "access" or mode == "trunk": |
| 189 |
mode = "layer2" |
| 190 |
elif intf_type == "svi": |
| 191 |
mode = "layer3" |
| 192 |
return mode |
| 193 |
|
| 194 |
|
| 195 |
def get_hsrp_group(group, interface, module): |
| 196 |
command = "show hsrp group {0} all | json".format(group) |
| 197 |
hsrp = {} |
| 198 |
|
| 199 |
hsrp_key = { |
| 200 |
"sh_if_index": "interface", |
| 201 |
"sh_group_num": "group", |
| 202 |
"sh_group_version": "version", |
| 203 |
"sh_cfg_prio": "priority", |
| 204 |
"sh_preempt": "preempt", |
| 205 |
"sh_vip": "vip", |
| 206 |
"sh_authentication_type": "auth_type", |
| 207 |
"sh_keystring_attr": "auth_enc", |
| 208 |
"sh_authentication_data": "auth_string", |
| 209 |
} |
| 210 |
|
| 211 |
try: |
| 212 |
body = run_commands(module, [command])[0] |
| 213 |
hsrp_table = body["TABLE_grp_detail"]["ROW_grp_detail"] |
| 214 |
if "sh_keystring_attr" not in hsrp_table: |
| 215 |
del hsrp_key["sh_keystring_attr"] |
| 216 |
if "unknown enum:" in str(hsrp_table): |
| 217 |
hsrp_table = get_hsrp_group_unknown_enum(module, command, hsrp_table) |
| 218 |
except (AttributeError, IndexError, TypeError, KeyError): |
| 219 |
return {} |
| 220 |
|
| 221 |
if isinstance(hsrp_table, dict): |
| 222 |
hsrp_table = [hsrp_table] |
| 223 |
|
| 224 |
for hsrp_group in hsrp_table: |
| 225 |
parsed_hsrp = apply_key_map(hsrp_key, hsrp_group) |
| 226 |
|
| 227 |
parsed_hsrp["interface"] = parsed_hsrp["interface"].lower() |
| 228 |
|
| 229 |
if parsed_hsrp["version"] == "v1": |
| 230 |
parsed_hsrp["version"] = "1" |
| 231 |
elif parsed_hsrp["version"] == "v2": |
| 232 |
parsed_hsrp["version"] = "2" |
| 233 |
|
| 234 |
if parsed_hsrp["auth_type"] == "md5": |
| 235 |
if parsed_hsrp["auth_enc"] == "hidden": |
| 236 |
parsed_hsrp["auth_enc"] = "7" |
| 237 |
else: |
| 238 |
parsed_hsrp["auth_enc"] = "0" |
| 239 |
|
| 240 |
if parsed_hsrp["interface"] == interface: |
| 241 |
return parsed_hsrp |
| 242 |
|
| 243 |
return hsrp |
| 244 |
|
| 245 |
|
| 246 |
def get_hsrp_group_unknown_enum(module, command, hsrp_table): |
| 247 |
"""Some older NXOS images fail to set the attr values when using structured output and |
| 248 |
instead set the values to <unknown enum>. This fallback method is a workaround that |
| 249 |
uses an unstructured (text) request to query the device a second time. |
| 250 |
'sh_preempt' is currently the only attr affected. Add checks for other attrs as needed. |
| 251 |
""" |
| 252 |
if "unknown enum:" in hsrp_table["sh_preempt"]: |
| 253 |
cmd = {"output": "text", "command": command.split("|")[0]} |
| 254 |
out = run_commands(module, cmd)[0] |
| 255 |
hsrp_table["sh_preempt"] = "enabled" if ("may preempt" in out) else "disabled" |
| 256 |
return hsrp_table |
| 257 |
|
| 258 |
|
| 259 |
def get_commands_remove_hsrp(group, interface): |
| 260 |
commands = ["interface {0}".format(interface), "no hsrp {0}".format(group)] |
| 261 |
return commands |
| 262 |
|
| 263 |
|
| 264 |
def get_commands_config_hsrp(delta, interface, args, existing): |
| 265 |
commands = [] |
| 266 |
|
| 267 |
config_args = { |
| 268 |
"group": "hsrp {group}", |
| 269 |
"priority": "{priority}", |
| 270 |
"preempt": "{preempt}", |
| 271 |
"vip": "{vip}", |
| 272 |
} |
| 273 |
|
| 274 |
preempt = delta.get("preempt", None) |
| 275 |
group = delta.get("group", None) |
| 276 |
vip = delta.get("vip", None) |
| 277 |
priority = delta.get("priority", None) |
| 278 |
|
| 279 |
if preempt: |
| 280 |
if preempt == "enabled": |
| 281 |
delta["preempt"] = "preempt" |
| 282 |
elif preempt == "disabled": |
| 283 |
delta["preempt"] = "no preempt" |
| 284 |
|
| 285 |
if priority: |
| 286 |
if priority == "default": |
| 287 |
if existing and existing.get("priority") != PARAM_TO_DEFAULT_KEYMAP.get("priority"): |
| 288 |
delta["priority"] = "no priority" |
| 289 |
else: |
| 290 |
del delta["priority"] |
| 291 |
else: |
| 292 |
delta["priority"] = "priority {0}".format(delta["priority"]) |
| 293 |
|
| 294 |
if vip: |
| 295 |
if vip == "default": |
| 296 |
if existing and existing.get("vip") != PARAM_TO_DEFAULT_KEYMAP.get("vip"): |
| 297 |
delta["vip"] = "no ip" |
| 298 |
else: |
| 299 |
del delta["vip"] |
| 300 |
else: |
| 301 |
delta["vip"] = "ip {0}".format(delta["vip"]) |
| 302 |
|
| 303 |
for key in delta: |
| 304 |
command = config_args.get(key, "DNE").format(**delta) |
| 305 |
if command and command != "DNE": |
| 306 |
if key == "group": |
| 307 |
commands.insert(0, command) |
| 308 |
else: |
| 309 |
commands.append(command) |
| 310 |
command = None |
| 311 |
|
| 312 |
auth_type = delta.get("auth_type", None) |
| 313 |
auth_string = delta.get("auth_string", None) |
| 314 |
auth_enc = delta.get("auth_enc", None) |
| 315 |
if auth_type or auth_string: |
| 316 |
if not auth_type: |
| 317 |
auth_type = args["auth_type"] |
| 318 |
elif not auth_string: |
| 319 |
auth_string = args["auth_string"] |
| 320 |
if auth_string != "default": |
| 321 |
if auth_type == "md5": |
| 322 |
command = "authentication md5 key-string {0} {1}".format(auth_enc, auth_string) |
| 323 |
commands.append(command) |
| 324 |
elif auth_type == "text": |
| 325 |
command = "authentication text {0}".format(auth_string) |
| 326 |
commands.append(command) |
| 327 |
else: |
| 328 |
if existing and existing.get("auth_string") != PARAM_TO_DEFAULT_KEYMAP.get( |
| 329 |
"auth_string", |
| 330 |
): |
| 331 |
commands.append("no authentication") |
| 332 |
|
| 333 |
if commands and not group: |
| 334 |
commands.insert(0, "hsrp {0}".format(args["group"])) |
| 335 |
|
| 336 |
version = delta.get("version", None) |
| 337 |
if version: |
| 338 |
if version == "2": |
| 339 |
command = "hsrp version 2" |
| 340 |
elif version == "1": |
| 341 |
command = "hsrp version 1" |
| 342 |
commands.insert(0, command) |
| 343 |
commands.insert(0, "interface {0}".format(interface)) |
| 344 |
|
| 345 |
if commands: |
| 346 |
if not commands[0].startswith("interface"): |
| 347 |
commands.insert(0, "interface {0}".format(interface)) |
| 348 |
|
| 349 |
return commands |
| 350 |
|
| 351 |
|
| 352 |
def is_default(interface, module): |
| 353 |
command = "show run interface {0}".format(interface) |
| 354 |
|
| 355 |
try: |
| 356 |
body = run_commands(module, [command], check_rc=False)[0] |
| 357 |
if "invalid" in body.lower(): |
| 358 |
return "DNE" |
| 359 |
else: |
| 360 |
raw_list = body.split("\n") |
| 361 |
if raw_list[-1].startswith("interface"): |
| 362 |
return True |
| 363 |
else: |
| 364 |
return False |
| 365 |
except KeyError: |
| 366 |
return "DNE" |
| 367 |
|
| 368 |
|
| 369 |
def validate_config(body, vip, module): |
| 370 |
new_body = "".join(body) |
| 371 |
if "invalid ip address" in new_body.lower(): |
| 372 |
module.fail_json(msg="Invalid VIP. Possible duplicate IP address.", vip=vip) |
| 373 |
|
| 374 |
|
| 375 |
def main(): |
| 376 |
argument_spec = dict( |
| 377 |
group=dict(required=True, type="str"), |
| 378 |
interface=dict(required=True), |
| 379 |
version=dict(choices=["1", "2"], default="1", required=False), |
| 380 |
priority=dict(type="str", required=False), |
| 381 |
preempt=dict(type="str", choices=["disabled", "enabled"], required=False), |
| 382 |
vip=dict(type="str", required=False), |
| 383 |
auth_type=dict(choices=["text", "md5"], required=False), |
| 384 |
auth_string=dict(type="str", required=False), |
| 385 |
state=dict(choices=["absent", "present"], required=False, default="present"), |
| 386 |
) |
| 387 |
|
| 388 |
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) |
| 389 |
|
| 390 |
warnings = list() |
| 391 |
results = dict(changed=False, warnings=warnings) |
| 392 |
|
| 393 |
interface = module.params["interface"].lower() |
| 394 |
group = module.params["group"] |
| 395 |
version = module.params["version"] |
| 396 |
state = module.params["state"] |
| 397 |
priority = module.params["priority"] |
| 398 |
preempt = module.params["preempt"] |
| 399 |
vip = module.params["vip"] |
| 400 |
auth_type = module.params["auth_type"] |
| 401 |
auth_full_string = module.params["auth_string"] |
| 402 |
auth_enc = "0" |
| 403 |
auth_string = None |
| 404 |
if auth_full_string: |
| 405 |
kstr = auth_full_string.split() |
| 406 |
if len(kstr) == 2: |
| 407 |
auth_enc = kstr[0] |
| 408 |
auth_string = kstr[1] |
| 409 |
elif len(kstr) == 1: |
| 410 |
auth_string = kstr[0] |
| 411 |
else: |
| 412 |
module.fail_json(msg="Invalid auth_string") |
| 413 |
if auth_enc != "0" and auth_enc != "7": |
| 414 |
module.fail_json(msg="Invalid auth_string, only 0 or 7 allowed") |
| 415 |
|
| 416 |
device_info = get_capabilities(module) |
| 417 |
network_api = device_info.get("network_api", "nxapi") |
| 418 |
|
| 419 |
intf_type = get_interface_type(interface) |
| 420 |
if intf_type != "ethernet" and network_api == "cliconf": |
| 421 |
if is_default(interface, module) == "DNE": |
| 422 |
module.fail_json( |
| 423 |
msg="That interface does not exist yet. Create " "it first.", |
| 424 |
interface=interface, |
| 425 |
) |
| 426 |
if intf_type == "loopback": |
| 427 |
module.fail_json( |
| 428 |
msg="Loopback interfaces don't support HSRP.", |
| 429 |
interface=interface, |
| 430 |
) |
| 431 |
|
| 432 |
mode = get_interface_mode(interface, intf_type, module) |
| 433 |
if mode == "layer2": |
| 434 |
module.fail_json( |
| 435 |
msg="That interface is a layer2 port.\nMake it " "a layer 3 port first.", |
| 436 |
interface=interface, |
| 437 |
) |
| 438 |
|
| 439 |
if auth_type or auth_string: |
| 440 |
if not (auth_type and auth_string): |
| 441 |
module.fail_json( |
| 442 |
msg="When using auth parameters, you need BOTH " "auth_type AND auth_string.", |
| 443 |
) |
| 444 |
|
| 445 |
args = dict( |
| 446 |
group=group, |
| 447 |
version=version, |
| 448 |
priority=priority, |
| 449 |
preempt=preempt, |
| 450 |
vip=vip, |
| 451 |
auth_type=auth_type, |
| 452 |
auth_string=auth_string, |
| 453 |
auth_enc=auth_enc, |
| 454 |
) |
| 455 |
|
| 456 |
proposed = dict((k, v) for k, v in args.items() if v is not None) |
| 457 |
|
| 458 |
existing = get_hsrp_group(group, interface, module) |
| 459 |
|
| 460 |
# This will enforce better practice with md5 and hsrp version. |
| 461 |
if proposed.get("auth_type", None) == "md5": |
| 462 |
if proposed["version"] == "1": |
| 463 |
module.fail_json(msg="It's recommended to use HSRP v2 " "when auth_type=md5") |
| 464 |
|
| 465 |
elif not proposed.get("auth_type", None) and existing: |
| 466 |
if (proposed["version"] == "1" and existing["auth_type"] == "md5") and state == "present": |
| 467 |
module.fail_json( |
| 468 |
msg="Existing auth_type is md5. It's recommended " "to use HSRP v2 when using md5", |
| 469 |
) |
| 470 |
|
| 471 |
commands = [] |
| 472 |
if state == "present": |
| 473 |
delta = dict(set(proposed.items()).difference(existing.items())) |
| 474 |
if delta: |
| 475 |
command = get_commands_config_hsrp(delta, interface, args, existing) |
| 476 |
commands.extend(command) |
| 477 |
|
| 478 |
elif state == "absent": |
| 479 |
if existing: |
| 480 |
command = get_commands_remove_hsrp(group, interface) |
| 481 |
commands.extend(command) |
| 482 |
|
| 483 |
if commands: |
| 484 |
if module.check_mode: |
| 485 |
module.exit_json(**results) |
| 486 |
else: |
| 487 |
load_config(module, commands) |
| 488 |
|
| 489 |
# validate IP |
| 490 |
if network_api == "cliconf" and state == "present": |
| 491 |
commands.insert(0, "config t") |
| 492 |
body = run_commands(module, commands) |
| 493 |
validate_config(body, vip, module) |
| 494 |
|
| 495 |
results["changed"] = True |
| 496 |
|
| 497 |
if "configure" in commands: |
| 498 |
commands.pop(0) |
| 499 |
|
| 500 |
results["commands"] = commands |
| 501 |
module.exit_json(**results) |
| 502 |
|
| 503 |
|
| 504 |
if __name__ == "__main__": |
| 505 |
main() |