# Buggy code
def get_vtp_password(module):
    command = "show vtp password"
    body = execute_show_command(command, module)[0]
    password = body["passwd"]
    if password:
        return str(password)
    else:
        return ""


# Traceback (TypeError)
TASK [nxos_vtp_version : configure vtp version] ***********************************************************************************************************
task path: /root/agents-ci/ansible/test/integration/targets/nxos_vtp_version/tests/common/sanity.yaml:11
<n5k-xxx.example.com> connection transport is cli
<n5k-xxx.example.com> using connection plugin network_cli
<n5k-xxx.example.com> socket_path: /root/.ansible/pc/1b4e3e6315
Using module file /root/agents-ci/ansible/lib/ansible/modules/network/nxos/nxos_vtp_version.py
<n5k-xxx.example.com> ESTABLISH LOCAL CONNECTION FOR USER: root
<n5k-xxx.example.com> EXEC /bin/sh -c 'echo ~ &amp;&amp; sleep 0'
<n5k-xxx.example.com> EXEC /bin/sh -c '( umask 77 &amp;&amp; mkdir -p "` echo /root/.ansible/tmp/ansible-tmp-1506024091.06-54110608208998 `" &amp;&amp; echo ansible-tmp-1506024091.06-54110608208998="` echo /root/.ansible/tmp/ansible-tmp-1506024091.06-54110608208998 `" ) &amp;&amp; sleep 0'
<n5k-xxx.example.com> PUT /tmp/tmpeJZyoX TO /root/.ansible/tmp/ansible-tmp-1506024091.06-54110608208998/nxos_vtp_version.py
<n5k-xxx.example.com> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1506024091.06-54110608208998/ /root/.ansible/tmp/ansible-tmp-1506024091.06-54110608208998/nxos_vtp_version.py &amp;&amp; sleep 0'
<n5k-xxx.example.com> EXEC /bin/sh -c '/usr/bin/python /root/.ansible/tmp/ansible-tmp-1506024091.06-54110608208998/nxos_vtp_version.py; rm -rf "/root/.ansible/tmp/ansible-tmp-1506024091.06-54110608208998/" > /dev/null 2>&amp;1 &amp;&amp; sleep 0'
The full traceback is:
Traceback (most recent call last):
File "/tmp/ansible_ovCMww/ansible_module_nxos_vtp_version.py", line 208, in <module>
main()
File "/tmp/ansible_ovCMww/ansible_module_nxos_vtp_version.py", line 172, in main
existing = get_vtp_config(module)
File "/tmp/ansible_ovCMww/ansible_module_nxos_vtp_version.py", line 142, in get_vtp_config
vtp_parsed['vtp_password'] = get_vtp_password(module)
File "/tmp/ansible_ovCMww/ansible_module_nxos_vtp_version.py", line 150, in get_vtp_password
password = body['passwd']
TypeError: string indices must be integers, not str
FAILED! => {
"changed": false,
"failed": true,
"module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_ovCMww/ansible_module_nxos_vtp_version.py\", line 208, in <module>\n    main()\n  File \"/tmp/ansible_ovCMww/ansible_module_nxos_vtp_version.py\", line 172, in main\n    existing = get_vtp_config(module)\n  File \"/tmp/ansible_ovCMww/ansible_module_nxos_vtp_version.py\", line 142, in get_vtp_config\n    vtp_parsed['vtp_password'] = get_vtp_password(module)\n  File \"/tmp/ansible_ovCMww/ansible_module_nxos_vtp_version.py\", line 150, in get_vtp_password\n    password = body['passwd']\nTypeError: string indices must be integers, not str\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 0
}

# Fixed code
def get_vtp_password(module):
    command = "show vtp password"
    body = execute_show_command(command, module)[0]
    try:
        password = body["passwd"]
        if password:
            return str(password)
        else:
            return ""
    except TypeError:
        return ""