fix nxos_udld issues (#37418)

This commit is contained in:
saichint 2018-03-15 03:07:46 -07:00 committed by Trishna Guha
commit 05b266cc66
2 changed files with 76 additions and 63 deletions

View file

@ -34,8 +34,6 @@ author:
- Jason Edelman (@jedelman8)
notes:
- Tested against NXOSv 7.3.(0)D1(1) on VIRL
- When C(state=absent), it unconfigures existing settings C(msg_time) and set it
to its default value of 15. It is cleaner to always use C(state=present).
- Module will fail if the udld feature has not been previously enabled.
options:
aggressive:
@ -46,18 +44,20 @@ options:
choices: ['enabled','disabled']
msg_time:
description:
- Message time in seconds for UDLD packets.
- Message time in seconds for UDLD packets or keyword 'default'.
required: false
default: null
reset:
description:
- Ability to reset UDLD down interfaces.
- Ability to reset all ports shut down by UDLD. 'state' parameter
cannot be 'absent' when this is present.
required: false
default: null
choices: ['true','false']
choices: ['true']
state:
description:
- Manage the state of the resource.
- Manage the state of the resource. When set to 'absent',
aggressive and msg_time are set to their default values.
required: false
default: present
choices: ['present','absent']
@ -117,6 +117,11 @@ from ansible.module_utils.network.nxos.nxos import get_capabilities, nxos_argume
from ansible.module_utils.basic import AnsibleModule
PARAM_TO_DEFAULT_KEYMAP = {
'msg_time': '15',
}
def execute_show_command(command, module, command_type='cli_show'):
device_info = get_capabilities(module)
network_api = device_info.get('network_api', 'nxapi')
@ -156,42 +161,32 @@ def apply_key_map(key_map, table):
return new_dict
def get_commands_config_udld_global(delta, reset):
config_args = {
'enabled': 'udld aggressive',
'disabled': 'no udld aggressive',
'msg_time': 'udld message-time {msg_time}'
}
def get_commands_config_udld_global(delta, reset, existing):
commands = []
for param, value in delta.items():
if param == 'aggressive':
if value == 'enabled':
command = 'udld aggressive'
elif value == 'disabled':
command = 'no udld aggressive'
else:
command = config_args.get(param, 'DNE').format(**delta)
if command and command != 'DNE':
command = 'udld aggressive' if value == 'enabled' else 'no udld aggressive'
commands.append(command)
command = None
elif param == 'msg_time':
if value == 'default':
if existing.get('msg_time') != PARAM_TO_DEFAULT_KEYMAP.get('msg_time'):
commands.append('no udld message-time')
else:
commands.append('udld message-time ' + value)
if reset:
command = 'udld reset'
commands.append(command)
return commands
def get_commands_remove_udld_global(delta):
config_args = {
'aggressive': 'no udld aggressive',
'msg_time': 'no udld message-time {msg_time}',
}
def get_commands_remove_udld_global(existing):
commands = []
for param, value in delta.items():
command = config_args.get(param, 'DNE').format(**delta)
if command and command != 'DNE':
commands.append(command)
command = None
if existing.get('aggressive') == 'enabled':
command = 'no udld aggressive'
commands.append(command)
if existing.get('msg_time') != PARAM_TO_DEFAULT_KEYMAP.get('msg_time'):
command = 'no udld message-time'
commands.append(command)
return commands
@ -222,7 +217,6 @@ def main():
argument_spec.update(nxos_argument_spec)
module = AnsibleModule(argument_spec=argument_spec,
required_one_of=[['aggressive', 'msg_time', 'reset']],
supports_check_mode=True)
warnings = list()
@ -232,20 +226,8 @@ def main():
reset = module.params['reset']
state = module.params['state']
if (aggressive or reset) and state == 'absent':
module.fail_json(msg="It's better to use state=present when "
"configuring or unconfiguring aggressive mode "
"or using reset flag. state=absent is just for "
"when using msg_time param.")
if msg_time:
try:
msg_time_int = int(msg_time)
if msg_time_int < 7 or msg_time_int > 90:
raise ValueError
except ValueError:
module.fail_json(msg='msg_time must be an integer'
'between 7 and 90')
if reset and state == 'absent':
module.fail_json(msg="state must be present when using reset flag.")
args = dict(aggressive=aggressive, msg_time=msg_time, reset=reset)
proposed = dict((k, v) for k, v in args.items() if v is not None)
@ -259,13 +241,12 @@ def main():
commands = []
if state == 'present':
if delta:
command = get_commands_config_udld_global(dict(delta), reset)
command = get_commands_config_udld_global(dict(delta), reset, existing)
commands.append(command)
elif state == 'absent':
common = set(proposed.items()).intersection(existing.items())
if common:
command = get_commands_remove_udld_global(dict(common))
command = get_commands_remove_udld_global(existing)
if command:
commands.append(command)
cmds = flatten_list(commands)