From b93878b685e6194ca9096906fcb5a765f58cb998 Mon Sep 17 00:00:00 2001 From: Nathaniel Case Date: Wed, 2 Aug 2017 08:46:44 -0400 Subject: [PATCH] Minor cleanup to nxos_vrrp (#27593) * Safe changes * Fold `[0]` into `execute_show_command()` We only ever execute one command, so only return the one response * Streamline `results` --- lib/ansible/modules/network/nxos/nxos_vrrp.py | 83 +++++-------------- test/sanity/pep8/legacy-files.txt | 1 - 2 files changed, 21 insertions(+), 63 deletions(-) diff --git a/lib/ansible/modules/network/nxos/nxos_vrrp.py b/lib/ansible/modules/network/nxos/nxos_vrrp.py index 287e9554120..f3c5637a045 100644 --- a/lib/ansible/modules/network/nxos/nxos_vrrp.py +++ b/lib/ansible/modules/network/nxos/nxos_vrrp.py @@ -84,7 +84,6 @@ EXAMPLES = ''' interface: vlan10 group: 100 vip: 10.1.100.1 - host: 68.170.147.165 - name: Ensure removal of the vrrp group config # vip is required to ensure the user knows what they are removing @@ -93,7 +92,6 @@ EXAMPLES = ''' group: 100 vip: 10.1.100.1 state: absent - host: 68.170.147.165 - name: Re-config with more params nxos_vrrp: @@ -103,39 +101,15 @@ EXAMPLES = ''' preempt: false priority: 130 authentication: AUTHKEY - host: 68.170.147.165 ''' RETURN = ''' -proposed: - description: k/v pairs of parameters passed into module - returned: always - type: dict - sample: {"authentication": "testing", "group": "150", "vip": "10.1.15.1", - "admin_state": "no shutdown"} -existing: - description: k/v pairs of existing vrrp info on the interface - returned: always - type: dict - sample: {} -end_state: - description: k/v pairs of vrrp after module execution - returned: always - type: dict - sample: {"authentication": "testing", "group": "150", "interval": "1", - "preempt": true, "priority": "100", "vip": "10.1.15.1", - "admin_state": "no shutdown"} -updates: +commands: description: commands sent to the device returned: always type: list sample: ["interface vlan10", "vrrp 150", "address 10.1.15.1", "authentication text testing", "no shutdown"] -changed: - description: check to see if a change was made on the device - returned: always - type: boolean - sample: true ''' from ansible.module_utils.nxos import load_config, run_commands @@ -148,12 +122,12 @@ def execute_show_command(command, module): output = 'json' else: output = 'text' - cmds = [{ + + commands = [{ 'command': command, 'output': output, }] - body = run_commands(module, cmds) - return body + return run_commands(module, commands)[0] def apply_key_map(key_map, table): @@ -161,13 +135,13 @@ def apply_key_map(key_map, table): for key, value in table.items(): new_key = key_map.get(key) if new_key: - value = table.get(key) if value: new_dict[new_key] = str(value) else: new_dict[new_key] = value return new_dict + def get_interface_type(interface): if interface.upper().startswith('ET'): return 'ethernet' @@ -189,7 +163,7 @@ def is_default(interface, module): command = 'show run interface {0}'.format(interface) try: - body = execute_show_command(command, module)[0] + body = execute_show_command(command, module) if 'invalid' in body.lower(): return 'DNE' else: @@ -206,7 +180,7 @@ def get_interface_mode(interface, intf_type, module): command = 'show interface {0}'.format(interface) interface = {} mode = 'unknown' - body = execute_show_command(command, module)[0] + body = execute_show_command(command, module) interface_table = body['TABLE_interface']['ROW_interface'] name = interface_table.get('interface') @@ -223,7 +197,7 @@ def get_interface_mode(interface, intf_type, module): def get_vrr_status(group, module, interface): command = 'show run all | section interface.{0}$'.format(interface) - body = execute_show_command(command, module)[0] + body = execute_show_command(command, module) vrf_index = None admin_state = 'shutdown' @@ -257,7 +231,7 @@ def get_existing_vrrp(interface, group, module, name): } try: - vrrp_table = body[0]['TABLE_vrrp_group'] + vrrp_table = body['TABLE_vrrp_group'] except (AttributeError, IndexError, TypeError): return {} @@ -355,23 +329,21 @@ def main(): preempt=dict(required=False, type='bool'), vip=dict(required=False, type='str'), admin_state=dict(required=False, type='str', - choices=['shutdown', 'no shutdown'], - default='no shutdown'), + choices=['shutdown', 'no shutdown'], + default='no shutdown'), authentication=dict(required=False, type='str'), - state=dict(choices=['absent', 'present'], - required=False, default='present'), + state=dict(choices=['absent', 'present'], required=False, default='present'), include_defaults=dict(default=False), config=dict(), save=dict(type='bool', default=False) ) - argument_spec.update(nxos_argument_spec) - module = AnsibleModule(argument_spec=argument_spec, - supports_check_mode=True) + module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) warnings = list() check_args(module, warnings) + results = {'changed': False, 'commands': [], 'warnings': warnings} state = module.params['state'] interface = module.params['interface'].lower() @@ -418,32 +390,19 @@ def main(): if delta: command = get_commands_config_vrrp(delta, group) commands.append(command) - elif state == 'absent': if existing: commands.append(['no vrrp {0}'.format(group)]) if commands: commands.insert(0, ['interface {0}'.format(interface)]) - - cmds = flatten_list(commands) - if cmds: - if module.check_mode: - module.exit_json(changed=True, commands=cmds) - else: - load_config(module, cmds) - changed = True - end_state = get_existing_vrrp(interface, group, module, name) - if 'configure' in cmds: - cmds.pop(0) - - results = {} - results['proposed'] = proposed - results['existing'] = existing - results['updates'] = cmds - results['changed'] = changed - results['warnings'] = warnings - results['end_state'] = end_state + commands = flatten_list(commands) + results['commands'] = commands + results['changed'] = True + if not module.check_mode: + load_config(module, commands) + if 'configure' in commands: + commands.pop(0) module.exit_json(**results) diff --git a/test/sanity/pep8/legacy-files.txt b/test/sanity/pep8/legacy-files.txt index 660013c1fb1..73d34379a49 100644 --- a/test/sanity/pep8/legacy-files.txt +++ b/test/sanity/pep8/legacy-files.txt @@ -349,7 +349,6 @@ lib/ansible/modules/network/nxos/nxos_udld_interface.py lib/ansible/modules/network/nxos/nxos_user.py lib/ansible/modules/network/nxos/nxos_vrf.py lib/ansible/modules/network/nxos/nxos_vrf_interface.py -lib/ansible/modules/network/nxos/nxos_vrrp.py lib/ansible/modules/network/nxos/nxos_vtp_domain.py lib/ansible/modules/network/nxos/nxos_vtp_password.py lib/ansible/modules/network/nxos/nxos_vtp_version.py