Repository: ansible/ansible

Issue:
nxos_command output processing errors when using xml output method
<!---
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_command

##### ANSIBLE VERSION
<!--- Paste verbatim output from “ansible --version” between quotes below -->
```
ansible 2.3.1.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
  python version = 2.7.13 (default, Jan 19 2017, 14:48:08) [GCC 6.3.0 20170118]

```

##### CONFIGURATION
<!---
Mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).
-->
callback plugin skippy used
##### 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.
-->
Linux/Ubuntu
##### SUMMARY
<!--- Explain the problem briefly -->
When tried to fetch cli output in xml format, nxos_command module breaks the xml output and reads the data partially leaving data in socket stream which is read when executing other commands in same play. 

##### 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 -->
main.yml
```
---
 - name: tester
   hosts: nxos7k
   connection: local
   gather_facts: no
   roles:
     - creds
   tasks:
   - nxos_command:
       commands: 
         - show module | xml 
         - show version | include image
         - show version | include image
         - show version | include image
       provider: "{{prov}}"
     register: out     
   - nxos_command:
       commands: 
         - show cli history
       provider: "{{prov}}"
     register: out2

   - debug: 
        var: out
   - debug: 
        var: out2

```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
```
vinp@vinp-lvm:~$ ansible-playbook buggy_bug.yml

PLAY [tester] ***********************************************************************************************************************

TASK [nxos_command] *****************************************************************************************************************
ok: [nxos7k.vinpcom]

TASK [nxos_command] *****************************************************************************************************************
ok: [nxos7k.vinpcom]

TASK [debug] ************************************************************************************************************************
ok: [nxos7k.vinpcom] => {
    "out": {
        "changed": false, 
        "stdout": [
            "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<nf:rpc-reply xmlns:nf=\"urn:ietf:params:xml:ns:netconf:base:1.0\" xmlns=\"http://www.cisco.com/nxos:1.0:lcmcli\">\n <nf:data>\n  <show> 
            <__XML__OPT_Cmd_show_module___readonly__>\n      <__readonly__>\n         <modinf>1</modinf>\n         <ports>32</ports>\n         <modtype>10 Gbps Ethernet Module</modtype>\n         <model>N7K-M132XP-12</model>\n         <status>ok</status>\n         <modinf>2</modinf>\n         <ports>48</ports>\n         <modtype>1000 Mbps Optical Ethernet Module</modtype>\n         <model>N7K-M148GS-11</model>\n         <status>ok</status>\n         <modinf>5</modinf>\n         <ports>0</ports>\n         <modtype>Supervisor Module-1X</modtype>\n         <model>N7K-SUP1</model>\n         <status>active *</status>\n         <modinf>6</modinf>\n         <ports>0</ports>\n         <modtype>Supervisor Module-1X</modtype>\n         <model>N7K-SUP1</model>\n         <status>ha-standby</status> 
            <TABLE_modwwninfo>\n        <ROW_modwwninfo>\n         <modwwn>1</modwwn>\n         <sw>6.2(12)</sw>\n        </ROW_modwwninfo>\n        <ROW_modwwninfo>\n         <modwwn>2</modwwn>\n         <sw>6.2(12)</sw> 
            1.3</hw>\n        </ROW_modwwninfo>\n        <ROW_modwwninfo>\n         <modwwn>5</modwwn>\n         <hw>3.0</hw>\n        </ROW_modwwninfo>\n        <ROW_modwwninfo>\n         <modwwn>6</modwwn><hw>1.8</hw>\n        </ROW_modwwninfo>\n       </TABLE_modwwninfo>\n         <modmac>1</modmac>\n         <mac> xx-xx-xx-xx-xx-xx to xx-xx-xx-xx-xx-xx  </mac>\n         <serialnum>xxxxx</serialnum>\n         <modmac>2</modmac>\n         <mac> xx-xx-xx-xx-xx-xx to xx-xx-xx-xx-xx-xx  </mac>\n         <serialnum>xxxxx</serialnum>
.............................", "  kickstart image file is: bootflash:///n7000-s1-kickstart.6.2.12.bin
  system image file is:    bootflash:///n7000-s1-dk9.6.2.12.bin", "  kickstart image file is: bootflash:///n7000-s1-kickstart.6.2.12.bin
  system image file is:    bootflash:///n7000-s1-dk9.6.2.12.bin", "  kickstart image file is: bootflash:///n7000-s1-kickstart.6.2.12.bin
  system image file is:    bootflash:///n7000-s1-dk9.6.2.12.bin"
        ], 
        "stdout_lines": [
            [
                "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>", 
                "<nf:rpc-reply xmlns:nf=\"urn:ietf:params:xml:ns:netconf:base:1.0\" xmlns=\"http://www.cisco.com/nxos:1.0:lcmcli\">", 
                " <nf:data>", 
                "  <show>"
            ], 
            [
                "<__XML__OPT_Cmd_show_module___readonly__>", 
                "      <__readonly__>", 
                "         <modinf>1</modinf>", 
                "         <ports>32</ports>", 
                "         <modtype>10 Gbps Ethernet Module</modtype>", 
                "         <model>N7K-M132XP-12</model>", 
                "         <status>ok</status>", 
                "         <modinf>2</modinf>", 
                "         <ports>48</ports>", 
                "         <modtype>1000 Mbps Optical Ethernet Module</modtype>", 
                "         <model>N7K-M148GS-11</model>", 
                "         <status>ok</status>", 
                "         <modinf>5</modinf>", 
                "         <ports>0</ports>", 
                "         <modtype>Supervisor Module-1X</modtype>", 
                "         <model>N7K-SUP1</model>", 
                "         <status>active *</status>", 
                "         <modinf>6</modinf>", 
                "         <ports>0</ports>", 
                "         <modtype>Supervisor Module-1X</modtype>", 
                "         <model>N7K-SUP1</model>", 
                "         <status>ha-standby</status>"
                "<TABLE_modwwninfo>", 
                "        <ROW_modwwninfo>", 
                "         <modwwn>1</modwwn>", 
                "         <sw>6.2(12)</sw>", 
                "        </ROW_modwwninfo>", 
                "        <ROW_modwwninfo>", 
                "         <modwwn>2</modwwn>", 
                "         <sw>6.2(12)</sw>"
                "1.3</hw>", 
                "        </ROW_modwwninfo>", 
                "        <ROW_modwwninfo>", 
                "         <modwwn>5</modwwn>", 
                "         <hw>3.0</hw>", 
                "        </ROW_modwwninfo>", 
                "        <ROW_modwwninfo>", 
                "         <modwwn>6</modwwn>"
                "<hw>1.8</hw>", 
                "        </ROW_modwwninfo>", 
                "       </TABLE_modwwninfo>", 
                "         <modmac>1</modmac>", 
                "         <mac> xx-xx-xx-xx-xx-xx to xx-xx-xx-xx-xx-xx  </mac>", 
                "         <serialnum>xxxxx</serialnum>", 
                "         <modmac>2</modmac>", 
                "         <mac> xx-xx-xx-xx-xx-xx to xx-xx-xx-xx-xx-xx  </mac>", 
                "         <serialnum>xxxxx</serialnum>"
                "....................."
            ], 
            [
				"  kickstart image file is: bootflash:///n7000-s1-kickstart.6.2.12.bin"
				"  system image file is:    bootflash:///n7000-s1-dk9.6.2.12.bin"
            ]
            [
				"  kickstart image file is: bootflash:///n7000-s1-kickstart.6.2.12.bin"
				"  system image file is:    bootflash:///n7000-s1-dk9.6.2.12.bin"
            ]
            [
				"  kickstart image file is: bootflash:///n7000-s1-kickstart.6.2.12.bin"
				"  system image file is:    bootflash:///n7000-s1-dk9.6.2.12.bin"
            ]
        ]
    }
}

TASK [debug] ************************************************************************************************************************
ok: [nxos7k.vinpcom] => {
    "out2": {
        "changed": false, 
        "stdout": [
            "0  10:15:02   terminal length 0\n\n1  10:15:03   terminal width 511\n\n2  10:15:03   show version | xml\n\n3  10:15:04   show version | include image\n\n4  10:15:07   show cli history"
        ], 
        "stdout_lines": [
            [
              "0  10:15:02   terminal length 0", 
                "", 
                "1  10:15:03   terminal width 511", 
                "", 
                "2  10:15:03   show version | xml", 
                "", 
                "3  10:15:04   show version | include image", 
                "", 
                "4  10:15:07   show cli history"

            ]
        ]
    }
}

PLAY RECAP **************************************************************************************************************************
nxos7k.vinpcom : ok=4    changed=0    unreachable=0    failed=0   
```

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->

<!--- Paste verbatim command output between quotes below -->
```

vinp@vinp-lvm:~$ ansible-playbook buggy_bug.yml -vvvv
Using /etc/ansible/ansible.cfg as config file
Loading callback plugin skippy of type stdout, v2.0 from /usr/local/lib/python2.7/dist-packages/ansible/plugins/callback/__init__.pyc

PLAYBOOK: buggy_bug.yml *************************************************************************************************************
1 plays in buggy_bug.yml

PLAY [tester] ***********************************************************************************************************************
META: ran handlers

TASK [nxos_command] *****************************************************************************************************************
task path: /home/vinp/buggy_bug.yml:12
<nxos7k.vinpcom> connection transport is cli
<nxos7k.vinpcom> using connection plugin network_cli
<nxos7k.vinpcom> socket_path: /home/vinp/.ansible/pc/ca1fb602dc
open_shell() returned 0 ok 
Using module file /usr/local/lib/python2.7/dist-packages/ansible/modules/network/nxos/nxos_command.py
<nxos7k.vinpcom> ESTABLISH LOCAL CONNECTION FOR USER: vinp
<nxos7k.vinpcom> EXEC /bin/sh -c 'echo ~ && sleep 0'
<nxos7k.vinpcom> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/vinp/.ansible/tmp/ansible-tmp-1498558648.45-119042556025541 `" && echo ansible-tmp-1498558648.45-119042556025541="` echo /home/vinp/.ansible/tmp/ansible-tmp-1498558648.45-119042556025541 `" ) && sleep 0'
<nxos7k.vinpcom> PUT /tmp/tmpyFZigB TO /home/vinp/.ansible/tmp/ansible-tmp-1498558648.45-119042556025541/nxos_command.py
<nxos7k.vinpcom> EXEC /bin/sh -c 'chmod u+x /home/vinp/.ansible/tmp/ansible-tmp-1498558648.45-119042556025541/ /home/vinp/.ansible/tmp/ansible-tmp-1498558648.45-119042556025541/nxos_command.py && sleep 0'
<nxos7k.vinpcom> EXEC /bin/sh -c '/usr/bin/python /home/vinp/.ansible/tmp/ansible-tmp-1498558648.45-119042556025541/nxos_command.py; rm -rf "/home/vinp/.ansible/tmp/ansible-tmp-1498558648.45-119042556025541/" > /dev/null 2>&1 && sleep 0'
ok: [nxos7k.vinpcom] => {
    "changed": false, 
    "invocation": {
        "module_args": {
            "commands": [
                "show module | xml", 
                "show version | include image", 
                "show version | include image", 
                "show version | include image"
            ], 
            "host": "nxos7k.vinpcom", 
            "interval": 1, 
            "match": "all", 
            "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", 
            "port": null, 
            "provider": {
                "host": "nxos7k.vinpcom", 
                "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", 
                "port": null, 
                "ssh_keyfile": null, 
                "timeout": null, 
                "transport": null, 
                "use_ssl": null, 
                "username": "vinpk", 
                "validate_certs": null
            }, 
            "retries": 10, 
            "ssh_keyfile": null, 
            "timeout": 10, 
            "transport": "cli", 
            "use_ssl": null, 
            "username": "vinpk", 
            "validate_certs": null, 
            "wait_for": null
        }
    }, 
    "stdout": [
        "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<nf:rpc-reply xmlns:nf=\"urn:ietf:params:xml:ns:netconf:base:1.0\" xmlns=\"http://www.cisco.com/nxos:1.0:lcmcli\">\n <nf:data>\n  <show>", 
        "<__XML__OPT_Cmd_show_module___readonly__>\n      <__readonly__>\n         <modinf>1</modinf>\n         <ports>32</ports>\n         <modtype>10 Gbps Ethernet Module</modtype>\n         <model>N7K-M132XP-12</model>\n         <status>ok</status>\n         <modinf>2</modinf>\n         <ports>48</ports>\n         <modtype>1000 Mbps Optical Ethernet Module</modtype>\n         <model>N7K-M148GS-11</model>\n         <status>ok</status>\n         <modinf>5</modinf>\n         <ports>0</ports>\n         <modtype>Supervisor Module-1X</modtype>\n         <model>N7K-SUP1</model>\n         <status>active *</status>\n         <modinf>6</modinf>\n         <ports>0</ports>\n         <modtype>Supervisor Module-1X</modtype>\n         <model>N7K-SUP1</model>\n         <status>ha-standby</status>", 
        "<TABLE_modwwninfo>\n        <ROW_modwwninfo>\n         <modwwn>1</modwwn>\n         <sw>6.2(12)</sw>\n        </ROW_modwwninfo>\n        <ROW_modwwninfo>\n         <modwwn>2</modwwn>\n         <sw>6.2(12)</sw>", 
        "1.3</hw>\n        </ROW_modwwninfo>\n        <ROW_modwwninfo>\n         <modwwn>5</modwwn>\n         <hw>3.0</hw>\n        </ROW_modwwninfo>\n        <ROW_modwwninfo>\n         <modwwn>6</modwwn>"
    ], 
    "stdout_lines": [
        [
            "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>", 
            "<nf:rpc-reply xmlns:nf=\"urn:ietf:params:xml:ns:netconf:base:1.0\" xmlns=\"http://www.cisco.com/nxos:1.0:lcmcli\">", 
            " <nf:data>", 
            "  <show>"
        ], 
        [
            "<__XML__OPT_Cmd_show_module___readonly__>", 
            "      <__readonly__>", 
            "         <modinf>1</modinf>", 
            "         <ports>32</ports>", 
            "         <modtype>10 Gbps Ethernet Module</modtype>", 
            "         <model>N7K-M132XP-12</model>", 
            "         <status>ok</status>", 
            "         <modinf>2</modinf>", 
            "         <ports>48</ports>", 
            "         <modtype>1000 Mbps Optical Ethernet Module</modtype>", 
            "         <model>N7K-M148GS-11</model>", 
            "         <status>ok</status>", 
            "         <modinf>5</modinf>", 
            "         <ports>0</ports>", 
            "         <modtype>Supervisor Module-1X</modtype>", 
            "         <model>N7K-SUP1</model>", 
            "         <status>active *</status>", 
            "         <modinf>6</modinf>", 
            "         <ports>0</ports>", 
            "         <modtype>Supervisor Module-1X</modtype>", 
            "         <model>N7K-SUP1</model>", 
            "         <status>ha-standby</status>"
        ], 
        [
            "<TABLE_modwwninfo>", 
            "        <ROW_modwwninfo>", 
            "         <modwwn>1</modwwn>", 
            "         <sw>6.2(12)</sw>", 
            "        </ROW_modwwninfo>", 
            "        <ROW_modwwninfo>", 
            "         <modwwn>2</modwwn>", 
            "         <sw>6.2(12)</sw>"
        ], 
        [
            "1.3</hw>", 
            "        </ROW_modwwninfo>", 
            "        <ROW_modwwninfo>", 
            "         <modwwn>5</modwwn>", 
            "         <hw>3.0</hw>", 
            "        </ROW_modwwninfo>", 
            "        <ROW_modwwninfo>", 
            "         <modwwn>6</modwwn>"
        ]
    ]
}

TASK [nxos_command] *****************************************************************************************************************
task path: /home/vinp/buggy_bug.yml:21
<nxos7k.vinpcom> connection transport is cli
<nxos7k.vinpcom> using connection plugin network_cli
<nxos7k.vinpcom> socket_path: /home/vinp/.ansible/pc/ca1fb602dc
Using module file /usr/local/lib/python2.7/dist-packages/ansible/modules/network/nxos/nxos_command.py
<nxos7k.vinpcom> ESTABLISH LOCAL CONNECTION FOR USER: vinp
<nxos7k.vinpcom> EXEC /bin/sh -c 'echo ~ && sleep 0'
<nxos7k.vinpcom> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/vinp/.ansible/tmp/ansible-tmp-1498558649.28-34554303414170 `" && echo ansible-tmp-1498558649.28-34554303414170="` echo /home/vinp/.ansible/tmp/ansible-tmp-1498558649.28-34554303414170 `" ) && sleep 0'
<nxos7k.vinpcom> PUT /tmp/tmpH3vFnE TO /home/vinp/.ansible/tmp/ansible-tmp-1498558649.28-34554303414170/nxos_command.py
<nxos7k.vinpcom> EXEC /bin/sh -c 'chmod u+x /home/vinp/.ansible/tmp/ansible-tmp-1498558649.28-34554303414170/ /home/vinp/.ansible/tmp/ansible-tmp-1498558649.28-34554303414170/nxos_command.py && sleep 0'
<nxos7k.vinpcom> EXEC /bin/sh -c '/usr/bin/python /home/vinp/.ansible/tmp/ansible-tmp-1498558649.28-34554303414170/nxos_command.py; rm -rf "/home/vinp/.ansible/tmp/ansible-tmp-1498558649.28-34554303414170/" > /dev/null 2>&1 && sleep 0'
ok: [nxos7k.vinpcom] => {
    "changed": false, 
    "invocation": {
        "module_args": {
            "commands": [
                "show cli history"
            ], 
            "host": "nxos7k.vinpcom", 
            "interval": 1, 
            "match": "all", 
            "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", 
            "port": null, 
            "provider": {
                "host": "nxos7k.vinpcom", 
                "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", 
                "port": null, 
                "ssh_keyfile": null, 
                "timeout": null, 
                "transport": null, 
                "use_ssl": null, 
                "username": "vinpk", 
                "validate_certs": null
            }, 
            "retries": 10, 
            "ssh_keyfile": null, 
            "timeout": 10, 
            "transport": "cli", 
            "use_ssl": null, 
            "username": "vinpk", 
            "validate_certs": null, 
            "wait_for": null
        }
    }, 
    "stdout": [
        "<hw>1.8</hw>\n        </ROW_modwwninfo>\n       </TABLE_modwwninfo>\n         <modmac>1</modmac>\n         <mac> xx-xx-xx-xx-xx-xx to xx-xx-xx-xx-xx-xx  </mac>\n         <serialnum>xxxxx</serialnum>\n         <modmac>2</modmac>\n         <mac> xx-xx-xx-xx-xx-xx to xx-xx-xx-xx-xx-xx  </mac>\n         <serialnum>xxxxx</serialnum>"
    ], 
    "stdout_lines": [
        [
            "<hw>1.8</hw>", 
            "        </ROW_modwwninfo>", 
            "       </TABLE_modwwninfo>", 
            "         <modmac>1</modmac>", 
            "         <mac> xx-xx-xx-xx-xx-xx to xx-xx-xx-xx-xx-xx  </mac>", 
            "         <serialnum>xxxxx</serialnum>", 
            "         <modmac>2</modmac>", 
            "         <mac> xx-xx-xx-xx-xx-xx to xx-xx-xx-xx-xx-xx  </mac>", 
            "         <serialnum>xxxxx</serialnum>"
        ]
    ]
}

TASK [debug] ************************************************************************************************************************
task path: /home/vinp/buggy_bug.yml:27
ok: [nxos7k.vinpcom] => {
    "out": {
        "changed": false, 
        "stdout": [
            "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<nf:rpc-reply xmlns:nf=\"urn:ietf:params:xml:ns:netconf:base:1.0\" xmlns=\"http://www.cisco.com/nxos:1.0:lcmcli\">\n <nf:data>\n  <show>", 
            "<__XML__OPT_Cmd_show_module___readonly__>\n      <__readonly__>\n         <modinf>1</modinf>\n         <ports>32</ports>\n         <modtype>10 Gbps Ethernet Module</modtype>\n         <model>N7K-M132XP-12</model>\n         <status>ok</status>\n         <modinf>2</modinf>\n         <ports>48</ports>\n         <modtype>1000 Mbps Optical Ethernet Module</modtype>\n         <model>N7K-M148GS-11</model>\n         <status>ok</status>\n         <modinf>5</modinf>\n         <ports>0</ports>\n         <modtype>Supervisor Module-1X</modtype>\n         <model>N7K-SUP1</model>\n         <status>active *</status>\n         <modinf>6</modinf>\n         <ports>0</ports>\n         <modtype>Supervisor Module-1X</modtype>\n         <model>N7K-SUP1</model>\n         <status>ha-standby</status>", 
            "<TABLE_modwwninfo>\n        <ROW_modwwninfo>\n         <modwwn>1</modwwn>\n         <sw>6.2(12)</sw>\n        </ROW_modwwninfo>\n        <ROW_modwwninfo>\n         <modwwn>2</modwwn>\n         <sw>6.2(12)</sw>", 
            "1.3</hw>\n        </ROW_modwwninfo>\n        <ROW_modwwninfo>\n         <modwwn>5</modwwn>\n         <hw>3.0</hw>\n        </ROW_modwwninfo>\n        <ROW_modwwninfo>\n         <modwwn>6</modwwn>"
        ], 
        "stdout_lines": [
            [
                "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>", 
                "<nf:rpc-reply xmlns:nf=\"urn:ietf:params:xml:ns:netconf:base:1.0\" xmlns=\"http://www.cisco.com/nxos:1.0:lcmcli\">", 
                " <nf:data>", 
                "  <show>"
            ], 
            [
                "<__XML__OPT_Cmd_show_module___readonly__>", 
                "      <__readonly__>", 
                "         <modinf>1</modinf>", 
                "         <ports>32</ports>", 
                "         <modtype>10 Gbps Ethernet Module</modtype>", 
                "         <model>N7K-M132XP-12</model>", 
                "         <status>ok</status>", 
                "         <modinf>2</modinf>", 
                "         <ports>48</ports>", 
                "         <modtype>1000 Mbps Optical Ethernet Module</modtype>", 
                "         <model>N7K-M148GS-11</model>", 
                "         <status>ok</status>", 
                "         <modinf>5</modinf>", 
                "         <ports>0</ports>", 
                "         <modtype>Supervisor Module-1X</modtype>", 
                "         <model>N7K-SUP1</model>", 
                "         <status>active *</status>", 
                "         <modinf>6</modinf>", 
                "         <ports>0</ports>", 
                "         <modtype>Supervisor Module-1X</modtype>", 
                "         <model>N7K-SUP1</model>", 
                "         <status>ha-standby</status>"
            ], 
            [
                "<TABLE_modwwninfo>", 
                "        <ROW_modwwninfo>", 
                "         <modwwn>1</modwwn>", 
                "         <sw>6.2(12)</sw>", 
                "        </ROW_modwwninfo>", 
                "        <ROW_modwwninfo>", 
                "         <modwwn>2</modwwn>", 
                "         <sw>6.2(12)</sw>"
            ], 
            [
                "1.3</hw>", 
                "        </ROW_modwwninfo>", 
                "        <ROW_modwwninfo>", 
                "         <modwwn>5</modwwn>", 
                "         <hw>3.0</hw>", 
                "        </ROW_modwwninfo>", 
                "        <ROW_modwwninfo>", 
                "         <modwwn>6</modwwn>"
            ]
        ]
    }
}

TASK [debug] ************************************************************************************************************************
task path: /home/vinp/buggy_bug.yml:29
ok: [nxos7k.vinpcom] => {
    "out2": {
        "changed": false, 
        "stdout": [
            "<hw>1.8</hw>\n        </ROW_modwwninfo>\n       </TABLE_modwwninfo>\n         <modmac>1</modmac>\n         <mac> xx-xx-xx-xx-xx-xx to xx-xx-xx-xx-xx-xx  </mac>\n         <serialnum>xxxxx</serialnum>\n         <modmac>2</modmac>\n         <mac> xx-xx-xx-xx-xx-xx to xx-xx-xx-xx-xx-xx  </mac>\n         <serialnum>xxxxx</serialnum>"
        ], 
        "stdout_lines": [
            [
                "<hw>1.8</hw>", 
                "        </ROW_modwwninfo>", 
                "       </TABLE_modwwninfo>", 
                "         <modmac>1</modmac>", 
                "         <mac> xx-xx-xx-xx-xx-xx to xx-xx-xx-xx-xx-xx  </mac>", 
                "         <serialnum>xxxxx</serialnum>", 
                "         <modmac>2</modmac>", 
                "         <mac> xx-xx-xx-xx-xx-xx to xx-xx-xx-xx-xx-xx  </mac>", 
                "         <serialnum>xxxxx</serialnum>"
            ]
        ]
    }
}
META: ran handlers
META: ran handlers

PLAY RECAP **************************************************************************************************************************
nxos7k.vinpcom : ok=4    changed=0    unreachable=0    failed=0   

```



Discussion:
Similar problem with ios_command module as well when i tried to use instead of nxos_command for Nexus devices, but with a difference, that all socket stream is read before passing on to next task, commands; though xml is still broken into list in the same variable and got unexpected results. 

```
vinp@vinp-lvm:~$ ansible-playbook buggy_bug.yml

PLAY [tester] **********************************************************************************************************************

TASK [ios_command] *****************************************************************************************************************
ok: [nxos7k.vinpcom]

TASK [ios_command] *****************************************************************************************************************
ok: [nxos7k.vinpcom]

TASK [debug] ***********************************************************************************************************************
ok: [nxos7k.vinpcom] => {
    "out": {
        "changed": false, 
        "stdout": [
            "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<nf:rpc-reply xmlns:nf=\"urn:ietf:params:xml:ns:netconf:base:1.0\" xmlns=\"http://www.cisco.com/nxos:1.0:lcmcli\">\n <nf:data>\n  <show>\n   <module>\n    <__XML__OPT_Cmd_show_module_module>\n     <__XML__OPT_Cmd_show_module___readonly__>\n      <__readonly__>\n       <TABLE_modinfo>\n        <ROW_modinfo>\n         <modinf>1</modinf>\n         <ports>32</ports>\n         <modtype>10 Gbps Ethernet Module</modtype>\n         <model>N7K-M132XP-12</model>\n         <status>ok</status>\n        </ROW_modinfo>\n        <ROW_modinfo>\n         <modinf>2</modinf>\n         <ports>48</ports>\n         <modtype>1000 Mbps Optical Ethernet Module</modtype>\n         <model>N7K-M148GS-11</model>\n         <status>ok</status>\n        </ROW_modinfo>\n        <ROW_modinfo>\n         <modinf>5</modinf>\n         <ports>0</ports>\n         <modtype>Supervisor Module-1X</modtype>\n         <model>N7K-SUP1</model>\n         <status>active *</status>\n        </ROW_modinfo>\n        <ROW_modinfo>\n         <modinf>6</modinf>\n         <ports>0</ports>\n         <modtype>Supervisor Module-1X</modtype>\n         <model>N7K-SUP1</model>\n         <status>ha-standby</status>\n        </ROW_modinfo>", 
            "<TABLE_modwwninfo>\n        <ROW_modwwninfo>\n         <modwwn>1</modwwn>\n         <sw>6.2(12)</sw>\n        </ROW_modwwninfo>\n        <ROW_modwwninfo>\n         <modwwn>2</modwwn>\n         <sw>6.2(12)</sw>", 
            "1.3</hw>\n        </ROW_modwwninfo>\n        <ROW_modwwninfo>\n         <modwwn>5</modwwn>\n         <sw>6.2(12)</sw>\n         <hw>3.0</hw>\n        </ROW_modwwninfo>\n        <ROW_modwwninfo>\n         <modwwn>6</modwwn>\n         <sw>6.2(12)</sw>\n         <hw>1.8</hw>\n        </ROW_modwwninfo>\n       </TABLE_modwwninfo>\n       <TABLE_modmacinfo>\n        <ROW_modmacinfo>\n         <modmac>1</modmac>\n         <mac> xx-xx-xx-xx-xx-xx to xx-xx-xx-xx-xx-xx  </mac>\n         <serialnum>xxxxx</serialnum>\n        </ROW_modmacinfo>\n        <ROW_modmacinfo>\n         <modmac>2</modmac>\n         <mac> xx-xx-xx-xx-xx-xx to xx-xx-xx-xx-xx-xx  </mac>\n         <serialnum>xxxxx</serialnum>\n        </ROW_modmacinfo>\n        <ROW_modmacinfo>\n         <modmac>5</modmac>\n         <mac> xx-xx-xx-xx-xx-xx to xx-xx-xx-xx-xx-xx  </mac>\n         <serialnum>xxxxx</serialnum>\n        </ROW_modmacinfo>\n        <ROW_modmacinfo>\n         <modmac>6</modmac>\n         <mac> xx-xx-xx-xx-xx-xx to xx-xx-xx-xx-xx-xx  </mac>\n         <serialnum>xxxxx</serialnum>\n        </ROW_modmacinfo>\n       </TABLE_modmacinfo>\n       <TABLE_moddiaginfo>\n        <ROW_moddiaginfo>\n         <mod>1</mod>\n         <diagstatus>Pass</diagstatus>\n        </ROW_moddiaginfo>\n        <ROW_moddiaginfo>\n         <mod>2</mod>\n         <diagstatus>Pass</diagstatus>\n        </ROW_moddiaginfo>\n        <ROW_moddiaginfo>\n         <mod>5</mod>\n         <diagstatus>Pass</diagstatus>\n        </ROW_moddiaginfo>\n        <ROW_moddiaginfo>\n         <mod>6</mod>\n         <diagstatus>Fail</diagstatus>\n        </ROW_moddiaginfo>\n       </TABLE_moddiaginfo>\n       <TABLE_xbarinfo>\n        <ROW_xbarinfo>\n         <xbarinf>1</xbarinf>\n         <xbarports>0</xbarports>\n         <xbartype>Fabric Module 1</xbartype>\n         <xbarmodel>N7K-C7010-FAB-1</xbarmodel>\n         <xbarstatus>ok</xbarstatus>\n        </ROW_xbarinfo>\n        <ROW_xbarinfo>\n         <xbarinf>2</xbarinf>\n         <xbarports>0</xbarports>\n         <xbartype>Fabric Module 1</xbartype>\n         <xbarmodel>N7K-C7010-FAB-1</xbarmodel>\n         <xbarstatus>ok</xbarstatus>\n        </ROW_xbarinfo>\n        <ROW_xbarinfo>\n         <xbarinf>3</xbarinf>\n         <xbarports>0</xbarports>\n         <xbartype>Fabric Module 1</xbartype>\n         <xbarmodel>N7K-C7010-FAB-1</xbarmodel>\n         <xbarstatus>ok</xbarstatus>\n        </ROW_xbarinfo>\n       </TABLE_xbarinfo>\n       <TABLE_xbarwwninfo>\n        <ROW_xbarwwninfo>\n         <xbarwwn>1</xbarwwn>\n         <xbarsw>NA</xbarsw>\n         <xbarhw>1.1</xbarhw>\n        </ROW_xbarwwninfo>\n        <ROW_xbarwwninfo>\n         <xbarwwn>2</xbarwwn>\n         <xbarsw>NA</xbarsw>\n         <xbarhw>1.1</xbarhw>\n        </ROW_xbarwwninfo>\n        <ROW_xbarwwninfo>\n         <xbarwwn>3</xbarwwn>\n         <xbarsw>NA</xbarsw>\n         <xbarhw>1.1</xbarhw>\n        </ROW_xbarwwninfo>\n       </TABLE_xbarwwninfo>\n       <TABLE_xbarmacinfo>\n        <ROW_xbarmacinfo>\n         <xbarmac>1</xbarmac>\n         <xbarmacaddr> NA                                      </xbarmacaddr>\n         <xbarserialnum>xxxxx</xbarserialnum>\n        </ROW_xbarmacinfo>\n        <ROW_xbarmacinfo>\n         <xbarmac>2</xbarmac>\n         <xbarmacaddr> NA                                      </xbarmacaddr>\n         <xbarserialnum>xxxxx</xbarserialnum>\n        </ROW_xbarmacinfo>\n        <ROW_xbarmacinfo>\n         <xbarmac>3</xbarmac>\n         <xbarmacaddr> NA                                      </xbarmacaddr>\n         <xbarserialnum>xxxxx</xbarserialnum>\n        </ROW_xbarmacinfo>\n       </TABLE_xbarmacinfo>\n      </__readonly__>\n     </__XML__OPT_Cmd_show_module___readonly__>\n    </__XML__OPT_Cmd_show_module_module>\n   </module>\n  </show>\n </nf:data>\n</nf:rpc-reply>\n]]>]]>", 
            "kickstart image file is: bootflash:///n7000-s1-kickstart.6.2.12.bin\n  system image file is:    bootflash:///n7000-s1-dk9.6.2.12.bin\n\n\n  kickstart image file is: bootflash:///n7000-s1-kickstart.6.2.12.bin\n  system image file is:    bootflash:///n7000-s1-dk9.6.2.12.bin\n\n\n  kickstart image file is: bootflash:///n7000-s1-kickstart.6.2.12.bin\n  system image file is:    bootflash:///n7000-s1-dk9.6.2.12.bin"
        ], 
        "stdout_lines": [
            [
                "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>", 
                "<nf:rpc-reply xmlns:nf=\"urn:ietf:params:xml:ns:netconf:base:1.0\" xmlns=\"http://www.cisco.com/nxos:1.0:lcmcli\">", 
                " <nf:data>", 
                "  <show>", 
                "   <module>", 
                "    <__XML__OPT_Cmd_show_module_module>", 
                "     <__XML__OPT_Cmd_show_module___readonly__>", 
                "      <__readonly__>", 
                "       <TABLE_modinfo>", 
                "        <ROW_modinfo>", 
                "         <modinf>1</modinf>", 
                "         <ports>32</ports>", 
                "         <modtype>10 Gbps Ethernet Module</modtype>", 
                "         <model>N7K-M132XP-12</model>", 
                "         <status>ok</status>", 
                "        </ROW_modinfo>", 
                "        <ROW_modinfo>", 
                "         <modinf>2</modinf>", 
                "         <ports>48</ports>", 
                "         <modtype>1000 Mbps Optical Ethernet Module</modtype>", 
                "         <model>N7K-M148GS-11</model>", 
                "         <status>ok</status>", 
                "        </ROW_modinfo>", 
                "        <ROW_modinfo>", 
                "         <modinf>5</modinf>", 
                "         <ports>0</ports>", 
                "         <modtype>Supervisor Module-1X</modtype>", 
                "         <model>N7K-SUP1</model>", 
                "         <status>active *</status>", 
                "        </ROW_modinfo>", 
                "        <ROW_modinfo>", 
                "         <modinf>6</modinf>", 
                "         <ports>0</ports>", 
                "         <modtype>Supervisor Module-1X</modtype>", 
                "         <model>N7K-SUP1</model>", 
                "         <status>ha-standby</status>", 
                "        </ROW_modinfo>"
            ], 
            [
                "<TABLE_modwwninfo>", 
                "        <ROW_modwwninfo>", 
                "         <modwwn>1</modwwn>", 
                "         <sw>6.2(12)</sw>", 
                "        </ROW_modwwninfo>", 
                "        <ROW_modwwninfo>", 
                "         <modwwn>2</modwwn>", 
                "         <sw>6.2(12)</sw>"
            ], 
            [
                "1.3</hw>", 
                "        </ROW_modwwninfo>", 
                "        <ROW_modwwninfo>", 
                "         <modwwn>5</modwwn>", 
                "         <sw>6.2(12)</sw>", 
                "         <hw>3.0</hw>", 
                "        </ROW_modwwninfo>", 
                "        <ROW_modwwninfo>", 
                "         <modwwn>6</modwwn>", 
                "         <sw>6.2(12)</sw>", 
                "         <hw>1.8</hw>", 
                "        </ROW_modwwninfo>", 
                "       </TABLE_modwwninfo>", 
                "       <TABLE_modmacinfo>", 
                "        <ROW_modmacinfo>", 
                "         <modmac>1</modmac>", 
                "         <mac> xx-xx-xx-xx-xx-xx to xx-xx-xx-xx-xx-xx  </mac>", 
                "         <serialnum>xxxxx</serialnum>", 
                "        </ROW_modmacinfo>", 
                "        <ROW_modmacinfo>", 
                "         <modmac>2</modmac>", 
                "         <mac> xx-xx-xx-xx-xx-xx to xx-xx-xx-xx-xx-xx  </mac>", 
                "         <serialnum>xxxxx</serialnum>", 
                "        </ROW_modmacinfo>", 
                "        <ROW_modmacinfo>", 
                "         <modmac>5</modmac>", 
                "         <mac> xx-xx-xx-xx-xx-xx to xx-xx-xx-xx-xx-xx  </mac>", 
                "         <serialnum>xxxxx</serialnum>", 
                "        </ROW_modmacinfo>", 
                "        <ROW_modmacinfo>", 
                "         <modmac>6</modmac>", 
                "         <mac> xx-xx-xx-xx-xx-xx to xx-xx-xx-xx-xx-xx  </mac>", 
                "         <serialnum>xxxxx</serialnum>", 
                "        </ROW_modmacinfo>", 
                "       </TABLE_modmacinfo>", 
                "       <TABLE_moddiaginfo>", 
                "        <ROW_moddiaginfo>", 
                "         <mod>1</mod>", 
                "         <diagstatus>Pass</diagstatus>", 
                "        </ROW_moddiaginfo>", 
                "        <ROW_moddiaginfo>", 
                "         <mod>2</mod>", 
                "         <diagstatus>Pass</diagstatus>", 
                "        </ROW_moddiaginfo>", 
                "        <ROW_moddiaginfo>", 
                "         <mod>5</mod>", 
                "         <diagstatus>Pass</diagstatus>", 
                "        </ROW_moddiaginfo>", 
                "        <ROW_moddiaginfo>", 
                "         <mod>6</mod>", 
                "         <diagstatus>Fail</diagstatus>", 
                "        </ROW_moddiaginfo>", 
                "       </TABLE_moddiaginfo>", 
                "       <TABLE_xbarinfo>", 
                "        <ROW_xbarinfo>", 
                "         <xbarinf>1</xbarinf>", 
                "         <xbarports>0</xbarports>", 
                "         <xbartype>Fabric Module 1</xbartype>", 
                "         <xbarmodel>N7K-C7010-FAB-1</xbarmodel>", 
                "         <xbarstatus>ok</xbarstatus>", 
                "        </ROW_xbarinfo>", 
                "        <ROW_xbarinfo>", 
                "         <xbarinf>2</xbarinf>", 
                "         <xbarports>0</xbarports>", 
                "         <xbartype>Fabric Module 1</xbartype>", 
                "         <xbarmodel>N7K-C7010-FAB-1</xbarmodel>", 
                "         <xbarstatus>ok</xbarstatus>", 
                "        </ROW_xbarinfo>", 
                "        <ROW_xbarinfo>", 
                "         <xbarinf>3</xbarinf>", 
                "         <xbarports>0</xbarports>", 
                "         <xbartype>Fabric Module 1</xbartype>", 
                "         <xbarmodel>N7K-C7010-FAB-1</xbarmodel>", 
                "         <xbarstatus>ok</xbarstatus>", 
                "        </ROW_xbarinfo>", 
                "       </TABLE_xbarinfo>", 
                "       <TABLE_xbarwwninfo>", 
                "        <ROW_xbarwwninfo>", 
                "         <xbarwwn>1</xbarwwn>", 
                "         <xbarsw>NA</xbarsw>", 
                "         <xbarhw>1.1</xbarhw>", 
                "        </ROW_xbarwwninfo>", 
                "        <ROW_xbarwwninfo>", 
                "         <xbarwwn>2</xbarwwn>", 
                "         <xbarsw>NA</xbarsw>", 
                "         <xbarhw>1.1</xbarhw>", 
                "        </ROW_xbarwwninfo>", 
                "        <ROW_xbarwwninfo>", 
                "         <xbarwwn>3</xbarwwn>", 
                "         <xbarsw>NA</xbarsw>", 
                "         <xbarhw>1.1</xbarhw>", 
                "        </ROW_xbarwwninfo>", 
                "       </TABLE_xbarwwninfo>", 
                "       <TABLE_xbarmacinfo>", 
                "        <ROW_xbarmacinfo>", 
                "         <xbarmac>1</xbarmac>", 
                "         <xbarmacaddr> NA                                      </xbarmacaddr>", 
                "         <xbarserialnum>xxxxx</xbarserialnum>", 
                "        </ROW_xbarmacinfo>", 
                "        <ROW_xbarmacinfo>", 
                "         <xbarmac>2</xbarmac>", 
                "         <xbarmacaddr> NA                                      </xbarmacaddr>", 
                "         <xbarserialnum>xxxxx</xbarserialnum>", 
                "        </ROW_xbarmacinfo>", 
                "        <ROW_xbarmacinfo>", 
                "         <xbarmac>3</xbarmac>", 
                "         <xbarmacaddr> NA                                      </xbarmacaddr>", 
                "         <xbarserialnum>xxxxx</xbarserialnum>", 
                "        </ROW_xbarmacinfo>", 
                "       </TABLE_xbarmacinfo>", 
                "      </__readonly__>", 
                "     </__XML__OPT_Cmd_show_module___readonly__>", 
                "    </__XML__OPT_Cmd_show_module_module>", 
                "   </module>", 
                "  </show>", 
                " </nf:data>", 
                "</nf:rpc-reply>", 
                "]]>]]>"
            ], 
            [
                "kickstart image file is: bootflash:///n7000-s1-kickstart.6.2.12.bin", 
                "  system image file is:    bootflash:///n7000-s1-dk9.6.2.12.bin", 
                "", 
                "", 
                "  kickstart image file is: bootflash:///n7000-s1-kickstart.6.2.12.bin", 
                "  system image file is:    bootflash:///n7000-s1-dk9.6.2.12.bin", 
                "", 
                "", 
                "  kickstart image file is: bootflash:///n7000-s1-kickstart.6.2.12.bin", 
                "  system image file is:    bootflash:///n7000-s1-dk9.6.2.12.bin"
            ]
        ]
    }
}

TASK [debug] ***********************************************************************************************************************
ok: [nxos7k.vinpcom] => {
    "out2": {
        "changed": false, 
        "stdout": [
            "0  10:41:39   terminal length 0\n\n1  10:41:40   terminal width 512\n\n2  10:41:40   show module | xml\n\n3  10:41:41   show version | include image\n\n4  10:41:43   show cli history"
        ], 
        "stdout_lines": [
            [
                "0  10:41:39   terminal length 0", 
                "", 
                "1  10:41:40   terminal width 512", 
                "", 
                "2  10:41:40   show module | xml", 
                "", 
                "3  10:41:41   show version | include image", 
                "", 
                "4  10:41:43   show cli history"
            ]
        ]
    }
}

PLAY RECAP *************************************************************************************************************************
nxos7k.vinpcom : ok=4    changed=0    unreachable=0    failed=0   
```

cc @jedelman8 @GGabriele @rahushen @privateip @rcarrillocruz @mikewiebe @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/plugins/terminal/nxos.py b/lib/ansible/plugins/terminal/nxos.py
--- a/lib/ansible/plugins/terminal/nxos.py
+++ b/lib/ansible/plugins/terminal/nxos.py
@@ -28,7 +28,7 @@
 class TerminalModule(TerminalBase):
 
     terminal_stdout_re = [
-        re.compile(br'[\r\n]?[a-zA-Z]{1}[a-zA-Z0-9-_.]*[>|#|%](?:\s*)$'),
+        re.compile(br'[\r\n](?!\s*<)?[a-zA-Z_]{1}[a-zA-Z0-9-_.]*[>|#|%](?:\s*)$'),
         re.compile(br'[\r\n]?[a-zA-Z]{1}[a-zA-Z0-9-_.]*\(.+\)#(?:\s*)$')
     ]
 


Test patch: