| 1 |
#!/usr/bin/python |
| 2 |
# Copyright: Ansible Project |
| 3 |
# GNU General Public License v3.0+ (see COPYING or |
| 4 |
# https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
from __future__ import absolute_import, division, print_function |
| 7 |
|
| 8 |
|
| 9 |
DOCUMENTATION = """ |
| 10 |
module: nxos_devicealias |
| 11 |
short_description: Configuration of device alias for Cisco NXOS MDS Switches. |
| 12 |
description: |
| 13 |
- Configuration of device alias for Cisco MDS NXOS. |
| 14 |
version_added: 1.0.0 |
| 15 |
author: |
| 16 |
- Suhas Bharadwaj (@srbharadwaj) ([email protected]) |
| 17 |
notes: |
| 18 |
- Tested against Cisco MDS NX-OS 8.4(1) |
| 19 |
options: |
| 20 |
distribute: |
| 21 |
description: |
| 22 |
- Enable/Disable device-alias distribution |
| 23 |
type: bool |
| 24 |
mode: |
| 25 |
description: |
| 26 |
- Mode of devices-alias, basic or enhanced |
| 27 |
choices: |
| 28 |
- basic |
| 29 |
- enhanced |
| 30 |
type: str |
| 31 |
da: |
| 32 |
description: |
| 33 |
- List of device-alias to be added or removed |
| 34 |
type: list |
| 35 |
elements: dict |
| 36 |
suboptions: |
| 37 |
name: |
| 38 |
description: |
| 39 |
- Name of the device-alias to be added or removed |
| 40 |
required: true |
| 41 |
type: str |
| 42 |
pwwn: |
| 43 |
description: |
| 44 |
- pwwn to which the name needs to be associated with |
| 45 |
type: str |
| 46 |
remove: |
| 47 |
description: |
| 48 |
- Removes the device-alias if set to True |
| 49 |
type: bool |
| 50 |
default: false |
| 51 |
rename: |
| 52 |
description: |
| 53 |
- List of device-alias to be renamed |
| 54 |
type: list |
| 55 |
elements: dict |
| 56 |
suboptions: |
| 57 |
old_name: |
| 58 |
description: |
| 59 |
- Old name of the device-alias that needs to be renamed |
| 60 |
required: true |
| 61 |
type: str |
| 62 |
new_name: |
| 63 |
description: |
| 64 |
- New name of the device-alias |
| 65 |
required: true |
| 66 |
type: str |
| 67 |
""" |
| 68 |
|
| 69 |
EXAMPLES = """ |
| 70 |
- name: Test that device alias module works |
| 71 |
cisco.nxos.nxos_devicealias: |
| 72 |
da: |
| 73 |
- name: test1_add |
| 74 |
pwwn: 56:2:22:11:22:88:11:67 |
| 75 |
- name: test2_add |
| 76 |
pwwn: 65:22:22:11:22:22:11:d |
| 77 |
- name: dev1 |
| 78 |
remove: true |
| 79 |
- name: dev2 |
| 80 |
remove: true |
| 81 |
distribute: true |
| 82 |
mode: enhanced |
| 83 |
rename: |
| 84 |
- new_name: bcd |
| 85 |
old_name: abc |
| 86 |
- new_name: bcd1 |
| 87 |
old_name: abc1 |
| 88 |
""" |
| 89 |
|
| 90 |
RETURN = """ |
| 91 |
commands: |
| 92 |
description: commands sent to the device |
| 93 |
returned: always |
| 94 |
type: list |
| 95 |
sample: |
| 96 |
- terminal dont-ask |
| 97 |
- device-alias database |
| 98 |
- device-alias name somename pwwn 10:00:00:00:89:a1:01:03 |
| 99 |
- device-alias name somename1 pwwn 10:00:00:00:89:a1:02:03 |
| 100 |
- device-alias commit |
| 101 |
- no terminal dont-ask |
| 102 |
""" |
| 103 |
|
| 104 |
import string |
| 105 |
|
| 106 |
from ansible.module_utils.basic import AnsibleModule |
| 107 |
|
| 108 |
from ansible_collections.cisco.nxos.plugins.module_utils.network.nxos.nxos import ( |
| 109 |
load_config, |
| 110 |
run_commands, |
| 111 |
) |
| 112 |
|
| 113 |
|
| 114 |
__metaclass__ = type |
| 115 |
|
| 116 |
VALID_DA_CHARS = ("-", "_", "$", "^") |
| 117 |
|
| 118 |
|
| 119 |
class showDeviceAliasStatus(object): |
| 120 |
"""docstring for showDeviceAliasStatus""" |
| 121 |
|
| 122 |
def __init__(self, module): |
| 123 |
self.module = module |
| 124 |
self.distribute = "" |
| 125 |
self.mode = "" |
| 126 |
self.locked = False |
| 127 |
self.update() |
| 128 |
|
| 129 |
def execute_show_cmd(self, cmd): |
| 130 |
output = execute_show_command(cmd, self.module)[0] |
| 131 |
return output |
| 132 |
|
| 133 |
def update(self): |
| 134 |
command = "show device-alias status" |
| 135 |
output = self.execute_show_cmd(command).split("\n") |
| 136 |
for o in output: |
| 137 |
if "Fabric Distribution" in o: |
| 138 |
self.distribute = o.split(":")[1].strip().lower() |
| 139 |
if "Mode" in o: |
| 140 |
self.mode = o.split("Mode:")[1].strip().lower() |
| 141 |
if "Locked" in o: |
| 142 |
self.locked = True |
| 143 |
|
| 144 |
def isLocked(self): |
| 145 |
return self.locked |
| 146 |
|
| 147 |
def getDistribute(self): |
| 148 |
return self.distribute |
| 149 |
|
| 150 |
def getMode(self): |
| 151 |
return self.mode |
| 152 |
|
| 153 |
|
| 154 |
class showDeviceAliasDatabase(object): |
| 155 |
"""docstring for showDeviceAliasDatabase""" |
| 156 |
|
| 157 |
def __init__(self, module): |
| 158 |
self.module = module |
| 159 |
self.da_dict = {} |
| 160 |
self.update() |
| 161 |
|
| 162 |
def execute_show_cmd(self, cmd): |
| 163 |
output = execute_show_command(cmd, self.module)[0] |
| 164 |
return output |
| 165 |
|
| 166 |
def update(self): |
| 167 |
command = "show device-alias database" |
| 168 |
# output = execute_show_command(command, self.module)[0].split("\n") |
| 169 |
output = self.execute_show_cmd(command) |
| 170 |
self.da_list = output.split("\n") |
| 171 |
for eachline in self.da_list: |
| 172 |
if "device-alias" in eachline: |
| 173 |
sv = eachline.strip().split() |
| 174 |
self.da_dict[sv[2]] = sv[4] |
| 175 |
|
| 176 |
def isNameInDaDatabase(self, name): |
| 177 |
return name in self.da_dict.keys() |
| 178 |
|
| 179 |
def isPwwnInDaDatabase(self, pwwn): |
| 180 |
newpwwn = ":".join( |
| 181 |
["0" + str(ep) if len(ep) == 1 else ep for ep in pwwn.split(":")], |
| 182 |
) |
| 183 |
return newpwwn in self.da_dict.values() |
| 184 |
|
| 185 |
def isNamePwwnPresentInDatabase(self, name, pwwn): |
| 186 |
newpwwn = ":".join( |
| 187 |
["0" + str(ep) if len(ep) == 1 else ep for ep in pwwn.split(":")], |
| 188 |
) |
| 189 |
if name in self.da_dict.keys(): |
| 190 |
if newpwwn == self.da_dict[name]: |
| 191 |
return True |
| 192 |
return False |
| 193 |
|
| 194 |
def getPwwnByName(self, name): |
| 195 |
if name in self.da_dict.keys(): |
| 196 |
return self.da_dict[name] |
| 197 |
else: |
| 198 |
return None |
| 199 |
|
| 200 |
def getNameByPwwn(self, pwwn): |
| 201 |
newpwwn = ":".join( |
| 202 |
["0" + str(ep) if len(ep) == 1 else ep for ep in pwwn.split(":")], |
| 203 |
) |
| 204 |
for n, p in self.da_dict.items(): |
| 205 |
if p == newpwwn: |
| 206 |
return n |
| 207 |
return None |
| 208 |
|
| 209 |
|
| 210 |
def isPwwnValid(pwwn): |
| 211 |
pwwnsplit = pwwn.split(":") |
| 212 |
if len(pwwnsplit) != 8: |
| 213 |
return False |
| 214 |
for eachpwwnsplit in pwwnsplit: |
| 215 |
if len(eachpwwnsplit) > 2 or len(eachpwwnsplit) < 1: |
| 216 |
return False |
| 217 |
if not all(c in string.hexdigits for c in eachpwwnsplit): |
| 218 |
return False |
| 219 |
return True |
| 220 |
|
| 221 |
|
| 222 |
def isNameValid(name): |
| 223 |
if not name[0].isalpha(): |
| 224 |
# Illegal first character. Name must start with a letter |
| 225 |
return False |
| 226 |
if len(name) > 64: |
| 227 |
return False |
| 228 |
for character in name: |
| 229 |
if not character.isalnum() and character not in VALID_DA_CHARS: |
| 230 |
return False |
| 231 |
return True |
| 232 |
|
| 233 |
|
| 234 |
def execute_show_command(command, module, command_type="cli_show"): |
| 235 |
output = "text" |
| 236 |
commands = [{"command": command, "output": output}] |
| 237 |
out = run_commands(module, commands) |
| 238 |
return out |
| 239 |
|
| 240 |
|
| 241 |
def flatten_list(command_lists): |
| 242 |
flat_command_list = [] |
| 243 |
for command in command_lists: |
| 244 |
if isinstance(command, list): |
| 245 |
flat_command_list.extend(command) |
| 246 |
else: |
| 247 |
flat_command_list.append(command) |
| 248 |
return flat_command_list |
| 249 |
|
| 250 |
|
| 251 |
def main(): |
| 252 |
element_spec = dict( |
| 253 |
name=dict(required=True, type="str"), |
| 254 |
pwwn=dict(type="str"), |
| 255 |
remove=dict(type="bool", default=False), |
| 256 |
) |
| 257 |
|
| 258 |
element_spec_rename = dict( |
| 259 |
old_name=dict(required=True, type="str"), |
| 260 |
new_name=dict(required=True, type="str"), |
| 261 |
) |
| 262 |
|
| 263 |
argument_spec = dict( |
| 264 |
distribute=dict(type="bool"), |
| 265 |
mode=dict(type="str", choices=["enhanced", "basic"]), |
| 266 |
da=dict(type="list", elements="dict", options=element_spec), |
| 267 |
rename=dict(type="list", elements="dict", options=element_spec_rename), |
| 268 |
) |
| 269 |
|
| 270 |
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) |
| 271 |
|
| 272 |
warnings = list() |
| 273 |
messages = list() |
| 274 |
commands_to_execute = list() |
| 275 |
result = {"changed": False} |
| 276 |
|
| 277 |
distribute = module.params["distribute"] |
| 278 |
mode = module.params["mode"] |
| 279 |
da = module.params["da"] |
| 280 |
rename = module.params["rename"] |
| 281 |
|
| 282 |
# Step 0.0: Validate syntax of name and pwwn |
| 283 |
# Also validate syntax of rename arguments |
| 284 |
if da is not None: |
| 285 |
for eachdict in da: |
| 286 |
name = eachdict["name"] |
| 287 |
pwwn = eachdict["pwwn"] |
| 288 |
remove = eachdict["remove"] |
| 289 |
if pwwn is not None: |
| 290 |
pwwn = pwwn.lower() |
| 291 |
if not remove: |
| 292 |
if pwwn is None: |
| 293 |
module.fail_json( |
| 294 |
msg="This device alias name " |
| 295 |
+ str(name) |
| 296 |
+ " which needs to be added, does not have pwwn specified. Please specify a valid pwwn", |
| 297 |
) |
| 298 |
if not isNameValid(name): |
| 299 |
module.fail_json( |
| 300 |
msg="This pwwn name is invalid : " |
| 301 |
+ str(name) |
| 302 |
+ ". Note that name cannot be more than 64 alphanumeric chars, " |
| 303 |
+ "it must start with a letter, and can only contain these characters: " |
| 304 |
+ ", ".join(["'{0}'".format(c) for c in VALID_DA_CHARS]), |
| 305 |
) |
| 306 |
if not isPwwnValid(pwwn): |
| 307 |
module.fail_json( |
| 308 |
msg="This pwwn is invalid : " |
| 309 |
+ str(pwwn) |
| 310 |
+ ". Please check that its a valid pwwn", |
| 311 |
) |
| 312 |
if rename is not None: |
| 313 |
for eachdict in rename: |
| 314 |
oldname = eachdict["old_name"] |
| 315 |
newname = eachdict["new_name"] |
| 316 |
if not isNameValid(oldname): |
| 317 |
module.fail_json( |
| 318 |
msg="This pwwn name is invalid : " |
| 319 |
+ str(oldname) |
| 320 |
+ ". Note that name cannot be more than 64 alphanumeric chars, " |
| 321 |
+ "it must start with a letter, and can only contain these characters: " |
| 322 |
+ ", ".join(["'{0}'".format(c) for c in VALID_DA_CHARS]), |
| 323 |
) |
| 324 |
if not isNameValid(newname): |
| 325 |
module.fail_json( |
| 326 |
msg="This pwwn name is invalid : " |
| 327 |
+ str(newname) |
| 328 |
+ ". Note that name cannot be more than 64 alphanumeric chars, " |
| 329 |
+ "it must start with a letter, and can only contain these characters: " |
| 330 |
+ ", ".join(["'{0}'".format(c) for c in VALID_DA_CHARS]), |
| 331 |
) |
| 332 |
|
| 333 |
# Step 0.1: Check DA status |
| 334 |
shDAStausObj = showDeviceAliasStatus(module) |
| 335 |
d = shDAStausObj.getDistribute() |
| 336 |
m = shDAStausObj.getMode() |
| 337 |
if shDAStausObj.isLocked(): |
| 338 |
module.fail_json( |
| 339 |
msg="device-alias has acquired lock on the switch. Hence cannot procced.", |
| 340 |
) |
| 341 |
|
| 342 |
# Step 1: Process distribute |
| 343 |
commands = [] |
| 344 |
if distribute is not None: |
| 345 |
if distribute: |
| 346 |
# playbook has distribute as True(enabled) |
| 347 |
if d == "disabled": |
| 348 |
# but switch distribute is disabled(false), so set it to |
| 349 |
# true(enabled) |
| 350 |
commands.append("device-alias distribute") |
| 351 |
messages.append( |
| 352 |
"device-alias distribute changed from disabled to enabled", |
| 353 |
) |
| 354 |
else: |
| 355 |
messages.append( |
| 356 |
"device-alias distribute remains unchanged. current distribution mode is enabled", |
| 357 |
) |
| 358 |
else: |
| 359 |
# playbook has distribute as False(disabled) |
| 360 |
if d == "enabled": |
| 361 |
# but switch distribute is enabled(true), so set it to |
| 362 |
# false(disabled) |
| 363 |
commands.append("no device-alias distribute") |
| 364 |
messages.append( |
| 365 |
"device-alias distribute changed from enabled to disabled", |
| 366 |
) |
| 367 |
else: |
| 368 |
messages.append( |
| 369 |
"device-alias distribute remains unchanged. current distribution mode is disabled", |
| 370 |
) |
| 371 |
|
| 372 |
cmds = flatten_list(commands) |
| 373 |
if cmds: |
| 374 |
commands_to_execute = commands_to_execute + cmds |
| 375 |
if module.check_mode: |
| 376 |
# Check mode implemented at the da_add/da_remove stage |
| 377 |
pass |
| 378 |
else: |
| 379 |
result["changed"] = True |
| 380 |
load_config(module, cmds) |
| 381 |
|
| 382 |
# Step 2: Process mode |
| 383 |
commands = [] |
| 384 |
if mode is not None: |
| 385 |
if mode == "basic": |
| 386 |
# playbook has mode as basic |
| 387 |
if m == "enhanced": |
| 388 |
# but switch mode is enhanced, so set it to basic |
| 389 |
commands.append("no device-alias mode enhanced") |
| 390 |
messages.append("device-alias mode changed from enhanced to basic") |
| 391 |
else: |
| 392 |
messages.append( |
| 393 |
"device-alias mode remains unchanged. current mode is basic", |
| 394 |
) |
| 395 |
|
| 396 |
else: |
| 397 |
# playbook has mode as enhanced |
| 398 |
if m == "basic": |
| 399 |
# but switch mode is basic, so set it to enhanced |
| 400 |
commands.append("device-alias mode enhanced") |
| 401 |
messages.append("device-alias mode changed from basic to enhanced") |
| 402 |
else: |
| 403 |
messages.append( |
| 404 |
"device-alias mode remains unchanged. current mode is enhanced", |
| 405 |
) |
| 406 |
|
| 407 |
if commands: |
| 408 |
if distribute: |
| 409 |
commands.append("device-alias commit") |
| 410 |
commands = ["terminal dont-ask"] + commands + ["no terminal dont-ask"] |
| 411 |
else: |
| 412 |
if distribute is None and d == "enabled": |
| 413 |
commands.append("device-alias commit") |
| 414 |
commands = ["terminal dont-ask"] + commands + ["no terminal dont-ask"] |
| 415 |
|
| 416 |
cmds = flatten_list(commands) |
| 417 |
|
| 418 |
if cmds: |
| 419 |
commands_to_execute = commands_to_execute + cmds |
| 420 |
if module.check_mode: |
| 421 |
# Check mode implemented at the end |
| 422 |
pass |
| 423 |
else: |
| 424 |
result["changed"] = True |
| 425 |
load_config(module, cmds) |
| 426 |
|
| 427 |
# Step 3: Process da |
| 428 |
commands = [] |
| 429 |
shDADatabaseObj = showDeviceAliasDatabase(module) |
| 430 |
if da is not None: |
| 431 |
da_remove_list = [] |
| 432 |
da_add_list = [] |
| 433 |
for eachdict in da: |
| 434 |
name = eachdict["name"] |
| 435 |
pwwn = eachdict["pwwn"] |
| 436 |
remove = eachdict["remove"] |
| 437 |
if pwwn is not None: |
| 438 |
pwwn = pwwn.lower() |
| 439 |
if remove: |
| 440 |
if shDADatabaseObj.isNameInDaDatabase(name): |
| 441 |
commands.append("no device-alias name " + name) |
| 442 |
da_remove_list.append(name) |
| 443 |
else: |
| 444 |
messages.append( |
| 445 |
name |
| 446 |
+ " - This device alias name is not in switch device-alias database, hence cannot be removed.", |
| 447 |
) |
| 448 |
else: |
| 449 |
if shDADatabaseObj.isNamePwwnPresentInDatabase(name, pwwn): |
| 450 |
messages.append( |
| 451 |
name |
| 452 |
+ " : " |
| 453 |
+ pwwn |
| 454 |
+ " - This device alias name,pwwn is already in switch device-alias database, hence nothing to configure", |
| 455 |
) |
| 456 |
else: |
| 457 |
if shDADatabaseObj.isNameInDaDatabase(name): |
| 458 |
module.fail_json( |
| 459 |
msg=name |
| 460 |
+ " - This device alias name is already present in switch device-alias database but assigned to another pwwn (" |
| 461 |
+ shDADatabaseObj.getPwwnByName(name) |
| 462 |
+ ") hence cannot be added", |
| 463 |
) |
| 464 |
|
| 465 |
elif shDADatabaseObj.isPwwnInDaDatabase(pwwn): |
| 466 |
module.fail_json( |
| 467 |
msg=pwwn |
| 468 |
+ " - This device alias pwwn is already present in switch device-alias database but assigned to another name (" |
| 469 |
+ shDADatabaseObj.getNameByPwwn(pwwn) |
| 470 |
+ ") hence cannot be added", |
| 471 |
) |
| 472 |
|
| 473 |
else: |
| 474 |
commands.append("device-alias name " + name + " pwwn " + pwwn) |
| 475 |
da_add_list.append(name) |
| 476 |
|
| 477 |
if len(da_add_list) != 0 or len(da_remove_list) != 0: |
| 478 |
commands = ["device-alias database"] + commands |
| 479 |
if distribute: |
| 480 |
commands.append("device-alias commit") |
| 481 |
commands = ["terminal dont-ask"] + commands + ["no terminal dont-ask"] |
| 482 |
else: |
| 483 |
if distribute is None and d == "enabled": |
| 484 |
commands.append("device-alias commit") |
| 485 |
commands = ["terminal dont-ask"] + commands + ["no terminal dont-ask"] |
| 486 |
|
| 487 |
cmds = flatten_list(commands) |
| 488 |
if cmds: |
| 489 |
commands_to_execute = commands_to_execute + cmds |
| 490 |
if module.check_mode: |
| 491 |
# Check mode implemented at the end |
| 492 |
pass |
| 493 |
else: |
| 494 |
result["changed"] = True |
| 495 |
load_config(module, cmds) |
| 496 |
if len(da_remove_list) != 0: |
| 497 |
messages.append( |
| 498 |
"the required device-alias were removed. " + ",".join(da_remove_list), |
| 499 |
) |
| 500 |
if len(da_add_list) != 0: |
| 501 |
messages.append( |
| 502 |
"the required device-alias were added. " + ",".join(da_add_list), |
| 503 |
) |
| 504 |
|
| 505 |
# Step 5: Process rename |
| 506 |
commands = [] |
| 507 |
if rename is not None: |
| 508 |
for eachdict in rename: |
| 509 |
oldname = eachdict["old_name"] |
| 510 |
newname = eachdict["new_name"] |
| 511 |
if shDADatabaseObj.isNameInDaDatabase(newname): |
| 512 |
module.fail_json( |
| 513 |
changed=False, |
| 514 |
commands=cmds, |
| 515 |
msg=newname |
| 516 |
+ " - this name is already present in the device-alias database, hence we cannot rename " |
| 517 |
+ oldname |
| 518 |
+ " with this one", |
| 519 |
) |
| 520 |
if shDADatabaseObj.isNameInDaDatabase(oldname): |
| 521 |
commands.append("device-alias rename " + oldname + " " + newname) |
| 522 |
else: |
| 523 |
module.fail_json( |
| 524 |
changed=False, |
| 525 |
commands=cmds, |
| 526 |
msg=oldname |
| 527 |
+ " - this name is not present in the device-alias database, hence we cannot rename.", |
| 528 |
) |
| 529 |
|
| 530 |
if len(commands) != 0: |
| 531 |
commands = ["device-alias database"] + commands |
| 532 |
if distribute: |
| 533 |
commands.append("device-alias commit") |
| 534 |
commands = ["terminal dont-ask"] + commands + ["no terminal dont-ask"] |
| 535 |
else: |
| 536 |
if distribute is None and d == "enabled": |
| 537 |
commands.append("device-alias commit") |
| 538 |
commands = ["terminal dont-ask"] + commands + ["no terminal dont-ask"] |
| 539 |
cmds = flatten_list(commands) |
| 540 |
if cmds: |
| 541 |
commands_to_execute = commands_to_execute + cmds |
| 542 |
if module.check_mode: |
| 543 |
# Check mode implemented at the end |
| 544 |
pass |
| 545 |
else: |
| 546 |
result["changed"] = True |
| 547 |
load_config(module, cmds) |
| 548 |
|
| 549 |
# Step END: check for 'check' mode |
| 550 |
if module.check_mode: |
| 551 |
module.exit_json( |
| 552 |
changed=False, |
| 553 |
commands=commands_to_execute, |
| 554 |
msg="Check Mode: No cmds issued to the hosts", |
| 555 |
) |
| 556 |
|
| 557 |
result["messages"] = messages |
| 558 |
result["commands"] = commands_to_execute |
| 559 |
result["warnings"] = warnings |
| 560 |
module.exit_json(**result) |
| 561 |
|
| 562 |
|
| 563 |
if __name__ == "__main__": |
| 564 |
main() |