From 01dda25e0fd11c3f8eefefe089c7478d76f20ce0 Mon Sep 17 00:00:00 2001 From: Nathaniel Case Date: Wed, 9 Aug 2017 09:44:02 -0400 Subject: [PATCH] fix nxapi transport in nxos_snmp_location (#27849) * fix nxapi transport in nxos_snmp_location * Appease Shippable --- .../network/nxos/nxos_snmp_location.py | 87 +++++-------------- test/sanity/pep8/legacy-files.txt | 1 - 2 files changed, 23 insertions(+), 65 deletions(-) diff --git a/lib/ansible/modules/network/nxos/nxos_snmp_location.py b/lib/ansible/modules/network/nxos/nxos_snmp_location.py index 0801dd5c3d0..684719a5f36 100644 --- a/lib/ansible/modules/network/nxos/nxos_snmp_location.py +++ b/lib/ansible/modules/network/nxos/nxos_snmp_location.py @@ -51,67 +51,42 @@ EXAMPLES = ''' - nxos_snmp_location: location: Test state: present - host: "{{ inventory_hostname }}" - username: "{{ un }}" - password: "{{ pwd }}" # ensure snmp location is not configured - nxos_snmp_location: location: Test state: absent - host: "{{ inventory_hostname }}" - username: "{{ un }}" - password: "{{ pwd }}" ''' RETURN = ''' -proposed: - description: k/v pairs of parameters passed into module - returned: always - type: dict - sample: {"location": "New_Test"} -existing: - description: k/v pairs of existing snmp location - returned: always - type: dict - sample: {"location": "Test"} -end_state: - description: k/v pairs of location info after module execution - returned: always - type: dict - sample: {"location": "New_Test"} -updates: +commands: description: command sent to the device returned: always type: list sample: ["snmp-server location New_Test"] -changed: - description: check to see if a change was made on the device - returned: always - type: boolean - sample: true ''' + +import re + from ansible.module_utils.nxos import get_config, load_config, run_commands from ansible.module_utils.nxos import nxos_argument_spec, check_args from ansible.module_utils.basic import AnsibleModule -import re -import re - - def execute_show_command(command, module, command_type='cli_show'): - if module.params['transport'] == 'cli': - if 'show run' not in command: - command += ' | json' - cmds = [command] - body = run_commands(module, cmds) - elif module.params['transport'] == 'nxapi': - cmds = [command] - body = run_commands(module, cmds) + if 'show run' not in command: + cmds = [{ + 'command': command, + 'output': 'json', + }] + else: + cmds = [{ + 'command': command, + 'output': 'text', + }] - return body + return run_commands(module, cmds) def apply_key_map(key_map, table): @@ -119,7 +94,6 @@ 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: @@ -156,25 +130,21 @@ def get_snmp_location(module): def main(): argument_spec = dict( location=dict(required=True, type='str'), - state=dict(choices=['absent', 'present'], - default='present') + state=dict(choices=['absent', 'present'], default='present') ) 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 = {'commands': [], 'changed': False, 'warnings': warnings} location = module.params['location'] state = module.params['state'] existing = get_snmp_location(module) - changed = False commands = [] proposed = dict(location=location) end_state = existing @@ -188,27 +158,16 @@ def main(): cmds = flatten_list(commands) if cmds: - if module.check_mode: - module.exit_json(changed=True, commands=cmds) - else: - changed = True + if not module.check_mode: load_config(module, cmds) - end_state = get_snmp_location(module) - if 'configure' in cmds: - cmds.pop(0) - results = {} - results['proposed'] = proposed - results['existing'] = existing - results['end_state'] = end_state - results['updates'] = cmds - results['changed'] = changed - results['warnings'] = warnings + if 'configure' in cmds: + cmds.pop(0) + results['commands'] = cmds + results['changed'] = True module.exit_json(**results) -from ansible.module_utils.basic import * if __name__ == "__main__": main() - diff --git a/test/sanity/pep8/legacy-files.txt b/test/sanity/pep8/legacy-files.txt index 098b4fa76ea..6753253f5f3 100644 --- a/test/sanity/pep8/legacy-files.txt +++ b/test/sanity/pep8/legacy-files.txt @@ -332,7 +332,6 @@ lib/ansible/modules/network/nxos/nxos_snapshot.py lib/ansible/modules/network/nxos/nxos_snmp_community.py lib/ansible/modules/network/nxos/nxos_snmp_contact.py lib/ansible/modules/network/nxos/nxos_snmp_host.py -lib/ansible/modules/network/nxos/nxos_snmp_location.py lib/ansible/modules/network/nxos/nxos_snmp_traps.py lib/ansible/modules/network/nxos/nxos_snmp_user.py lib/ansible/modules/network/nxos/nxos_static_route.py