# Buggy code
def validate_feature(module, mode="show"):
    """Some features may need to be mapped due to inconsistency
    between how they appear from "show feature" output and
    how they are configured"""

    feature = module.params["feature"]
    info = get_capabilities(module).get("device_info", {})
    os_version = info.get("network_os_version", "")

    if "8.1" in os_version:
        feature_to_be_mapped = {
            "show": {
                "nv overlay": "nve",
                "vn-segment-vlan-based": "vnseg_vlan",
                "hsrp": "hsrp_engine",
                "fabric multicast": "fabric_mcast",
                "scp-server": "scpServer",
                "sftp-server": "sftpServer",
                "sla responder": "sla_responder",
                "sla sender": "sla_sender",
                "ssh": "sshServer",
                "tacacs+": "tacacs",
                "telnet": "telnetServer",
                "ethernet-link-oam": "elo",
            },
            "config": {
                "nve": "nv overlay",
                "vnseg_vlan": "vn-segment-vlan-based",
                "hsrp_engine": "hsrp",
                "fabric_mcast": "fabric multicast",
                "scpServer": "scp-server",
                "sftpServer": "sftp-server",
                "sla_sender": "sla sender",
                "sla_responder": "sla responder",
                "sshServer": "ssh",
                "tacacs": "tacacs+",
                "telnetServer": "telnet",
                "elo": "ethernet-link-oam",
            },
        }
    else:
        feature_to_be_mapped = {
            "show": {
                "nv overlay": "nve",
                "vn-segment-vlan-based": "vnseg_vlan",
                "hsrp": "hsrp_engine",
                "fabric multicast": "fabric_mcast",
                "scp-server": "scpServer",
                "sftp-server": "sftpServer",
                "sla responder": "sla_responder",
                "sla sender": "sla_sender",
                "ssh": "sshServer",
                "tacacs+": "tacacs",
                "telnet": "telnetServer",
                "ethernet-link-oam": "elo",
                "port-security": "eth_port_sec",
            },
            "config": {
                "nve": "nv overlay",
                "vnseg_vlan": "vn-segment-vlan-based",
                "hsrp_engine": "hsrp",
                "fabric_mcast": "fabric multicast",
                "scpServer": "scp-server",
                "sftpServer": "sftp-server",
                "sla_sender": "sla sender",
                "sla_responder": "sla responder",
                "sshServer": "ssh",
                "tacacs": "tacacs+",
                "telnetServer": "telnet",
                "elo": "ethernet-link-oam",
                "eth_port_sec": "port-security",
            },
        }

    if feature in feature_to_be_mapped[mode]:
        feature = feature_to_be_mapped[mode][feature]

    return feature


# Traceback (nansible.module_utils.connection.ConnectionError)
vagrant@acs:/etc/ansible$ ansible-playbook playbook_nxos_feature.yaml

PLAY [lab_nxos] *********************************************************************************************************************************************************************

TASK [GET CREDENTIALS] **************************************************************************************************************************************************************
ok: [ANS-SPINE2]

TASK [DEFINE CONNECTION] ************************************************************************************************************************************************************
ok: [ANS-SPINE2]

TASK [Show feature | grep vpc] ******************************************************************************************************************************************************
ok: [ANS-SPINE2]

TASK [debug] ************************************************************************************************************************************************************************
ok: [ANS-SPINE2] => {
"pre_check_show_feature.stdout_lines": [
[
"vpc                   1         enabled"
]
]
}

TASK [Feature enable / disable] *****************************************************************************************************************************************************
ANS-SPINE2#n occurred during task execution. To see the full traceback, use -vvv. The error was:
fatal: [ANS-SPINE2]: FAILED! => {"changed": false, "module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible__QRLaF/ansible_module_nxos_feature.py\", line 251, in <module>\n    main()\n  File \"/tmp/ansible__QRLaF/ansible_module_nxos_feature.py\", line 220, in main\n    feature = validate_feature(module)\n  File \"/tmp/ansible__QRLaF/ansible_module_nxos_feature.py\", line 133, in validate_feature\n    info = get_capabilities(module).get('device_info', {})\n  File \"/tmp/ansible__QRLaF/ansible_modlib.zip/ansible/module_utils/network/nxos/nxos.py\", line 474, in get_capabilities\n  File \"/tmp/ansible__QRLaF/ansible_modlib.zip/ansible/module_utils/network/nxos/nxos.py\", line 225, in get_capabilities\n  File \"/tmp/ansible__QRLaF/ansible_modlib.zip/ansible/module_utils/connection.py\", line 146, in __rpc__\nansible.module_utils.connection.ConnectionError: show version | json\r\r\n                           ^\r\n% Invalid command at '^' marker.\r\n\rANS-SPINE2# \n", "module_stdout": "", "msg": "MODULE FAILURE", "rc": 1}
[WARNING]: Could not create retry file '/etc/ansible/playbook_nxos_feature.retry'.         [Errno 13] Permission denied: u'/etc/ansible/playbook_nxos_feature.retry'


PLAY RECAP **************************************************************************************************************************************************************************
ANS-SPINE2                 : ok=4    changed=0    unreachable=0    failed=1

vagrant@acs:/etc/ansible$

# Fixed code
def validate_feature(module, mode="show"):
    """Some features may need to be mapped due to inconsistency
    between how they appear from "show feature" output and
    how they are configured"""

    feature = module.params["feature"]

    try:
        info = get_capabilities(module)
        device_info = info.get("device_info", {})
        os_version = device_info.get("network_os_version", "")
    except ConnectionError:
        os_version = ""

    if "8.1" in os_version:
        feature_to_be_mapped = {
            "show": {
                "nv overlay": "nve",
                "vn-segment-vlan-based": "vnseg_vlan",
                "hsrp": "hsrp_engine",
                "fabric multicast": "fabric_mcast",
                "scp-server": "scpServer",
                "sftp-server": "sftpServer",
                "sla responder": "sla_responder",
                "sla sender": "sla_sender",
                "ssh": "sshServer",
                "tacacs+": "tacacs",
                "telnet": "telnetServer",
                "ethernet-link-oam": "elo",
            },
            "config": {
                "nve": "nv overlay",
                "vnseg_vlan": "vn-segment-vlan-based",
                "hsrp_engine": "hsrp",
                "fabric_mcast": "fabric multicast",
                "scpServer": "scp-server",
                "sftpServer": "sftp-server",
                "sla_sender": "sla sender",
                "sla_responder": "sla responder",
                "sshServer": "ssh",
                "tacacs": "tacacs+",
                "telnetServer": "telnet",
                "elo": "ethernet-link-oam",
            },
        }
    else:
        feature_to_be_mapped = {
            "show": {
                "nv overlay": "nve",
                "vn-segment-vlan-based": "vnseg_vlan",
                "hsrp": "hsrp_engine",
                "fabric multicast": "fabric_mcast",
                "scp-server": "scpServer",
                "sftp-server": "sftpServer",
                "sla responder": "sla_responder",
                "sla sender": "sla_sender",
                "ssh": "sshServer",
                "tacacs+": "tacacs",
                "telnet": "telnetServer",
                "ethernet-link-oam": "elo",
                "port-security": "eth_port_sec",
            },
            "config": {
                "nve": "nv overlay",
                "vnseg_vlan": "vn-segment-vlan-based",
                "hsrp_engine": "hsrp",
                "fabric_mcast": "fabric multicast",
                "scpServer": "scp-server",
                "sftpServer": "sftp-server",
                "sla_sender": "sla sender",
                "sla_responder": "sla responder",
                "sshServer": "ssh",
                "tacacs": "tacacs+",
                "telnetServer": "telnet",
                "elo": "ethernet-link-oam",
                "eth_port_sec": "port-security",
            },
        }

    if feature in feature_to_be_mapped[mode]:
        feature = feature_to_be_mapped[mode][feature]

    return feature