# Buggy code
def run_commands(module, commands, check_rc=True):
    responses = list()
    for cmd in to_list(commands):
        cmd = module.jsonify(cmd)
        rc, out, err = exec_command(module, cmd)
        if check_rc and rc != 0:
            module.fail_json(msg=err, rc=rc)
        responses.append(out)
    return responses


# Traceback (TypeError)
TASK [ios_facts : test getting all facts] ********************************************************************************************************************************************************************************************************
task path: /home/johnb/git/ansible-inc/ansible/test/integration/targets/ios_facts/tests/network_cli/all_facts.yaml:5
20483 1486646441.98275: sending task start callback
20483 1486646441.98278: entering _queue_task() for ios01/ios_facts
20483 1486646441.98281: Creating lock for ios_facts
20483 1486646441.98573: worker is 1 (out of 1 available)
20483 1486646441.98663: exiting _queue_task() for ios01/ios_facts
20483 1486646441.98715: done queuing things up, now waiting for results queue to drain
20483 1486646441.98721: waiting for pending results...
20536 1486646441.99037: running TaskExecutor() for ios01/TASK: ios_facts : test getting all facts
20536 1486646441.99172: in run()
20536 1486646441.99212: calling self._execute()
20536 1486646441.99883: Loading FilterModule 'core' from /home/johnb/git/ansible-inc/ansible/lib/ansible/plugins/filter/core.py
20536 1486646442.02356: Loading FilterModule 'ipaddr' from /home/johnb/git/ansible-inc/ansible/lib/ansible/plugins/filter/ipaddr.py
20536 1486646442.02404: Loading FilterModule 'json_query' from /home/johnb/git/ansible-inc/ansible/lib/ansible/plugins/filter/json_query.py
20536 1486646442.02427: Loading FilterModule 'mathstuff' from /home/johnb/git/ansible-inc/ansible/lib/ansible/plugins/filter/mathstuff.py
20536 1486646442.02519: Loading TestModule 'core' from /home/johnb/git/ansible-inc/ansible/lib/ansible/plugins/test/core.py
20536 1486646442.02534: Loading TestModule 'files' from /home/johnb/git/ansible-inc/ansible/lib/ansible/plugins/test/files.py
20536 1486646442.02546: Loading TestModule 'mathstuff' from /home/johnb/git/ansible-inc/ansible/lib/ansible/plugins/test/mathstuff.py
20536 1486646442.08269: Loading Connection 'network_cli' from /home/johnb/git/ansible-inc/ansible/lib/ansible/plugins/connection/network_cli.py
20536 1486646442.08383: Loading ShellModule 'csh' from /home/johnb/git/ansible-inc/ansible/lib/ansible/plugins/shell/csh.py
20536 1486646442.08414: Loading ShellModule 'fish' from /home/johnb/git/ansible-inc/ansible/lib/ansible/plugins/shell/fish.py
20536 1486646442.08455: Loading ShellModule 'powershell' from /home/johnb/git/ansible-inc/ansible/lib/ansible/plugins/shell/powershell.py
20536 1486646442.08472: Loading ShellModule 'sh' from /home/johnb/git/ansible-inc/ansible/lib/ansible/plugins/shell/sh.py
20536 1486646442.08497: Loading ShellModule 'sh' from /home/johnb/git/ansible-inc/ansible/lib/ansible/plugins/shell/sh.py (found_in_cache=True, class_only=False)
20536 1486646442.08509: in VariableManager get_vars()
20536 1486646442.08545: done with get_vars()
20536 1486646442.08559: Loading Connection 'network_cli' from /home/johnb/git/ansible-inc/ansible/lib/ansible/plugins/connection/network_cli.py (found_in_cache=True, class_only=True)
20536 1486646442.08599: Loading ActionModule 'network' from /home/johnb/git/ansible-inc/ansible/lib/ansible/plugins/action/network.py
20536 1486646442.08608: starting attempt loop
20536 1486646442.08612: running the handler
<ios01> closing connection
20536 1486646442.09082: done running TaskExecutor() for ios01/TASK: ios_facts : test getting all facts
20536 1486646442.09087: sending task result
20536 1486646442.09130: done sending task result
20536 1486646442.09140: WORKER PROCESS EXITING
20483 1486646442.09335: in VariableManager get_vars()
20483 1486646442.09553: done with get_vars()
20483 1486646442.09591: marking ios01 as failed
20483 1486646442.09609: marking host ios01 failed, current state: HOST STATE: block=4, task=2, rescue=0, always=0, run_state=ITERATING_TASKS, fail_state=FAILED_NONE, pending_setup=False, tasks child state? (HOST STATE: block=0, task=4, rescue=0, always=0, run_state=ITERATING_TASKS, fail_state=FAILED_NONE, pending_setup=False, tasks child state? (HOST STATE: block=0, task=2, rescue=0, always=0, run_state=ITERATING_TASKS, fail_state=FAILED_NONE, pending_setup=False, tasks child state? (None), rescue child state? (None), always child state? (None), did rescue? False, did start at task? False), rescue child state? (None), always child state? (None), did rescue? False, did start at task? False), rescue child state? (None), always child state? (None), did rescue? False, did start at task? False
20483 1486646442.09619: ^ failed state is now: HOST STATE: block=4, task=2, rescue=0, always=0, run_state=ITERATING_TASKS, fail_state=FAILED_NONE, pending_setup=False, tasks child state? (HOST STATE: block=0, task=4, rescue=0, always=0, run_state=ITERATING_TASKS, fail_state=FAILED_NONE, pending_setup=False, tasks child state? (HOST STATE: block=0, task=2, rescue=0, always=0, run_state=ITERATING_COMPLETE, fail_state=FAILED_TASKS, pending_setup=False, tasks child state? (None), rescue child state? (None), always child state? (None), did rescue? False, did start at task? False), rescue child state? (None), always child state? (None), did rescue? False, did start at task? False), rescue child state? (None), always child state? (None), did rescue? False, did start at task? False
20483 1486646442.09627: getting the next task for host ios01
20483 1486646442.09636: done getting next task for host ios01
20483 1486646442.09641:  ^ task is: None
20483 1486646442.09648:  ^ state is: HOST STATE: block=4, task=2, rescue=0, always=0, run_state=ITERATING_COMPLETE, fail_state=FAILED_TASKS, pending_setup=False, tasks child state? (None), rescue child state? (None), always child state? (None), did rescue? False, did start at task? False
An exception occurred during task execution. The full traceback is:
Traceback (most recent call last):
File "/home/johnb/git/ansible-inc/ansible/lib/ansible/executor/task_executor.py", line 126, in run
res = self._execute()
File "/home/johnb/git/ansible-inc/ansible/lib/ansible/executor/task_executor.py", line 504, in _execute
result = self._handler.run(task_vars=variables)
File "/home/johnb/git/ansible-inc/ansible/lib/ansible/plugins/action/network.py", line 43, in run
module.main()
File "/home/johnb/git/ansible-inc/ansible/lib/ansible/modules/network/ios/ios_facts.py", line 495, in main
inst.populate()
File "/home/johnb/git/ansible-inc/ansible/lib/ansible/modules/network/ios/ios_facts.py", line 238, in populate
super(Hardware, self).populate()
File "/home/johnb/git/ansible-inc/ansible/lib/ansible/modules/network/ios/ios_facts.py", line 185, in populate
self.responses = run_commands(self.module, self.COMMANDS, check_rc=False)
File "/home/johnb/git/ansible-inc/ansible/lib/ansible/modules/network/ios/ios_facts.py", line 165, in invoke
return func(*args, **kwargs)
File "/home/johnb/git/ansible-inc/ansible/lib/ansible/module_utils/ios.py", line 51, in run_commands
rc, out, err = module.exec_command(cmd)
File "/home/johnb/git/ansible-inc/ansible/lib/ansible/module_utils/local.py", line 59, in exec_command
rc, out, err = self.connection.exec_command(args)
File "/home/johnb/git/ansible-inc/ansible/lib/ansible/plugins/connection/network_cli.py", line 235, in exec_command
if obj['command'] == 'close_shell()':
TypeError: string indices must be integers

fatal: [ios01]: FAILED! => {
"failed": true,
"msg": "Unexpected failure during module execution.",
"stdout": ""
}
20483 1486646442.09842: no more pending results, returning what we have

# Fixed code
def run_commands(module, commands, check_rc=True):
    responses = list()
    commands = to_commands(to_list(commands))
    for cmd in commands:
        cmd = module.jsonify(cmd)
        rc, out, err = exec_command(module, cmd)
        if check_rc and rc != 0:
            module.fail_json(msg=err, rc=rc)
        responses.append(out)
    return responses