Repository: ansible/ansible

Issue:
(P1)nxos_aaa_server: fails when using transport as nxapi
<!---
Verify first that your issue/request is not already reported on GitHub.
Also test if the latest release, and master branch are affected too.
-->

##### ISSUE TYPE
<!--- Pick one below and delete the rest: -->
 - Bug Report

##### COMPONENT NAME
<!--- Name of the module/plugin/task/feature -->
- nxos_aaa_server
##### ANSIBLE VERSION
<!--- Paste verbatim output from “ansible --version” between quotes below -->
```
ansible 2.4.0 (devel 9649195c26) last updated 2017/07/24 12:53:01 (GMT -400)
  config file = None
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  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
<!---
Mention the OS you are running Ansible from, and the OS you are
managing, or say “N/A” for anything that is not platform-specific.
Also mention the specific version of what you are trying to control,
e.g. if this is a network bug the version of firmware on the network device.
-->
- Ubuntu 14.04

##### SUMMARY
<!--- Explain the problem briefly -->
Traceback seen while applying simple playbook and using `nxapi` as transport.

##### STEPS TO REPRODUCE
<!---
For bugs, show exactly how to reproduce the problem, using a minimal test-case.
For new features, show how the feature would be used.
-->

<!--- Paste example playbooks or commands between quotes below -->
```yaml
---
cli:
  host: "{{ inventory_hostname }}"
  username: "{{ nxos_cli_user | default('admin') }}"
  password: "{{ nxos_cli_pass | default('admin') }}"

nxapi:
  host: "{{ inventory_hostname }}"
  username: "{{ nxos_nxapi_user | default('admin') }}"
  password: "{{ nxos_nxapi_pass | default('admin') }}"
  transport: nxapi
  user_ssl: no

---
- hosts: nxos
  connection: local
  gather_facts: no

  tasks:
  - name: "Radius Server Basic settings"
    nxos_aaa_server:
      server_type: radius
      server_timeout: 9
      deadtime: 20
      directed_request: enabled
      provider: "{{ nxapi }}"
```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
Playbook to apply successfully. The cli transport works fine.

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->
```
The full traceback is:
Traceback (most recent call last):
  File "/tmp/ansible_eVPvvr/ansible_module_nxos_aaa_server.py", line 383, in <module>
    main()
  File "/tmp/ansible_eVPvvr/ansible_module_nxos_aaa_server.py", line 322, in main
    existing = get_aaa_server_info(server_type, module) 
  File "/tmp/ansible_eVPvvr/ansible_module_nxos_aaa_server.py", line 197, in get_aaa_server_info
    split_server = server_body.splitlines()  
AttributeError: 'dict' object has no attribute 'splitlines'
FAILED! => {
    "changed": false, 
    "failed": true, 
    "module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_eVPvvr/ansible_module_nxos_aaa_server.py\", line 383, in <module>\n    main()\n  File \"/tmp/ansible_eVPvvr/ansible_module_nxos_aaa_server.py\", line 322, in main\n    existing = get_aaa_server_info(server_type, module)\n  File \"/tmp/ansible_eVPvvr/ansible_module_nxos_aaa_server.py\", line 197, in get_aaa_server_info\n    split_server = server_body.splitlines()\nAttributeError: 'dict' object has no attribute 'splitlines'\n", 
    "module_stdout": "", 
    "msg": "MODULE FAILURE", 
    "rc": 0

```


Discussion:
cc @GGabriele @jedelman8 @mikewiebe @privateip @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_aaa_server.py b/lib/ansible/modules/network/nxos/nxos_aaa_server.py
--- a/lib/ansible/modules/network/nxos/nxos_aaa_server.py
+++ b/lib/ansible/modules/network/nxos/nxos_aaa_server.py
@@ -95,9 +95,6 @@
         server_timeout: 9
         deadtime: 20
         directed_request: enabled
-        host:  inventory_hostname }}
-        username:  un }}
-        password:  pwd }}
 
 # Tacacs Server Basic settings
   - name: "Tacacs Server Basic settings"
@@ -106,56 +103,21 @@
         server_timeout: 8
         deadtime: 19
         directed_request: disabled
-        host:  inventory_hostname }}
-        username:  un }}
-        password:  pwd }}
 
 # Setting Global Key
   - name: "AAA Server Global Key"
     nxos_aaa_server:
         server_type: radius
         global_key: test_key
-        host:  inventory_hostname }}
-        username:  un }}
-        password:  pwd }}
 '''
 
 RETURN = '''
-proposed:
-    description: k/v pairs of parameters passed into module
-    returned: always
-    type: dict
-    sample: {"deadtime": "22", "directed_request": "enabled",
-            "server_type": "radius", "server_timeout": "11"}
-existing:
-    description:
-        - k/v pairs of existing aaa server
-    returned: always
-    type: dict
-    sample: {"deadtime": "0", "directed_request": "disabled",
-            "global_key": "unknown", "server_timeout": "5"}
-end_state:
-    description: k/v pairs of aaa params after module execution
-    returned: always
-    type: dict
-    sample: {"deadtime": "22", "directed_request": "enabled",
-            "global_key": "unknown", "server_timeout": "11"}
-state:
-    description: state as sent in from the playbook
-    returned: always
-    type: string
-    sample: "present"
-updates:
+commands:
     description: command sent to the device
     returned: always
     type: list
     sample: ["radius-server deadtime 22", "radius-server timeout 11",
              "radius-server directed-request"]
-changed:
-    description: check to see if a change was made on the device
-    returned: always
-    type: boolean
-    sample: true
 '''
 import re
 
@@ -165,12 +127,12 @@
 
 
 def execute_show_command(command, module, command_type='cli_show'):
-    cmds = [command]
-    if module.params['transport'] == 'cli':
-        body = run_commands(module, cmds)
-    elif module.params['transport'] == 'nxapi':
-        body = run_commands(module, cmds)
-    return body
+    command = {
+        'command': command,
+        'output': 'text',
+    }
+
+    return run_commands(module, command)
 
 
 def flatten_list(command_lists):
@@ -183,7 +145,6 @@ def flatten_list(command_lists):
     return flat_command_list
 
 
-
 def get_aaa_server_info(server_type, module):
     aaa_server_info = {}
     server_command = 'show {0}-server'.format(server_type)
@@ -281,25 +242,22 @@ def default_aaa_server(existing, params, server_type):
 
 def main():
     argument_spec = dict(
-        server_type=dict(type='str',
-                             choices=['radius', 'tacacs'], required=True),
+        server_type=dict(type='str', choices=['radius', 'tacacs'], required=True),
         global_key=dict(type='str'),
         encrypt_type=dict(type='str', choices=['0', '7']),
         deadtime=dict(type='str'),
         server_timeout=dict(type='str'),
-        directed_request=dict(type='str',
-                                  choices=['enabled', 'disabled', 'default']),
+        directed_request=dict(type='str', choices=['enabled', 'disabled', 'default']),
         state=dict(choices=['default', '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}
 
     server_type = module.params['server_type']
     global_key = module.params['global_key']
@@ -316,11 +274,9 @@ def main():
                 encrypt_type=encrypt_type, deadtime=deadtime,
                 server_timeout=server_timeout, directed_request=directed_request)
 
-    changed = False
     proposed = dict((k, v) for k, v in args.items() if v is not None)
 
     existing = get_aaa_server_info(server_type, module)
-    end_state = existing
 
     commands = []
     if state == 'present':
@@ -359,26 +315,15 @@ def main():
 
     cmds = flatten_list(commands)
     if cmds:
-        if module.check_mode:
-            module.exit_json(changed=True, commands=cmds)
-        else:
-            changed = True
+        results['changed'] = True
+        if not module.check_mode:
             load_config(module, cmds)
-            end_state = get_aaa_server_info(server_type, module)
-            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
+        if 'configure' in cmds:
+            cmds.pop(0)
+        results['commands'] = cmds
 
     module.exit_json(**results)
 
 
 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
@@ -299,7 +299,6 @@ lib/ansible/modules/network/netvisor/pn_vrouterif.py
 lib/ansible/modules/network/netvisor/pn_vrouterlbif.py
 lib/ansible/modules/net_tools/nmcli.py
 lib/ansible/modules/network/nxos/_nxos_mtu.py
-lib/ansible/modules/network/nxos/nxos_aaa_server.py
 lib/ansible/modules/network/nxos/nxos_aaa_server_host.py
 lib/ansible/modules/network/nxos/nxos_command.py
 lib/ansible/modules/network/nxos/nxos_config.py