fix nxos_udld issues (#37418)

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

View file

@ -34,8 +34,6 @@ author:
- Jason Edelman (@jedelman8) - Jason Edelman (@jedelman8)
notes: notes:
- Tested against NXOSv 7.3.(0)D1(1) on VIRL - 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. - Module will fail if the udld feature has not been previously enabled.
options: options:
aggressive: aggressive:
@ -46,18 +44,20 @@ options:
choices: ['enabled','disabled'] choices: ['enabled','disabled']
msg_time: msg_time:
description: description:
- Message time in seconds for UDLD packets. - Message time in seconds for UDLD packets or keyword 'default'.
required: false required: false
default: null default: null
reset: reset:
description: 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 required: false
default: null default: null
choices: ['true','false'] choices: ['true']
state: state:
description: 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 required: false
default: present default: present
choices: ['present','absent'] 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 from ansible.module_utils.basic import AnsibleModule
PARAM_TO_DEFAULT_KEYMAP = {
'msg_time': '15',
}
def execute_show_command(command, module, command_type='cli_show'): def execute_show_command(command, module, command_type='cli_show'):
device_info = get_capabilities(module) device_info = get_capabilities(module)
network_api = device_info.get('network_api', 'nxapi') network_api = device_info.get('network_api', 'nxapi')
@ -156,42 +161,32 @@ def apply_key_map(key_map, table):
return new_dict return new_dict
def get_commands_config_udld_global(delta, reset): def get_commands_config_udld_global(delta, reset, existing):
config_args = {
'enabled': 'udld aggressive',
'disabled': 'no udld aggressive',
'msg_time': 'udld message-time {msg_time}'
}
commands = [] commands = []
for param, value in delta.items(): for param, value in delta.items():
if param == 'aggressive': if param == 'aggressive':
if value == 'enabled': command = 'udld aggressive' if value == 'enabled' else 'no udld aggressive'
command = 'udld aggressive'
elif value == 'disabled':
command = 'no udld aggressive'
else:
command = config_args.get(param, 'DNE').format(**delta)
if command and command != 'DNE':
commands.append(command) 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: if reset:
command = 'udld reset' command = 'udld reset'
commands.append(command) commands.append(command)
return commands return commands
def get_commands_remove_udld_global(delta): def get_commands_remove_udld_global(existing):
config_args = {
'aggressive': 'no udld aggressive',
'msg_time': 'no udld message-time {msg_time}',
}
commands = [] commands = []
for param, value in delta.items(): if existing.get('aggressive') == 'enabled':
command = config_args.get(param, 'DNE').format(**delta) command = 'no udld aggressive'
if command and command != 'DNE': commands.append(command)
commands.append(command) if existing.get('msg_time') != PARAM_TO_DEFAULT_KEYMAP.get('msg_time'):
command = None command = 'no udld message-time'
commands.append(command)
return commands return commands
@ -222,7 +217,6 @@ def main():
argument_spec.update(nxos_argument_spec) argument_spec.update(nxos_argument_spec)
module = AnsibleModule(argument_spec=argument_spec, module = AnsibleModule(argument_spec=argument_spec,
required_one_of=[['aggressive', 'msg_time', 'reset']],
supports_check_mode=True) supports_check_mode=True)
warnings = list() warnings = list()
@ -232,20 +226,8 @@ def main():
reset = module.params['reset'] reset = module.params['reset']
state = module.params['state'] state = module.params['state']
if (aggressive or reset) and state == 'absent': if reset and state == 'absent':
module.fail_json(msg="It's better to use state=present when " module.fail_json(msg="state must be present when using reset flag.")
"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')
args = dict(aggressive=aggressive, msg_time=msg_time, reset=reset) 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) proposed = dict((k, v) for k, v in args.items() if v is not None)
@ -259,13 +241,12 @@ def main():
commands = [] commands = []
if state == 'present': if state == 'present':
if delta: if delta:
command = get_commands_config_udld_global(dict(delta), reset) command = get_commands_config_udld_global(dict(delta), reset, existing)
commands.append(command) commands.append(command)
elif state == 'absent': elif state == 'absent':
common = set(proposed.items()).intersection(existing.items()) command = get_commands_remove_udld_global(existing)
if common: if command:
command = get_commands_remove_udld_global(dict(common))
commands.append(command) commands.append(command)
cmds = flatten_list(commands) cmds = flatten_list(commands)

View file

@ -5,7 +5,7 @@
- set_fact: udld_run="true" - set_fact: udld_run="true"
- set_fact: udld_run="false" - set_fact: udld_run="false"
when: (( platform is search('N9K-F')) and (imagetag and ( imagetag is version('F3', 'lt')))) when: ((platform is search('N9K-F')) and (imagetag and (imagetag is version_compare('F3', 'lt'))))
- set_fact: udld_run="false" - set_fact: udld_run="false"
when: titanium when: titanium
@ -16,14 +16,9 @@
state: enabled state: enabled
provider: "{{ connection }}" provider: "{{ connection }}"
- name: Reset udld - name: Configure udld
nxos_udld:
reset: True
provider: "{{ connection }}"
- name: Ensure udld agg mode is globally disabled and msg time is 20
nxos_udld: &conf1 nxos_udld: &conf1
aggressive: disabled aggressive: enabled
msg_time: 20 msg_time: 20
provider: "{{ connection }}" provider: "{{ connection }}"
register: result register: result
@ -32,7 +27,7 @@
that: that:
- "result.changed == true" - "result.changed == true"
- name: "Conf1 Idempotence" - name: "Check Idempotence"
nxos_udld: *conf1 nxos_udld: *conf1
register: result register: result
@ -40,22 +35,59 @@
that: that:
- "result.changed == false" - "result.changed == false"
- name: Reset udld
nxos_udld:
reset: True
provider: "{{ connection }}"
- name: Ensure udld agg mode is globally enabled and msg time is 15 - name: Configure udld2
nxos_udld: &conf2 nxos_udld: &conf2
aggressive: enabled aggressive: disabled
msg_time: 15
provider: "{{ connection }}" provider: "{{ connection }}"
register: result register: result
- assert: *true - assert: *true
- name: "conf2 Idempotence" - name: "Check Idempotence"
nxos_udld: *conf2 nxos_udld: *conf2
register: result register: result
- assert: *false - assert: *false
- name: Configure udld3
nxos_udld: &conf3
msg_time: default
provider: "{{ connection }}"
register: result
- assert: *true
- name: "Check Idempotence"
nxos_udld: *conf3
register: result
- assert: *false
- name: Configure udld again
nxos_udld: *conf1
register: result
- assert: *true
- name: Remove udld config
nxos_udld: &conf4
state: absent
provider: "{{ connection }}"
register: result
- assert: *true
- name: "Check Idempotence"
nxos_udld: *conf4
register: result
- assert: *false
when: udld_run when: udld_run
always: always: