# Buggy code
def map_config_to_obj(module):
    objs = list()
    output = run_commands(module, ["show port-channel summary | json"])[0]
    if not output:
        return list()

    try:
        channels = output["TABLE_channel"]["ROW_channel"]
    except KeyError:
        return objs

    if channels:
        if isinstance(channels, list):
            for channel in channels:
                obj = parse_channel_options(module, output, channel)
                objs.append(obj)

        elif isinstance(channels, dict):
            obj = parse_channel_options(module, output, channels)
            objs.append(obj)

    return objs


# Traceback (TypeError)
TASK [nxos_linkagg : create linkagg] *****************************************************************************************************************************
task path: /root/agents-ci/ansible/test/integration/targets/nxos_linkagg/tests/common/sanity.yaml:39
<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-29626w5zfjt/ansible-tmp-1528990630.34-160148825984664 `" &amp;&amp; echo ansible-tmp-1528990630.34-160148825984664="` echo /root/.ansible/tmp/ansible-local-29626w5zfjt/ansible-tmp-1528990630.34-160148825984664 `" ) &amp;&amp; sleep 0'
Using module file /root/agents-ci/ansible/lib/ansible/modules/network/nxos/nxos_linkagg.py
<n9k.example.com> PUT /root/.ansible/tmp/ansible-local-29626w5zfjt/tmpKqKLUJ TO /root/.ansible/tmp/ansible-local-29626w5zfjt/ansible-tmp-1528990630.34-160148825984664/nxos_linkagg.py
<n9k.example.com> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-local-29626w5zfjt/ansible-tmp-1528990630.34-160148825984664/ /root/.ansible/tmp/ansible-local-29626w5zfjt/ansible-tmp-1528990630.34-160148825984664/nxos_linkagg.py &amp;&amp; sleep 0'
<n9k.example.com> EXEC /bin/sh -c '/root/p2/bin/python /root/.ansible/tmp/ansible-local-29626w5zfjt/ansible-tmp-1528990630.34-160148825984664/nxos_linkagg.py &amp;&amp; sleep 0'
<n9k.example.com> EXEC /bin/sh -c 'rm -f -r /root/.ansible/tmp/ansible-local-29626w5zfjt/ansible-tmp-1528990630.34-160148825984664/ > /dev/null 2>&amp;1 &amp;&amp; sleep 0'
The full traceback is:
Traceback (most recent call last):
File "/tmp/ansible_oiQISX/ansible_module_nxos_linkagg.py", line 398, in <module>
main()
File "/tmp/ansible_oiQISX/ansible_module_nxos_linkagg.py", line 385, in main
have = map_config_to_obj(module)
File "/tmp/ansible_oiQISX/ansible_module_nxos_linkagg.py", line 329, in map_config_to_obj
channels = output['TABLE_channel']['ROW_channel']
TypeError: string indices must be integers

fatal: [n9k.example.com]: FAILED! => {
"changed": false,
"module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_oiQISX/ansible_module_nxos_linkagg.py\", line 398, in <module>\n    main()\n  File \"/tmp/ansible_oiQISX/ansible_module_nxos_linkagg.py\", line 385, in main\n    have = map_config_to_obj(module)\n  File \"/tmp/ansible_oiQISX/ansible_module_nxos_linkagg.py\", line 329, in map_config_to_obj\n    channels = output['TABLE_channel']['ROW_channel']\nTypeError: string indices must be integers\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 1
}

# Fixed code
def map_config_to_obj(module):
    objs = list()
    output = run_commands(module, ["show port-channel summary | json"])[0]
    if not output:
        return list()

    try:
        channels = output["TABLE_channel"]["ROW_channel"]
    except (TypeError, KeyError):
        return objs

    if channels:
        if isinstance(channels, list):
            for channel in channels:
                obj = parse_channel_options(module, output, channel)
                objs.append(obj)

        elif isinstance(channels, dict):
            obj = parse_channel_options(module, output, channels)
            objs.append(obj)

    return objs