# Buggy code
def get_value(arg, config):
    command = PARAM_TO_COMMAND_KEYMAP[arg]
    has_command = re.search(r"\s+{0}\s*$".format(command), config, re.M)
    has_command_value = re.search(
        r"(?:{0}\s)(?P<value>.*)$".format(command), config, re.M
    )

    if arg in BOOL_PARAMS:
        value = False
        try:
            if has_command:
                value = True
        except TypeError:
            value = False
    elif arg == "log_neighbor_changes":
        value = ""
        if has_command:
            if has_command_value:
                value = "disable"
            else:
                value = "enable"

    elif arg == "remove_private_as":
        value = "disable"
        if has_command:
            if has_command_value:
                value = has_command_value.group("value")
            else:
                value = "enable"
    else:
        value = ""

        if has_command_value:
            value = has_command_value.group("value")

            if command in ["timers", "password"]:
                split_value = value.split()
                value = ""

                if arg in ["timers_keepalive", "pwd_type"]:
                    value = split_value[0]
                elif arg in ["timers_holdtime", "pwd"] and len(split_value) == 2:
                    value = split_value[1]

    return value


# Traceback (KeyError)
PLAY [NXOS Sample Playbook] ***********************************************************************************************************************************************************************************************************************************

TASK [Ensure BGP Is Enabled] **********************************************************************************************************************************************************************************************************************************
ok: [nxxxx.cisco.com]

TASK [Configure BGP neighbor] *********************************************************************************************************************************************************************************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: KeyError: 'timers-keepalive'
fatal: [nxxxx.cisco.com]: FAILED! => {"changed": false, "failed": true, "module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_mwd6V4/ansible_module_nxos_bgp_neighbor.py\", line 508, in <module>\n    main()\n  File \"/tmp/ansible_mwd6V4/ansible_module_nxos_bgp_neighbor.py\", line 492, in main\n    state_present(module, existing, proposed, candidate)\n  File \"/tmp/ansible_mwd6V4/ansible_module_nxos_bgp_neighbor.py\", line 394, in state_present\n    proposed_commands['timers-keepalive'],\nKeyError: 'timers-keepalive'\n", "module_stdout": "", "msg": "MODULE FAILURE", "rc": 0}

# Fixed code
def get_value(arg, config):
    command = PARAM_TO_COMMAND_KEYMAP[arg]
    has_command = re.search(r"\s+{0}\s*$".format(command), config, re.M)
    has_command_value = re.search(
        r"(?:{0}\s)(?P<value>.*)$".format(command), config, re.M
    )

    if arg == "dynamic_capability":
        has_no_command = re.search(r"\s+no\s{0}\s*$".format(command), config, re.M)
        value = True
        if has_no_command:
            value = False
    elif arg in BOOL_PARAMS:
        value = False
        if has_command:
            value = True
    elif arg == "log_neighbor_changes":
        value = ""
        if has_command:
            if has_command_value:
                value = "disable"
            else:
                value = "enable"

    elif arg == "remove_private_as":
        value = "disable"
        if has_command:
            if has_command_value:
                value = has_command_value.group("value")
            else:
                value = "enable"
    else:
        value = ""

        if has_command_value:
            value = has_command_value.group("value")

            if command in ["timers", "password"]:
                split_value = value.split()
                value = ""

                if arg in ["timers_keepalive", "pwd_type"]:
                    value = split_value[0]
                elif arg in ["timers_holdtime", "pwd"] and len(split_value) == 2:
                    value = split_value[1]

    return value