# Buggy code
def get_hsrp_group(group, interface, module):
    command = "show hsrp group {0} all".format(group)
    hsrp = {}

    hsrp_key = {
        "sh_if_index": "interface",
        "sh_group_num": "group",
        "sh_group_version": "version",
        "sh_cfg_prio": "priority",
        "sh_preempt": "preempt",
        "sh_vip": "vip",
        "sh_authentication_type": "auth_type",
        "sh_keystring_attr": "auth_enc",
        "sh_authentication_data": "auth_string",
    }

    try:
        body = execute_show_command(command, module)[0]
        hsrp_table = body["TABLE_grp_detail"]["ROW_grp_detail"]
    except (AttributeError, IndexError, TypeError):
        return {}

    if isinstance(hsrp_table, dict):
        hsrp_table = [hsrp_table]

    for hsrp_group in hsrp_table:
        parsed_hsrp = apply_key_map(hsrp_key, hsrp_group)

        parsed_hsrp["interface"] = parsed_hsrp["interface"].lower()

        if parsed_hsrp["version"] == "v1":
            parsed_hsrp["version"] = "1"
        elif parsed_hsrp["version"] == "v2":
            parsed_hsrp["version"] = "2"

        if parsed_hsrp["auth_type"] == "md5":
            if parsed_hsrp["auth_enc"] == "hidden":
                parsed_hsrp["auth_enc"] = "7"
            else:
                parsed_hsrp["auth_enc"] = "0"

        if parsed_hsrp["interface"] == interface:
            return parsed_hsrp

    return hsrp


# 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 get_hsrp_group(group, interface, module):
    command = "show hsrp group {0} all | json".format(group)
    hsrp = {}

    hsrp_key = {
        "sh_if_index": "interface",
        "sh_group_num": "group",
        "sh_group_version": "version",
        "sh_cfg_prio": "priority",
        "sh_preempt": "preempt",
        "sh_vip": "vip",
        "sh_authentication_type": "auth_type",
        "sh_keystring_attr": "auth_enc",
        "sh_authentication_data": "auth_string",
    }

    try:
        body = run_commands(module, [command])[0]
        hsrp_table = body["TABLE_grp_detail"]["ROW_grp_detail"]
    except (AttributeError, IndexError, TypeError, KeyError):
        return {}

    if isinstance(hsrp_table, dict):
        hsrp_table = [hsrp_table]

    for hsrp_group in hsrp_table:
        parsed_hsrp = apply_key_map(hsrp_key, hsrp_group)

        parsed_hsrp["interface"] = parsed_hsrp["interface"].lower()

        if parsed_hsrp["version"] == "v1":
            parsed_hsrp["version"] = "1"
        elif parsed_hsrp["version"] == "v2":
            parsed_hsrp["version"] = "2"

        if parsed_hsrp["auth_type"] == "md5":
            if parsed_hsrp["auth_enc"] == "hidden":
                parsed_hsrp["auth_enc"] = "7"
            else:
                parsed_hsrp["auth_enc"] = "0"

        if parsed_hsrp["interface"] == interface:
            return parsed_hsrp

    return hsrp