From 9e4cdd2fce253ccb080fb7207fca094d6d62785c Mon Sep 17 00:00:00 2001 From: Nathaniel Case Date: Fri, 1 Sep 2017 07:12:49 -0400 Subject: [PATCH] Clean up nxos_snmp_contact & nxos_snmp_location --- .../modules/network/nxos/nxos_snmp_contact.py | 31 ++++------ .../network/nxos/nxos_snmp_location.py | 61 ++++++------------- 2 files changed, 30 insertions(+), 62 deletions(-) diff --git a/lib/ansible/modules/network/nxos/nxos_snmp_contact.py b/lib/ansible/modules/network/nxos/nxos_snmp_contact.py index d6b1cfa1d4..badba043a5 100644 --- a/lib/ansible/modules/network/nxos/nxos_snmp_contact.py +++ b/lib/ansible/modules/network/nxos/nxos_snmp_contact.py @@ -66,7 +66,7 @@ commands: import re -from ansible.module_utils.nxos import get_config, load_config, run_commands +from ansible.module_utils.nxos import load_config, run_commands from ansible.module_utils.nxos import nxos_argument_spec, check_args from ansible.module_utils.basic import AnsibleModule @@ -77,7 +77,7 @@ def execute_show_command(command, module): 'output': 'text', } - return run_commands(module, [command]) + return run_commands(module, command) def flatten_list(command_lists): @@ -92,17 +92,12 @@ def flatten_list(command_lists): def get_snmp_contact(module): contact = {} - contact_regex = '.*snmp-server\scontact\s(?P\S+).*' - command = 'show run snmp' + contact_regex = r'^\s*snmp-server\scontact\s(?P.+)$' - body = execute_show_command(command, module)[0] - - try: - match_contact = re.match(contact_regex, body, re.DOTALL) - group_contact = match_contact.groupdict() - contact['contact'] = group_contact["contact"] - except AttributeError: - contact = {} + body = execute_show_command('show run snmp', module)[0] + match_contact = re.search(contact_regex, body, re.M) + if match_contact: + contact['contact'] = match_contact.group("contact") return contact @@ -110,25 +105,21 @@ def get_snmp_contact(module): def main(): argument_spec = dict( contact=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 = {'changed': False, 'commands': [], 'warnings': warnings} - contact = module.params['contact'] state = module.params['state'] existing = get_snmp_contact(module) - proposed = dict(contact=contact) commands = [] if state == 'absent': @@ -140,11 +131,12 @@ def main(): cmds = flatten_list(commands) if cmds: + results['changed'] = True if not module.check_mode: load_config(module, cmds) + if 'configure' in cmds: cmds.pop(0) - results['changed'] = True results['commands'] = cmds module.exit_json(**results) @@ -152,4 +144,3 @@ def main(): if __name__ == '__main__': main() - diff --git a/lib/ansible/modules/network/nxos/nxos_snmp_location.py b/lib/ansible/modules/network/nxos/nxos_snmp_location.py index bd728c4f7b..52604148e0 100644 --- a/lib/ansible/modules/network/nxos/nxos_snmp_location.py +++ b/lib/ansible/modules/network/nxos/nxos_snmp_location.py @@ -16,7 +16,6 @@ # along with Ansible. If not, see . # - ANSIBLE_METADATA = {'metadata_version': '1.1', 'status': ['preview'], 'supported_by': 'network'} @@ -33,6 +32,8 @@ description: author: - Jason Edelman (@jedelman8) - Gabriele Gerbino (@GGabriele) +notes: + - Tested against NXOSv 7.3.(0)D1(1) on VIRL options: location: description: @@ -60,7 +61,7 @@ EXAMPLES = ''' RETURN = ''' commands: - description: command sent to the device + description: commands sent to the device returned: always type: list sample: ["snmp-server location New_Test"] @@ -69,36 +70,18 @@ commands: import re -from ansible.module_utils.nxos import get_config, load_config, run_commands +from ansible.module_utils.nxos import load_config, run_commands from ansible.module_utils.nxos import nxos_argument_spec, check_args from ansible.module_utils.basic import AnsibleModule -def execute_show_command(command, module, command_type='cli_show'): - if 'show run' not in command: - cmds = [{ - 'command': command, - 'output': 'json', - }] - else: - cmds = [{ - 'command': command, - 'output': 'text', - }] +def execute_show_command(command, module): + command = { + 'command': command, + 'output': 'text', + } - return run_commands(module, cmds) - - -def apply_key_map(key_map, table): - new_dict = {} - for key, value in table.items(): - new_key = key_map.get(key) - if new_key: - if value: - new_dict[new_key] = str(value) - else: - new_dict[new_key] = value - return new_dict + return run_commands(module, command) def flatten_list(command_lists): @@ -113,16 +96,12 @@ def flatten_list(command_lists): def get_snmp_location(module): location = {} - location_regex = '.*snmp-server\slocation\s(?P\S+).*' - command = 'show run snmp' + location_regex = r'^\s*snmp-server\slocation\s(?P.+)$' - body = execute_show_command(command, module, command_type='cli_show_ascii') - try: - match_location = re.match(location_regex, body[0], re.DOTALL) - group_location = match_location.groupdict() - location['location'] = group_location["location"] - except (AttributeError, TypeError): - location = {} + body = execute_show_command('show run snmp', module)[0] + match_location = re.search(location_regex, body, re.M) + if match_location: + location['location'] = match_location.group("location") return location @@ -130,7 +109,7 @@ 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) @@ -139,15 +118,13 @@ def main(): warnings = list() check_args(module, warnings) - results = {'commands': [], 'changed': False, 'warnings': warnings} + results = {'changed': False, 'commands': [], 'warnings': warnings} location = module.params['location'] state = module.params['state'] existing = get_snmp_location(module) commands = [] - proposed = dict(location=location) - end_state = existing if state == 'absent': if existing and existing['location'] == location: @@ -158,16 +135,16 @@ def main(): cmds = flatten_list(commands) if cmds: + results['changed'] = True if not module.check_mode: load_config(module, cmds) if 'configure' in cmds: cmds.pop(0) results['commands'] = cmds - results['changed'] = True module.exit_json(**results) -if __name__ == "__main__": +if __name__ == '__main__': main()