# Buggy code
def get_udld_global(module):
    command = "show udld global"
    udld_table = execute_show_command(command, module)[0]

    status = str(udld_table.get("udld-global-mode", None))
    if status == "enabled-aggressive":
        aggressive = "enabled"
    else:
        aggressive = "disabled"

    interval = str(udld_table.get("message-interval", None))
    udld = dict(msg_time=interval, aggressive=aggressive)

    return udld


# Traceback (AttributeError)
TASK [nxos_udld : Configure udld] ****************************************************************************************************************************************************************************************************************
task path: /root/agents-ci/ansible/test/integration/targets/nxos_udld/tests/common/sanity.yaml:18
<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/f0f0e4c317
<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-11514NqfDwc/ansible-tmp-1527874936.34-83926721175582 `" &amp;&amp; echo ansible-tmp-1527874936.34-83926721175582="` echo /root/.ansible/tmp/ansible-local-11514NqfDwc/ansible-tmp-1527874936.34-83926721175582 `" ) &amp;&amp; sleep 0'
Using module file /root/agents-ci/ansible/lib/ansible/modules/network/nxos/nxos_udld.py
<n9k.example.com> PUT /root/.ansible/tmp/ansible-local-11514NqfDwc/tmpo6Zn0h TO /root/.ansible/tmp/ansible-local-11514NqfDwc/ansible-tmp-1527874936.34-83926721175582/nxos_udld.py
<n9k.example.com> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-local-11514NqfDwc/ansible-tmp-1527874936.34-83926721175582/ /root/.ansible/tmp/ansible-local-11514NqfDwc/ansible-tmp-1527874936.34-83926721175582/nxos_udld.py &amp;&amp; sleep 0'
<n9k.example.com> EXEC /bin/sh -c '/usr/bin/python /root/.ansible/tmp/ansible-local-11514NqfDwc/ansible-tmp-1527874936.34-83926721175582/nxos_udld.py &amp;&amp; sleep 0'
<n9k.example.com> EXEC /bin/sh -c 'rm -f -r /root/.ansible/tmp/ansible-local-11514NqfDwc/ansible-tmp-1527874936.34-83926721175582/ > /dev/null 2>&amp;1 &amp;&amp; sleep 0'
The full traceback is:
Traceback (most recent call last):
File "/tmp/ansible_A0KRl3/ansible_module_nxos_udld.py", line 268, in <module>
main()
File "/tmp/ansible_A0KRl3/ansible_module_nxos_udld.py", line 228, in main
existing = get_udld_global(module)
File "/tmp/ansible_A0KRl3/ansible_module_nxos_udld.py", line 190, in get_udld_global
status = str(udld_table.get('udld-global-mode', None))
AttributeError: 'unicode' object has no attribute 'get'

fatal: [n9k.example.com]: FAILED! => {
"changed": false,
"module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_A0KRl3/ansible_module_nxos_udld.py\", line 268, in <module>\n    main()\n  File \"/tmp/ansible_A0KRl3/ansible_module_nxos_udld.py\", line 228, in main\n    existing = get_udld_global(module)\n  File \"/tmp/ansible_A0KRl3/ansible_module_nxos_udld.py\", line 190, in get_udld_global\n    status = str(udld_table.get('udld-global-mode', None))\nAttributeError: 'unicode' object has no attribute 'get'\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 1
}

# Fixed code
def get_udld_global(module):
    command = "show udld global | json"
    udld_table = run_commands(module, [command])[0]

    status = str(udld_table.get("udld-global-mode", None))
    if status == "enabled-aggressive":
        aggressive = "enabled"
    else:
        aggressive = "disabled"

    interval = str(udld_table.get("message-interval", None))
    udld = dict(msg_time=interval, aggressive=aggressive)

    return udld