Repository: ansible/ansible

Issue:
(P2) nxos_reboot errors out for cli and nxapi
##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
 - nxos_reboot

##### NXOS VERSION
7.0(3)I5(3)

##### ANSIBLE VERSION
```
ansible 2.4.0 (devel aa19563388) last updated 2017/08/01 09:45:40 (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
There are two problems here that can be demonstrated with the playbook that follows:

* First Issue
  * Module sends an incorrectly formatted command `terminal-dont-ask` that caused the module to error out.

* Second Issue:
  * With the fix for the terminal dont-ask issue resolved, the module still errors out for nxapi.
```diff
diff --git a/lib/ansible/modules/network/nxos/nxos_reboot.py b/lib/ansible/modules/network/nxos/nxos_reboot.py
index 338c8a0..5112fde 100644
--- a/lib/ansible/modules/network/nxos/nxos_reboot.py
+++ b/lib/ansible/modules/network/nxos/nxos_reboot.py
@@ -66,7 +66,7 @@ from ansible.module_utils.basic import AnsibleModule
 
 def reboot(module):
     cmds = [
-        {'command': 'terminal-dont-ask'},
+        {'command': 'terminal dont-ask'},
         {'command': 'reload', 'output': 'text'}
     ]
     run_commands(module, cmds)
```

```yaml
---
- debug: msg="START TRANSPORT:{{ connection.transport }} nxos_reboot sanity test"

- block:
  - name: Reboot Switch
    nxos_reboot:
      provider: "{{ connection }}"
    ignore_errors: yes

  always:
  - name: Wait for device to come back up
    wait_for:
      port: 22
      state: started
      timeout: 600
      delay: 60
      host: "{{ inventory_hostname }}"

  - debug: msg="END TRANSPORT:{{ connection.transport }} nxos_reboot sanity test"
```

* First Issue logs:
```shell
TASK [nxos_reboot : Reboot Switch] **************************************************************************************************************************************************************************************************************************
task path: /root/agents-ci/ansible/test/integration/targets/nxos_reboot/tests/common/sanity.yaml:5
<n9k.example.com> connection transport is nxapi
Using module_utils file /root/agents-ci/ansible/lib/ansible/module_utils/basic.py
Using module_utils file /root/agents-ci/ansible/lib/ansible/module_utils/nxos.py
Using module_utils file /root/agents-ci/ansible/lib/ansible/module_utils/_text.py
Using module_utils file /root/agents-ci/ansible/lib/ansible/module_utils/parsing/convert_bool.py
Using module_utils file /root/agents-ci/ansible/lib/ansible/module_utils/parsing/__init__.py
Using module_utils file /root/agents-ci/ansible/lib/ansible/module_utils/pycompat24.py
Using module_utils file /root/agents-ci/ansible/lib/ansible/module_utils/six/__init__.py
Using module_utils file /root/agents-ci/ansible/lib/ansible/module_utils/connection.py
Using module_utils file /root/agents-ci/ansible/lib/ansible/module_utils/network_common.py
Using module_utils file /root/agents-ci/ansible/lib/ansible/module_utils/urls.py
Using module file /root/agents-ci/ansible/lib/ansible/modules/network/nxos/nxos_reboot.py
<n9k.example.com> ESTABLISH LOCAL CONNECTION FOR USER: root
<n9k.example.com> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1501711059.59-209487996678595 `" && echo ansible-tmp-1501711059.59-209487996678595="` echo $HOME/.ansible/tmp/ansible-tmp-1501711059.59-209487996678595 `" ) && sleep 0'
<n9k.example.com> PUT /tmp/tmpf998Ut TO /root/.ansible/tmp/ansible-tmp-1501711059.59-209487996678595/nxos_reboot.py
<n9k.example.com> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1501711059.59-209487996678595/ /root/.ansible/tmp/ansible-tmp-1501711059.59-209487996678595/nxos_reboot.py && sleep 0'
<n9k.example.com> EXEC /bin/sh -c '/usr/bin/python /root/.ansible/tmp/ansible-tmp-1501711059.59-209487996678595/nxos_reboot.py; rm -rf "/root/.ansible/tmp/ansible-tmp-1501711059.59-209487996678595/" > /dev/null 2>&1 && sleep 0'
fatal: [n9k.example.com]: FAILED! => {
    "changed": false, 
    "clierror": "Syntax error while parsing 'terminal-dont-ask  | xml '\n\n\nCmd exec error.\n", 
    "code": "400", 
    "failed": true, 
    "input": "terminal-dont-ask", 
    "invocation": {
        "module_args": {
            "host": "n9k.example.com", 
            "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", 
            "port": 80, 
            "provider": {
                "host": "n9k.example.com", 
                "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", 
                "port": 80, 
                "ssh_keyfile": null, 
                "timeout": 10, 
                "transport": "nxapi", 
                "use_ssl": false, 
                "username": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", 
                "validate_certs": true
            }, 
            "ssh_keyfile": null, 
            "timeout": 10, 
            "transport": "nxapi", 
            "url_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", 
            "url_username": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", 
            "use_ssl": false, 
            "username": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", 
            "validate_certs": true
        }
    }, 
    "msg": "Input CLI command error", 
    "output": {
        "clierror": "Syntax error while parsing 'terminal-dont-ask  | xml '\n\n\nCmd exec error.\n", 
        "code": "400", 
        "input": "terminal-dont-ask", 
        "msg": "Input CLI command error"
    }, 
    "url": "http://n9k.example.com:80/ins"
}
```

* Second Issue logs:
```shell
TASK [nxos_reboot : debug] ****************************************************************************************************************************************************************************************************************************
task path: /root/agents-ci/ansible/test/integration/targets/nxos_reboot/tests/common/sanity.yaml:2
ok: [n9k.example.com] => {
    "msg": "START TRANSPORT:nxapi nxos_reboot sanity test"
}

TASK [nxos_reboot : Reboot Switch] ********************************************************************************************************************************************************************************************************************
task path: /root/agents-ci/ansible/test/integration/targets/nxos_reboot/tests/common/sanity.yaml:5
<n9k.example.com> connection transport is nxapi
Using module_utils file /root/agents-ci/ansible/lib/ansible/module_utils/basic.py
Using module_utils file /root/agents-ci/ansible/lib/ansible/module_utils/nxos.py
Using module_utils file /root/agents-ci/ansible/lib/ansible/module_utils/_text.py
Using module_utils file /root/agents-ci/ansible/lib/ansible/module_utils/parsing/convert_bool.py
Using module_utils file /root/agents-ci/ansible/lib/ansible/module_utils/parsing/__init__.py
Using module_utils file /root/agents-ci/ansible/lib/ansible/module_utils/pycompat24.py
Using module_utils file /root/agents-ci/ansible/lib/ansible/module_utils/six/__init__.py
Using module_utils file /root/agents-ci/ansible/lib/ansible/module_utils/connection.py
Using module_utils file /root/agents-ci/ansible/lib/ansible/module_utils/network_common.py
Using module_utils file /root/agents-ci/ansible/lib/ansible/module_utils/urls.py
Using module file /root/agents-ci/ansible/lib/ansible/modules/network/nxos/nxos_reboot.py
<n9k.example.com> ESTABLISH LOCAL CONNECTION FOR USER: root
<n9k.example.com> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1501711371.75-15898415662202 `" && echo ansible-tmp-1501711371.75-15898415662202="` echo $HOME/.ansible/tmp/ansible-tmp-1501711371.75-15898415662202 `" ) && sleep 0'
<n9k.example.com> PUT /tmp/tmpnYmiZs TO /root/.ansible/tmp/ansible-tmp-1501711371.75-15898415662202/nxos_reboot.py
<n9k.example.com> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1501711371.75-15898415662202/ /root/.ansible/tmp/ansible-tmp-1501711371.75-15898415662202/nxos_reboot.py && sleep 0'
<n9k.example.com> EXEC /bin/sh -c '/usr/bin/python /root/.ansible/tmp/ansible-tmp-1501711371.75-15898415662202/nxos_reboot.py; rm -rf "/root/.ansible/tmp/ansible-tmp-1501711371.75-15898415662202/" > /dev/null 2>&1 && sleep 0'
fatal: [n9k.example.com]: FAILED! => {
    "changed": false, 
    "clierror": "Syntax error while parsing 'terminal dont-ask  | xml '\n\n\nCmd exec error.\n", 
    "code": "400", 
    "failed": true, 
    "input": "terminal dont-ask", 
    "invocation": {
        "module_args": {
            "host": "n9k.example.com", 
            "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", 
            "port": 80, 
            "provider": {
                "host": "n9k.example.com", 
                "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", 
                "port": 80, 
                "ssh_keyfile": null, 
                "timeout": 10, 
                "transport": "nxapi", 
                "use_ssl": false, 
                "username": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", 
                "validate_certs": true
            }, 
            "ssh_keyfile": null, 
            "timeout": 10, 
            "transport": "nxapi", 
            "url_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", 
            "url_username": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", 
            "use_ssl": false, 
            "username": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", 
            "validate_certs": true
        }
    }, 
    "msg": "Input CLI command error", 
    "output": {
        "clierror": "Syntax error while parsing 'terminal dont-ask  | xml '\n\n\nCmd exec error.\n", 
        "code": "400", 
        "input": "terminal dont-ask", 
        "msg": "Input CLI command error"
    }, 
    "url": "http://n9k.example.com:80/ins"
}
```

With the fix for terminal dont-ask the module works fine for `cli` transport.


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_reboot.py b/lib/ansible/modules/network/nxos/nxos_reboot.py
--- a/lib/ansible/modules/network/nxos/nxos_reboot.py
+++ b/lib/ansible/modules/network/nxos/nxos_reboot.py
@@ -67,7 +67,7 @@
 
 def reboot(module):
     cmds = [
-        {'command': 'terminal-dont-ask'},
+        {'command': 'terminal dont-ask', 'output': 'text'},
         {'command': 'reload', 'output': 'text'}
     ]
     run_commands(module, cmds)


Test patch: