Repository: ansible/ansible

Issue:
(P1) nxos_snmp_location does work properly for transport nxapi
##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
 - nxos_snmp_location

##### NXOS VERSION
7.0(3)I5(3)

##### ANSIBLE VERSION
```
ansible 2.4.0 (devel 661d81ef86) last updated 2017/08/03 12:33:15 (GMT -400)
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/usr/share/ansible']
  ansible python module location = /root/agents-ci/ansible/lib/ansible
  executable location = /root/agents-ci/ansible/bin/ansible
  python version = 2.7.6 (default, Oct 26 2016, 20:30:19) [GCC 4.8.4]
```

##### OS / ENVIRONMENT
Ubuntu 14.04

##### SUMMARY
The following playbook demonstrates the problem:

**NOTE:** Test passes for transport `cli`.

```yaml
---
- debug: msg="START TRANSPORT:{{ connection.transport }} nxos_snmp_location sanity test"

- name: Setup - Remove snmp_location if configured
  nxos_snmp_location: &remove
    location: Test
    state: absent
    provider: "{{ connection }}"
  ignore_errors: yes

- block:

  - name: Configure snmp host
    nxos_snmp_location: &config
      location: Test
      state: present
      provider: "{{ connection }}"
    register: result

  - assert: &true
      that:
        - "result.changed == true"

  - name: Idempotence Check
    nxos_snmp_location: *config
    register: result

  - assert: &false
      that:
        - "result.changed == false"

  always:
  - name: Cleanup
    nxos_snmp_location: *remove
    register: result

  - assert: *true

  - name: Cleanup Idempotence
    nxos_snmp_location: *remove
    register: result

  - assert: *false

  - debug: msg="END TRANSPORT:{{ connection.transport }} nxos_snmp_location sanity test"
```

```shell
TASK [nxos_snmp_location : debug] ***********************************************************************************************************************************************************************************************************************
ok: [n9k.example.comm] => {
    "msg": "START TRANSPORT:nxapi nxos_snmp_location sanity test"
}

TASK [nxos_snmp_location : Setup - Remove snmp_location if configured] **********************************************************************************************************************************************************************************
fatal: [n9k.example.comm]: FAILED! => {"changed": false, "failed": true, "msg": "Connection failure: timed out", "status": -1, "url": "http://n9k.example.comm:80/ins"}
...ignoring

TASK [nxos_snmp_location : Configure snmp host] *********************************************************************************************************************************************************************************************************
fatal: [n9k.example.comm]: FAILED! => {"changed": false, "failed": true, "msg": "Connection failure: timed out", "status": -1, "url": "http://n9k.example.comm:80/ins"}

TASK [nxos_snmp_location : Cleanup] *********************************************************************************************************************************************************************************************************************
fatal: [n9k.example.comm]: FAILED! => {"changed": false, "failed": true, "msg": "Connection failure: timed out", "status": -1, "url": "http://n9k.example.comm:80/ins"}
	to retry, use: --limit @/root/agents-ci/ansible/test/integration/nxos.retry

PLAY RECAP **********************************************************************************************************************************************************************************************************************************************
n9k.example.comm          : ok=34   changed=5    unreachable=0    failed=2   
```

If I set the timeout to 60 I don't get the timed out errors, but the configuration is not applied to the device.


Discussion:
cc @GGabriele @jedelman8 @privateip @rahushen @rcarrillocruz @trishnaguha
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->

Resolving patch:
diff --git a/lib/ansible/modules/network/nxos/nxos_snmp_location.py b/lib/ansible/modules/network/nxos/nxos_snmp_location.py
--- a/lib/ansible/modules/network/nxos/nxos_snmp_location.py
+++ b/lib/ansible/modules/network/nxos/nxos_snmp_location.py
@@ -51,67 +51,42 @@
 - 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()
-


Test patch:
diff --git a/test/sanity/pep8/legacy-files.txt b/test/sanity/pep8/legacy-files.txt
--- a/test/sanity/pep8/legacy-files.txt
+++ b/test/sanity/pep8/legacy-files.txt
@@ -334,7 +334,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