|
|
@ -115,10 +115,6 @@ from ansible.module_utils.nxos import nxos_argument_spec, check_args
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def execute_show_command(command, module, command_type='cli_show'):
|
|
|
|
def execute_show_command(command, module, command_type='cli_show'):
|
|
|
|
if module.params['transport'] == 'cli':
|
|
|
|
if module.params['transport'] == 'cli':
|
|
|
|
if 'show run' not in command:
|
|
|
|
if 'show run' not in command:
|
|
|
@ -167,50 +163,33 @@ def get_udld_interface(module, interface):
|
|
|
|
return interface_udld
|
|
|
|
return interface_udld
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def is_interface_copper(module, interface):
|
|
|
|
def get_commands_config_udld_interface1(delta, interface, module, existing):
|
|
|
|
command = 'show interface status'
|
|
|
|
commands = []
|
|
|
|
copper = []
|
|
|
|
if delta:
|
|
|
|
try:
|
|
|
|
mode = delta['mode']
|
|
|
|
body = execute_show_command(command, module)[0]
|
|
|
|
if mode == 'aggressive':
|
|
|
|
table = body['TABLE_interface']['ROW_interface']
|
|
|
|
command = 'udld aggressive'
|
|
|
|
for each in table:
|
|
|
|
if mode == 'enabled':
|
|
|
|
itype = each.get('type', 'DNE')
|
|
|
|
command = 'no udld aggressive ; udld enable'
|
|
|
|
if 'CU' in itype or '1000' in itype or '10GBaseT' in itype:
|
|
|
|
elif mode == 'disabled':
|
|
|
|
copper.append(str(each['interface'].lower()))
|
|
|
|
command = 'no udld aggressive ; no udld enable'
|
|
|
|
except (KeyError, AttributeError):
|
|
|
|
if command:
|
|
|
|
pass
|
|
|
|
commands.append(command)
|
|
|
|
|
|
|
|
commands.insert(0, 'interface {0}'.format(interface))
|
|
|
|
if interface in copper:
|
|
|
|
|
|
|
|
found = True
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
found = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return found
|
|
|
|
return commands
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_commands_config_udld_interface(delta, interface, module, existing):
|
|
|
|
def get_commands_config_udld_interface2(delta, interface, module, existing):
|
|
|
|
commands = []
|
|
|
|
commands = []
|
|
|
|
copper = is_interface_copper(module, interface)
|
|
|
|
|
|
|
|
if delta:
|
|
|
|
if delta:
|
|
|
|
mode = delta['mode']
|
|
|
|
mode = delta['mode']
|
|
|
|
if mode == 'aggressive':
|
|
|
|
if mode == 'aggressive':
|
|
|
|
command = 'udld aggressive'
|
|
|
|
command = 'udld aggressive'
|
|
|
|
elif copper:
|
|
|
|
if mode == 'enabled':
|
|
|
|
if mode == 'enabled':
|
|
|
|
command = 'no udld aggressive ; no udld disable'
|
|
|
|
if existing['mode'] == 'aggressive':
|
|
|
|
elif mode == 'disabled':
|
|
|
|
command = 'no udld aggressive ; udld enable'
|
|
|
|
command = 'no udld aggressive ; udld disable'
|
|
|
|
else:
|
|
|
|
|
|
|
|
command = 'udld enable'
|
|
|
|
|
|
|
|
elif mode == 'disabled':
|
|
|
|
|
|
|
|
command = 'no udld enable'
|
|
|
|
|
|
|
|
elif not copper:
|
|
|
|
|
|
|
|
if mode == 'enabled':
|
|
|
|
|
|
|
|
if existing['mode'] == 'aggressive':
|
|
|
|
|
|
|
|
command = 'no udld aggressive ; no udld disable'
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
command = 'no udld disable'
|
|
|
|
|
|
|
|
elif mode == 'disabled':
|
|
|
|
|
|
|
|
command = 'udld disable'
|
|
|
|
|
|
|
|
if command:
|
|
|
|
if command:
|
|
|
|
commands.append(command)
|
|
|
|
commands.append(command)
|
|
|
|
commands.insert(0, 'interface {0}'.format(interface))
|
|
|
|
commands.insert(0, 'interface {0}'.format(interface))
|
|
|
@ -218,24 +197,35 @@ def get_commands_config_udld_interface(delta, interface, module, existing):
|
|
|
|
return commands
|
|
|
|
return commands
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_commands_remove_udld_interface(delta, interface, module, existing):
|
|
|
|
def get_commands_remove_udld_interface1(delta, interface, module, existing):
|
|
|
|
commands = []
|
|
|
|
commands = []
|
|
|
|
copper = is_interface_copper(module, interface)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if delta:
|
|
|
|
if delta:
|
|
|
|
mode = delta['mode']
|
|
|
|
mode = delta['mode']
|
|
|
|
if mode == 'aggressive':
|
|
|
|
if mode == 'aggressive':
|
|
|
|
command = 'no udld aggressive'
|
|
|
|
command = 'no udld aggressive'
|
|
|
|
elif copper:
|
|
|
|
if mode == 'enabled':
|
|
|
|
if mode == 'enabled':
|
|
|
|
command = 'no udld enable'
|
|
|
|
command = 'no udld enable'
|
|
|
|
elif mode == 'disabled':
|
|
|
|
elif mode == 'disabled':
|
|
|
|
command = 'udld enable'
|
|
|
|
command = 'udld enable'
|
|
|
|
if command:
|
|
|
|
elif not copper:
|
|
|
|
commands.append(command)
|
|
|
|
if mode == 'enabled':
|
|
|
|
commands.insert(0, 'interface {0}'.format(interface))
|
|
|
|
command = 'udld disable'
|
|
|
|
|
|
|
|
elif mode == 'disabled':
|
|
|
|
return commands
|
|
|
|
command = 'no udld disable'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_commands_remove_udld_interface2(delta, interface, module, existing):
|
|
|
|
|
|
|
|
commands = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if delta:
|
|
|
|
|
|
|
|
mode = delta['mode']
|
|
|
|
|
|
|
|
if mode == 'aggressive':
|
|
|
|
|
|
|
|
command = 'no udld aggressive'
|
|
|
|
|
|
|
|
if mode == 'enabled':
|
|
|
|
|
|
|
|
command = 'udld disable'
|
|
|
|
|
|
|
|
elif mode == 'disabled':
|
|
|
|
|
|
|
|
command = 'no udld disable'
|
|
|
|
if command:
|
|
|
|
if command:
|
|
|
|
commands.append(command)
|
|
|
|
commands.append(command)
|
|
|
|
commands.insert(0, 'interface {0}'.format(interface))
|
|
|
|
commands.insert(0, 'interface {0}'.format(interface))
|
|
|
@ -259,7 +249,6 @@ def main():
|
|
|
|
warnings = list()
|
|
|
|
warnings = list()
|
|
|
|
check_args(module, warnings)
|
|
|
|
check_args(module, warnings)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
interface = module.params['interface'].lower()
|
|
|
|
interface = module.params['interface'].lower()
|
|
|
|
mode = module.params['mode']
|
|
|
|
mode = module.params['mode']
|
|
|
|
state = module.params['state']
|
|
|
|
state = module.params['state']
|
|
|
@ -274,13 +263,13 @@ def main():
|
|
|
|
commands = []
|
|
|
|
commands = []
|
|
|
|
if state == 'present':
|
|
|
|
if state == 'present':
|
|
|
|
if delta:
|
|
|
|
if delta:
|
|
|
|
command = get_commands_config_udld_interface(delta, interface,
|
|
|
|
command = get_commands_config_udld_interface1(delta, interface,
|
|
|
|
module, existing)
|
|
|
|
module, existing)
|
|
|
|
commands.append(command)
|
|
|
|
commands.append(command)
|
|
|
|
elif state == 'absent':
|
|
|
|
elif state == 'absent':
|
|
|
|
common = set(proposed.items()).intersection(existing.items())
|
|
|
|
common = set(proposed.items()).intersection(existing.items())
|
|
|
|
if common:
|
|
|
|
if common:
|
|
|
|
command = get_commands_remove_udld_interface(
|
|
|
|
command = get_commands_remove_udld_interface1(
|
|
|
|
dict(common), interface, module, existing
|
|
|
|
dict(common), interface, module, existing
|
|
|
|
)
|
|
|
|
)
|
|
|
|
commands.append(command)
|
|
|
|
commands.append(command)
|
|
|
@ -291,7 +280,32 @@ def main():
|
|
|
|
module.exit_json(changed=True, commands=cmds)
|
|
|
|
module.exit_json(changed=True, commands=cmds)
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
changed = True
|
|
|
|
changed = True
|
|
|
|
load_config(module, cmds)
|
|
|
|
# set the return_error to True for load_config
|
|
|
|
|
|
|
|
msgs = load_config(module, cmds, True)
|
|
|
|
|
|
|
|
# since there are multiple commands sent simultaneously
|
|
|
|
|
|
|
|
# the output will have one error code for each command.
|
|
|
|
|
|
|
|
# For commands which are successful, it is empty
|
|
|
|
|
|
|
|
for item in msgs:
|
|
|
|
|
|
|
|
if item:
|
|
|
|
|
|
|
|
err_str = ''
|
|
|
|
|
|
|
|
if isinstance(item, list) and item['msg']:
|
|
|
|
|
|
|
|
err_str = item['msg']
|
|
|
|
|
|
|
|
elif isinstance(item, str):
|
|
|
|
|
|
|
|
err_str = item
|
|
|
|
|
|
|
|
if 'rejecting a config that is valid only for' in err_str:
|
|
|
|
|
|
|
|
commands = []
|
|
|
|
|
|
|
|
if state == 'present':
|
|
|
|
|
|
|
|
command = get_commands_config_udld_interface2(delta, interface,
|
|
|
|
|
|
|
|
module, existing)
|
|
|
|
|
|
|
|
elif state == 'absent':
|
|
|
|
|
|
|
|
command = get_commands_remove_udld_interface2(
|
|
|
|
|
|
|
|
dict(common), interface, module, existing
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
commands.append(command)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cmds = flatten_list(commands)
|
|
|
|
|
|
|
|
load_config(module, cmds)
|
|
|
|
|
|
|
|
|
|
|
|
end_state = get_udld_interface(module, interface)
|
|
|
|
end_state = get_udld_interface(module, interface)
|
|
|
|
if 'configure' in cmds:
|
|
|
|
if 'configure' in cmds:
|
|
|
|
cmds.pop(0)
|
|
|
|
cmds.pop(0)
|
|
|
@ -306,6 +320,7 @@ def main():
|
|
|
|
|
|
|
|
|
|
|
|
module.exit_json(**results)
|
|
|
|
module.exit_json(**results)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|
|
|
|
main()
|
|
|
|
|
|
|
|
|
|
|
|