# Buggy code
def get_ntp_peer(module):
    command = "show run | inc ntp.(server|peer)"
    ntp_peer_list = []
    ntp = execute_show_command(command, module, command_type="cli_show_ascii")[0]

    ntp_regex = (
        ".*ntp\s(server\s(?P<address>\S+)|peer\s(?P<peer_address>\S+))"
        "\s*((?P<prefer>prefer)\s*)?(use-vrf\s(?P<vrf_name>\S+)\s*)?"
        "(key\s(?P<key_id>\d+))?.*"
    )

    split_ntp = ntp.splitlines()
    for peer_line in split_ntp:
        ntp_peer = {}
        try:
            peer_address = None
            vrf_name = None
            prefer = None
            key_id = None
            match_ntp = re.match(ntp_regex, peer_line, re.DOTALL)
            group_ntp = match_ntp.groupdict()

            address = group_ntp["address"]
            peer_address = group_ntp["peer_address"]
            prefer = group_ntp["prefer"]
            vrf_name = group_ntp["vrf_name"]
            key_id = group_ntp["key_id"]

            if prefer is not None:
                prefer = "enabled"
            else:
                prefer = "disabled"

            if address is not None:
                peer_type = "server"
            elif peer_address is not None:
                peer_type = "peer"
                address = peer_address

            args = dict(
                peer_type=peer_type,
                address=address,
                prefer=prefer,
                vrf_name=vrf_name,
                key_id=key_id,
            )

            ntp_peer = dict((k, v) for k, v in args.items())
            ntp_peer_list.append(ntp_peer)
        except AttributeError:
            ntp_peer_list = []

    return ntp_peer_list


# Traceback (IndexError)
An exception occurred during task execution. The full traceback is:
Traceback (most recent call last):
File "/tmp/ansible_B58nks/ansible_module_nxos_ntp.py", line 635, in <module>
main()
File "/tmp/ansible_B58nks/ansible_module_nxos_ntp.py", line 578, in main
existing, peer_server_list = get_ntp_existing(address, peer_type, module)
File "/tmp/ansible_B58nks/ansible_module_nxos_ntp.py", line 454, in get_ntp_existing
peer_list = get_ntp_peer(module)
File "/tmp/ansible_B58nks/ansible_module_nxos_ntp.py", line 403, in get_ntp_peer
command, module, command_type='cli_show_ascii')[0]
IndexError: list index out of range

fatal: [192.168.2.11]: FAILED! => {
"changed": false,
"failed": true,
"invocation": {
"module_name": "nxos_ntp"
},
"module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_B58nks/ansible_module_nxos_ntp.py\", line 635, in <module>\n    main()\n  File \"/tmp/ansible_B58nks/ansible_module_nxos_ntp.py\", line 578, in main\n    existing, peer_server_list = get_ntp_existing(address, peer_type, module)\n  File \"/tmp/ansible_B58nks/ansible_module_nxos_ntp.py\", line 454, in get_ntp_existing\n    peer_list = get_ntp_peer(module)\n  File \"/tmp/ansible_B58nks/ansible_module_nxos_ntp.py\", line 403, in get_ntp_peer\n    command, module, command_type='cli_show_ascii')[0]\nIndexError: list index out of range\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 0

# Fixed code
def get_ntp_peer(module):
    command = "show run | inc ntp.(server|peer)"
    ntp_peer_list = []
    ntp = execute_show_command(command, module, command_type="cli_show_ascii")
    if ntp:
        ntp = ntp[0]

        ntp_regex = (
            ".*ntp\s(server\s(?P<address>\S+)|peer\s(?P<peer_address>\S+))"
            "\s*((?P<prefer>prefer)\s*)?(use-vrf\s(?P<vrf_name>\S+)\s*)?"
            "(key\s(?P<key_id>\d+))?.*"
        )

        split_ntp = ntp.splitlines()
        for peer_line in split_ntp:
            ntp_peer = {}
            try:
                peer_address = None
                vrf_name = None
                prefer = None
                key_id = None
                match_ntp = re.match(ntp_regex, peer_line, re.DOTALL)
                group_ntp = match_ntp.groupdict()

                address = group_ntp["address"]
                peer_address = group_ntp["peer_address"]
                prefer = group_ntp["prefer"]
                vrf_name = group_ntp["vrf_name"]
                key_id = group_ntp["key_id"]

                if prefer is not None:
                    prefer = "enabled"
                else:
                    prefer = "disabled"

                if address is not None:
                    peer_type = "server"
                elif peer_address is not None:
                    peer_type = "peer"
                    address = peer_address

                args = dict(
                    peer_type=peer_type,
                    address=address,
                    prefer=prefer,
                    vrf_name=vrf_name,
                    key_id=key_id,
                )

                ntp_peer = dict((k, v) for k, v in args.items())
                ntp_peer_list.append(ntp_peer)
            except AttributeError:
                ntp_peer_list = []

    return ntp_peer_list