# Buggy code
def run_commands(self, commands, check_rc=True):
    """Runs list of commands on remote device and returns results"""
    try:
        out = self.send_request(commands)
    except ConnectionError as exc:
        if check_rc:
            raise
        out = to_text(exc)

    out = to_list(out)
    for index, response in enumerate(out):
        if response[0] == "{":
            out[index] = json.loads(response)
    return out


# Traceback (ansible.module_utils.connection.ConnectionError)
TASK [nxos_hsrp : configure nxos_hsrp] ***********************************************************************************************************************************************************
task path: /root/agents-ci/ansible/test/integration/targets/nxos_hsrp/tests/common/sanity.yaml:32
<n9k.example.com> loaded API plugin for network_os nxos
<n9k.example.com> attempting to start connection
<n9k.example.com> using connection plugin httpapi
<n9k.example.com> found existing local domain socket, using it!
<n9k.example.com> updating play_context for connection
<n9k.example.com>
<n9k.example.com> local domain socket path is /root/.ansible/pc/04159f7b87
<n9k.example.com> ESTABLISH LOCAL CONNECTION FOR USER: root
<n9k.example.com> EXEC /bin/sh -c '( umask 77 &amp;&amp; mkdir -p "` echo /root/.ansible/tmp/ansible-local-3978XNF1Jv/ansible-tmp-1527259621.9-280686702421970 `" &amp;&amp; echo ansible-tmp-1527259621.9-280686702421970="` echo /root/.ansible/tmp/ansible-local-3978XNF1Jv/ansible-tmp-1527259621.9-280686702421970 `" ) &amp;&amp; sleep 0'
Using module file /root/agents-ci/ansible/lib/ansible/modules/network/nxos/nxos_hsrp.py
<n9k.example.com> PUT /root/.ansible/tmp/ansible-local-3978XNF1Jv/tmpma5sjg TO /root/.ansible/tmp/ansible-local-3978XNF1Jv/ansible-tmp-1527259621.9-280686702421970/nxos_hsrp.py
<n9k.example.com> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-local-3978XNF1Jv/ansible-tmp-1527259621.9-280686702421970/ /root/.ansible/tmp/ansible-local-3978XNF1Jv/ansible-tmp-1527259621.9-280686702421970/nxos_hsrp.py &amp;&amp; sleep 0'
<n9k.example.com> EXEC /bin/sh -c '/usr/bin/python /root/.ansible/tmp/ansible-local-3978XNF1Jv/ansible-tmp-1527259621.9-280686702421970/nxos_hsrp.py &amp;&amp; sleep 0'
<n9k.example.com> EXEC /bin/sh -c 'rm -f -r /root/.ansible/tmp/ansible-local-3978XNF1Jv/ansible-tmp-1527259621.9-280686702421970/ > /dev/null 2>&amp;1 &amp;&amp; sleep 0'
The full traceback is:
Traceback (most recent call last):
File "/tmp/ansible_Nafv7I/ansible_module_nxos_hsrp.py", line 508, in <module>
main()
File "/tmp/ansible_Nafv7I/ansible_module_nxos_hsrp.py", line 459, in main
existing = get_hsrp_group(group, interface, module)
File "/tmp/ansible_Nafv7I/ansible_module_nxos_hsrp.py", line 243, in get_hsrp_group
body = execute_show_command(command, module)[0]
File "/tmp/ansible_Nafv7I/ansible_module_nxos_hsrp.py", line 169, in execute_show_command
body = run_commands(module, cmds)
File "/tmp/ansible_Nafv7I/ansible_modlib.zip/ansible/module_utils/network/nxos/nxos.py", line 431, in run_commands
File "/tmp/ansible_Nafv7I/ansible_modlib.zip/ansible/module_utils/network/nxos/nxos.py", line 150, in run_commands
File "/tmp/ansible_Nafv7I/ansible_modlib.zip/ansible/module_utils/connection.py", line 149, in __rpc__
ansible.module_utils.connection.ConnectionError: string index out of range

fatal: [n9k.example.com]: FAILED! => {
"changed": false,
"module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_Nafv7I/ansible_module_nxos_hsrp.py\", line 508, in <module>\n    main()\n  File \"/tmp/ansible_Nafv7I/ansible_module_nxos_hsrp.py\", line 459, in main\n    existing = get_hsrp_group(group, interface, module)\n  File \"/tmp/ansible_Nafv7I/ansible_module_nxos_hsrp.py\", line 243, in get_hsrp_group\n    body = execute_show_command(command, module)[0]\n  File \"/tmp/ansible_Nafv7I/ansible_module_nxos_hsrp.py\", line 169, in execute_show_command\n    body = run_commands(module, cmds)\n  File \"/tmp/ansible_Nafv7I/ansible_modlib.zip/ansible/module_utils/network/nxos/nxos.py\", line 431, in run_commands\n  File \"/tmp/ansible_Nafv7I/ansible_modlib.zip/ansible/module_utils/network/nxos/nxos.py\", line 150, in run_commands\n  File \"/tmp/ansible_Nafv7I/ansible_modlib.zip/ansible/module_utils/connection.py\", line 149, in __rpc__\nansible.module_utils.connection.ConnectionError: string index out of range\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 1
}

# Fixed code
def run_commands(self, commands, check_rc=True):
    """Runs list of commands on remote device and returns results"""
    try:
        out = self.send_request(commands)
    except ConnectionError as exc:
        if check_rc:
            raise
        out = to_text(exc)

    out = to_list(out)
    if not out[0]:
        return out

    for index, response in enumerate(out):
        if response[0] == "{":
            out[index] = json.loads(response)

    return out