# Buggy code
def state_present(module, existing, proposed, candidate):
    commands = list()
    proposed_commands = apply_key_map(PARAM_TO_COMMAND_KEYMAP, proposed)
    existing_commands = apply_key_map(PARAM_TO_COMMAND_KEYMAP, existing)

    for key, value in proposed_commands.items():
        if value is True:
            commands.append(key)
        elif value is False:
            commands.append("no {0}".format(key))
        elif value == "default":
            if existing_commands.get(key):
                existing_value = existing_commands.get(key)
                commands.append("no {0} {1}".format(key, existing_value))
        else:
            if key == "log-neighbor-changes":
                if value == "enable":
                    commands.append("{0}".format(key))
                elif value == "disable":
                    commands.append("{0} {1}".format(key, value))
                elif value == "inherit":
                    if existing_commands.get(key):
                        commands.append("no {0}".format(key))
            elif key == "password":
                pwd_type = module.params["pwd_type"]
                if pwd_type == "3des":
                    pwd_type = 3
                else:
                    pwd_type = 7
                command = "{0} {1} {2}".format(key, pwd_type, value)
                if command not in commands:
                    commands.append(command)
            elif key == "remove-private-as":
                if value == "enable":
                    command = "{0}".format(key)
                    commands.append(command)
                elif value == "disable":
                    if existing_commands.get(key) != "disable":
                        command = "no {0}".format(key)
                        commands.append(command)
                else:
                    command = "{0} {1}".format(key, value)
                    commands.append(command)
            elif key.startswith("timers"):
                command = "timers {0} {1}".format(
                    proposed_commands["timers-keepalive"],
                    proposed_commands["timers-holdtime"],
                )
                if command not in commands:
                    commands.append(command)
            else:
                command = "{0} {1}".format(key, value)
                commands.append(command)

    if commands:
        parents = ["router bgp {0}".format(module.params["asn"])]
        if module.params["vrf"] != "default":
            parents.append("vrf {0}".format(module.params["vrf"]))

        parents.append("neighbor {0}".format(module.params["neighbor"]))

        # make sure that local-as is the last command in the list.
        local_as_command = "local-as {0}".format(module.params["local_as"])
        if local_as_command in commands:
            commands.remove(local_as_command)
            commands.append(local_as_command)
        candidate.add(commands, parents=parents)


# 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 state_present(module, existing, proposed, candidate):
    commands = list()
    proposed_commands = apply_key_map(PARAM_TO_COMMAND_KEYMAP, proposed)
    existing_commands = apply_key_map(PARAM_TO_COMMAND_KEYMAP, existing)

    for key, value in proposed_commands.items():
        if value is True:
            commands.append(key)
        elif value is False:
            commands.append("no {0}".format(key))
        elif value == "default":
            if existing_commands.get(key):
                existing_value = existing_commands.get(key)
                commands.append("no {0} {1}".format(key, existing_value))
        else:
            if key == "log-neighbor-changes":
                if value == "enable":
                    commands.append("{0}".format(key))
                elif value == "disable":
                    commands.append("{0} {1}".format(key, value))
                elif value == "inherit":
                    if existing_commands.get(key):
                        commands.append("no {0}".format(key))
            elif key == "password":
                pwd_type = module.params["pwd_type"]
                if pwd_type == "3des":
                    pwd_type = 3
                else:
                    pwd_type = 7
                command = "{0} {1} {2}".format(key, pwd_type, value)
                if command not in commands:
                    commands.append(command)
            elif key == "remove-private-as":
                if value == "enable":
                    command = "{0}".format(key)
                    commands.append(command)
                elif value == "disable":
                    if existing_commands.get(key) != "disable":
                        command = "no {0}".format(key)
                        commands.append(command)
                else:
                    command = "{0} {1}".format(key, value)
                    commands.append(command)
            elif key == "timers":
                command = "timers {0} {1}".format(
                    proposed["timers_keepalive"], proposed["timers_holdtime"]
                )
                if command not in commands:
                    commands.append(command)
            else:
                command = "{0} {1}".format(key, value)
                commands.append(command)

    if commands:
        parents = ["router bgp {0}".format(module.params["asn"])]
        if module.params["vrf"] != "default":
            parents.append("vrf {0}".format(module.params["vrf"]))

        parents.append("neighbor {0}".format(module.params["neighbor"]))

        # make sure that local-as is the last command in the list.
        local_as_command = "local-as {0}".format(module.params["local_as"])
        if local_as_command in commands:
            commands.remove(local_as_command)
            commands.append(local_as_command)
        candidate.add(commands, parents=parents)