| 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_udld |
| 26 |
extends_documentation_fragment: |
| 27 |
- cisco.nxos.nxos |
| 28 |
short_description: Manages UDLD global configuration params. |
| 29 |
description: |
| 30 |
- Manages UDLD global configuration params. |
| 31 |
version_added: 1.0.0 |
| 32 |
author: |
| 33 |
- Jason Edelman (@jedelman8) |
| 34 |
notes: |
| 35 |
- Tested against NXOSv 7.3.(0)D1(1) on VIRL |
| 36 |
- Unsupported for Cisco MDS |
| 37 |
- Module will fail if the udld feature has not been previously enabled. |
| 38 |
options: |
| 39 |
aggressive: |
| 40 |
description: |
| 41 |
- Toggles aggressive mode. |
| 42 |
choices: |
| 43 |
- enabled |
| 44 |
- disabled |
| 45 |
type: str |
| 46 |
msg_time: |
| 47 |
description: |
| 48 |
- Message time in seconds for UDLD packets or keyword 'default'. |
| 49 |
type: str |
| 50 |
reset: |
| 51 |
description: |
| 52 |
- Ability to reset all ports shut down by UDLD. 'state' parameter cannot be 'absent' |
| 53 |
when this is present. |
| 54 |
type: bool |
| 55 |
state: |
| 56 |
description: |
| 57 |
- Manage the state of the resource. When set to 'absent', aggressive and msg_time |
| 58 |
are set to their default values. |
| 59 |
default: present |
| 60 |
choices: |
| 61 |
- present |
| 62 |
- absent |
| 63 |
type: str |
| 64 |
""" |
| 65 |
EXAMPLES = """ |
| 66 |
# ensure udld aggressive mode is globally disabled and se global message interval is 20 |
| 67 |
- cisco.nxos.nxos_udld: |
| 68 |
aggressive: disabled |
| 69 |
msg_time: 20 |
| 70 |
host: '{{ inventory_hostname }}' |
| 71 |
username: '{{ un }}' |
| 72 |
password: '{{ pwd }}' |
| 73 |
|
| 74 |
# Ensure agg mode is globally enabled and msg time is 15 |
| 75 |
- cisco.nxos.nxos_udld: |
| 76 |
aggressive: enabled |
| 77 |
msg_time: 15 |
| 78 |
host: '{{ inventory_hostname }}' |
| 79 |
username: '{{ un }}' |
| 80 |
password: '{{ pwd }}' |
| 81 |
""" |
| 82 |
|
| 83 |
RETURN = """ |
| 84 |
proposed: |
| 85 |
description: k/v pairs of parameters passed into module |
| 86 |
returned: always |
| 87 |
type: dict |
| 88 |
sample: {"aggressive": "enabled", "msg_time": "40"} |
| 89 |
existing: |
| 90 |
description: |
| 91 |
- k/v pairs of existing udld configuration |
| 92 |
returned: always |
| 93 |
type: dict |
| 94 |
sample: {"aggressive": "disabled", "msg_time": "15"} |
| 95 |
end_state: |
| 96 |
description: k/v pairs of udld configuration after module execution |
| 97 |
returned: always |
| 98 |
type: dict |
| 99 |
sample: {"aggressive": "enabled", "msg_time": "40"} |
| 100 |
updates: |
| 101 |
description: command sent to the device |
| 102 |
returned: always |
| 103 |
type: list |
| 104 |
sample: ["udld message-time 40", "udld aggressive"] |
| 105 |
changed: |
| 106 |
description: check to see if a change was made on the device |
| 107 |
returned: always |
| 108 |
type: bool |
| 109 |
sample: true |
| 110 |
""" |
| 111 |
|
| 112 |
from ansible.module_utils.basic import AnsibleModule |
| 113 |
|
| 114 |
from ansible_collections.cisco.nxos.plugins.module_utils.network.nxos.nxos import ( |
| 115 |
load_config, |
| 116 |
run_commands, |
| 117 |
) |
| 118 |
|
| 119 |
|
| 120 |
PARAM_TO_DEFAULT_KEYMAP = {"msg_time": "15"} |
| 121 |
|
| 122 |
|
| 123 |
def flatten_list(command_lists): |
| 124 |
flat_command_list = [] |
| 125 |
for command in command_lists: |
| 126 |
if isinstance(command, list): |
| 127 |
flat_command_list.extend(command) |
| 128 |
else: |
| 129 |
flat_command_list.append(command) |
| 130 |
return flat_command_list |
| 131 |
|
| 132 |
|
| 133 |
def apply_key_map(key_map, table): |
| 134 |
new_dict = {} |
| 135 |
for key, value in table.items(): |
| 136 |
new_key = key_map.get(key) |
| 137 |
if new_key: |
| 138 |
value = table.get(key) |
| 139 |
if value: |
| 140 |
new_dict[new_key] = str(value) |
| 141 |
else: |
| 142 |
new_dict[new_key] = value |
| 143 |
return new_dict |
| 144 |
|
| 145 |
|
| 146 |
def get_commands_config_udld_global(delta, reset, existing): |
| 147 |
commands = [] |
| 148 |
for param, value in delta.items(): |
| 149 |
if param == "aggressive": |
| 150 |
command = "udld aggressive" if value == "enabled" else "no udld aggressive" |
| 151 |
commands.append(command) |
| 152 |
elif param == "msg_time": |
| 153 |
if value == "default": |
| 154 |
if existing.get("msg_time") != PARAM_TO_DEFAULT_KEYMAP.get("msg_time"): |
| 155 |
commands.append("no udld message-time") |
| 156 |
else: |
| 157 |
commands.append("udld message-time " + value) |
| 158 |
if reset: |
| 159 |
command = "udld reset" |
| 160 |
commands.append(command) |
| 161 |
return commands |
| 162 |
|
| 163 |
|
| 164 |
def get_commands_remove_udld_global(existing): |
| 165 |
commands = [] |
| 166 |
if existing.get("aggressive") == "enabled": |
| 167 |
command = "no udld aggressive" |
| 168 |
commands.append(command) |
| 169 |
if existing.get("msg_time") != PARAM_TO_DEFAULT_KEYMAP.get("msg_time"): |
| 170 |
command = "no udld message-time" |
| 171 |
commands.append(command) |
| 172 |
return commands |
| 173 |
|
| 174 |
|
| 175 |
def get_udld_global(module): |
| 176 |
command = "show udld global | json" |
| 177 |
udld_table = run_commands(module, [command])[0] |
| 178 |
|
| 179 |
status = str(udld_table.get("udld-global-mode", None)) |
| 180 |
if status == "enabled-aggressive": |
| 181 |
aggressive = "enabled" |
| 182 |
else: |
| 183 |
aggressive = "disabled" |
| 184 |
|
| 185 |
interval = str(udld_table.get("message-interval", None)) |
| 186 |
udld = dict(msg_time=interval, aggressive=aggressive) |
| 187 |
|
| 188 |
return udld |
| 189 |
|
| 190 |
|
| 191 |
def main(): |
| 192 |
argument_spec = dict( |
| 193 |
aggressive=dict(required=False, choices=["enabled", "disabled"]), |
| 194 |
msg_time=dict(required=False, type="str"), |
| 195 |
reset=dict(required=False, type="bool"), |
| 196 |
state=dict(choices=["absent", "present"], default="present"), |
| 197 |
) |
| 198 |
|
| 199 |
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) |
| 200 |
|
| 201 |
warnings = list() |
| 202 |
|
| 203 |
aggressive = module.params["aggressive"] |
| 204 |
msg_time = module.params["msg_time"] |
| 205 |
reset = module.params["reset"] |
| 206 |
state = module.params["state"] |
| 207 |
|
| 208 |
if reset and state == "absent": |
| 209 |
module.fail_json(msg="state must be present when using reset flag.") |
| 210 |
|
| 211 |
args = dict(aggressive=aggressive, msg_time=msg_time, reset=reset) |
| 212 |
proposed = dict((k, v) for k, v in args.items() if v is not None) |
| 213 |
|
| 214 |
existing = get_udld_global(module) |
| 215 |
end_state = existing |
| 216 |
|
| 217 |
delta = set(proposed.items()).difference(existing.items()) |
| 218 |
changed = False |
| 219 |
|
| 220 |
commands = [] |
| 221 |
if state == "present": |
| 222 |
if delta: |
| 223 |
command = get_commands_config_udld_global(dict(delta), reset, existing) |
| 224 |
commands.append(command) |
| 225 |
|
| 226 |
elif state == "absent": |
| 227 |
command = get_commands_remove_udld_global(existing) |
| 228 |
if command: |
| 229 |
commands.append(command) |
| 230 |
|
| 231 |
cmds = flatten_list(commands) |
| 232 |
if cmds: |
| 233 |
if module.check_mode: |
| 234 |
module.exit_json(changed=True, commands=cmds) |
| 235 |
else: |
| 236 |
changed = True |
| 237 |
load_config(module, cmds) |
| 238 |
end_state = get_udld_global(module) |
| 239 |
if "configure" in cmds: |
| 240 |
cmds.pop(0) |
| 241 |
|
| 242 |
results = {} |
| 243 |
results["proposed"] = proposed |
| 244 |
results["existing"] = existing |
| 245 |
results["end_state"] = end_state |
| 246 |
results["updates"] = cmds |
| 247 |
results["changed"] = changed |
| 248 |
results["warnings"] = warnings |
| 249 |
|
| 250 |
module.exit_json(**results) |
| 251 |
|
| 252 |
|
| 253 |
if __name__ == "__main__": |
| 254 |
main() |