# Buggy code
def get_interface(intf, module):
    """Gets current config/state of interface
    Args:
        intf (string): full name of interface, i.e. Ethernet1/1, loopback10,
            port-channel20, vlan20
    Returns:
      dictionary that has relevant config/state data about the given
          interface based on the type of interface it is
    """
    base_key_map = {
        "interface": "interface",
        "admin_state": "admin_state",
        "desc": "description",
    }
    mode_map = {"eth_mode": "mode"}
    loop_map = {"state": "admin_state"}
    svi_map = {"svi_admin_state": "admin_state", "desc": "description"}
    mode_value_map = {
        "mode": {
            "access": "layer2",
            "trunk": "layer2",
            "routed": "layer3",
            "layer3": "layer3",
        }
    }

    key_map = {}
    interface = {}

    command = "show interface {0}".format(intf)
    try:
        body = execute_show_command(command, module)[0]
    except IndexError:
        body = []
    if body:
        interface_table = body["TABLE_interface"]["ROW_interface"]
        if interface_table.get("eth_mode") == "fex-fabric":
            module.fail_json(
                msg='nxos_interface does not support interfaces with mode "fex-fabric"'
            )
        intf_type = get_interface_type(intf)
        if intf_type in ["portchannel", "ethernet"]:
            if not interface_table.get("eth_mode"):
                interface_table["eth_mode"] = "layer3"

        if intf_type == "ethernet":
            key_map.update(base_key_map)
            key_map.update(mode_map)
            temp_dict = apply_key_map(key_map, interface_table)
            temp_dict = apply_value_map(mode_value_map, temp_dict)
            interface.update(temp_dict)

        elif intf_type == "svi":
            key_map.update(svi_map)
            temp_dict = apply_key_map(key_map, interface_table)
            interface.update(temp_dict)
            attributes = get_manual_interface_attributes(intf, module)
            interface["admin_state"] = str(attributes.get("admin_state", "nxapibug"))
            interface["description"] = str(attributes.get("description", "nxapi_bug"))
            command = "show run interface {0}".format(intf)
            body = execute_show_command(command, module)[0]
            if "ip forward" in body:
                interface["ip_forward"] = "enable"
            else:
                interface["ip_forward"] = "disable"
            if "fabric forwarding mode anycast-gateway" in body:
                interface["fabric_forwarding_anycast_gateway"] = True
            else:
                interface["fabric_forwarding_anycast_gateway"] = False

        elif intf_type == "loopback":
            key_map.update(base_key_map)
            key_map.pop("admin_state")
            key_map.update(loop_map)
            temp_dict = apply_key_map(key_map, interface_table)
            if not temp_dict.get("description"):
                temp_dict["description"] = "None"
            interface.update(temp_dict)

        elif intf_type == "management":
            key_map.update(base_key_map)
            temp_dict = apply_key_map(key_map, interface_table)
            interface.update(temp_dict)

        elif intf_type == "portchannel":
            key_map.update(base_key_map)
            key_map.update(mode_map)
            temp_dict = apply_key_map(key_map, interface_table)
            temp_dict = apply_value_map(mode_value_map, temp_dict)
            if not temp_dict.get("description"):
                temp_dict["description"] = "None"
            interface.update(temp_dict)

        elif intf_type == "nve":
            key_map.update(base_key_map)
            temp_dict = apply_key_map(key_map, interface_table)
            if not temp_dict.get("description"):
                temp_dict["description"] = "None"
            interface.update(temp_dict)

    return interface


# Traceback (TypeError)
TASK [Ensure all layer 2 interfaces are up] ***********************************************************************************************************
Tuesday 24 October 2017  10:54:15 +0000 (0:00:21.378)       0:01:00.450 *******
changed: [fbs0-b505-10] => (item={u'interface': u'Ethernet1/1', u'description': u'to infra0-1-b505-10'})
changed: [fbs0-b505-9] => (item={u'interface': u'Ethernet1/1', u'description': u'to infra0-1-b505-10'})
changed: [fbs0-b505-9] => (item={u'interface': u'Ethernet1/2', u'description': u'to infra0-2-b505-10'})
changed: [fbs0-b505-10] => (item={u'interface': u'Ethernet1/2', u'description': u'to infra0-2-b505-10'})
changed: [fbs0-b505-10] => (item={u'interface': u'Ethernet1/3', u'description': u'to infra0-3-b505-10'})
changed: [fbs0-b505-9] => (item={u'interface': u'Ethernet1/3', u'description': u'to infra0-3-b505-10'})
changed: [fbs0-b505-10] => (item={u'interface': u'Ethernet1/4', u'description': u'to infra0-4-b505-10'})
changed: [fbs0-b505-9] => (item={u'interface': u'Ethernet1/4', u'description': u'to infra0-4-b505-10'})
changed: [fbs0-b505-10] => (item={u'interface': u'Ethernet1/5', u'description': u'to infra0-5-b505-10'})
changed: [fbs0-b505-9] => (item={u'interface': u'Ethernet1/5', u'description': u'to infra0-5-b505-10'})
changed: [fbs0-b505-10] => (item={u'interface': u'Ethernet1/6', u'description': u'to infra0-6-b505-10'})
changed: [fbs0-b505-9] => (item={u'interface': u'Ethernet1/6', u'description': u'to infra0-6-b505-10'})
changed: [fbs0-b505-10] => (item={u'interface': u'Ethernet1/7', u'description': u'to infra0-7-b505-10'})
changed: [fbs0-b505-9] => (item={u'interface': u'Ethernet1/7', u'description': u'to infra0-7-b505-10'})
changed: [fbs0-b505-10] => (item={u'interface': u'Ethernet1/8', u'description': u'to infra0-8-b505-10'})
changed: [fbs0-b505-9] => (item={u'interface': u'Ethernet1/8', u'description': u'to infra0-8-b505-10'})
changed: [fbs0-b505-10] => (item={u'interface': u'Ethernet1/9', u'description': u'to infra0-1-b505-9'})
changed: [fbs0-b505-9] => (item={u'interface': u'Ethernet1/9', u'description': u'to infra0-1-b505-9'})
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: TypeError: string indices must be integers, not str
failed: [fbs0-b505-10] (item={u'interface': u'Ethernet1/10', u'description': u'to infra0-2-b505-9'}) => {"changed": false, "failed": true, "item": {"description": "to infra0-2-b505-9", "interface": "Ethernet1/10"}, "module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_SPjZ0l/ansible_module_nxos_interface.py\", line 710, in <module>\n    main()\n  File \"/tmp/ansible_SPjZ0l/ansible_module_nxos_interface.py\", line 701, in main\n    normalized_interface)\n  File \"/tmp/ansible_SPjZ0l/ansible_module_nxos_interface.py\", line 534, in smart_existing\n    existing = get_interface(normalized_interface, module)\n  File \"/tmp/ansible_SPjZ0l/ansible_module_nxos_interface.py\", line 281, in get_interface\n    interface_table = body['TABLE_interface']['ROW_interface']\nTypeError: string indices must be integers, not str\n", "module_stdout": "", "msg": "MODULE FAILURE", "rc": 0}
changed: [fbs0-b505-9] => (item={u'interface': u'Ethernet1/10', u'description': u'to infra0-2-b505-9'})
changed: [fbs0-b505-10] => (item={u'interface': u'Ethernet1/11', u'description': u'to infra0-3-b505-9'})
changed: [fbs0-b505-9] => (item={u'interface': u'Ethernet1/11', u'description': u'to infra0-3-b505-9'})
changed: [fbs0-b505-10] => (item={u'interface': u'Ethernet1/12', u'description': u'to infra0-4-b505-9'})
changed: [fbs0-b505-9] => (item={u'interface': u'Ethernet1/12', u'description': u'to infra0-4-b505-9'})
changed: [fbs0-b505-10] => (item={u'interface': u'Ethernet1/13', u'description': u'to infra0-5-b505-9'})
changed: [fbs0-b505-9] => (item={u'interface': u'Ethernet1/13', u'description': u'to infra0-5-b505-9'})
changed: [fbs0-b505-10] => (item={u'interface': u'Ethernet1/14', u'description': u'to infra0-6-b505-9'})
changed: [fbs0-b505-9] => (item={u'interface': u'Ethernet1/14', u'description': u'to infra0-6-b505-9'})
changed: [fbs0-b505-10] => (item={u'interface': u'Ethernet1/15', u'description': u'to infra0-7-b505-9'})
changed: [fbs0-b505-9] => (item={u'interface': u'Ethernet1/15', u'description': u'to infra0-7-b505-9'})
changed: [fbs0-b505-10] => (item={u'interface': u'Ethernet1/16', u'description': u'to infra0-8-b505-9'})
changed: [fbs0-b505-9] => (item={u'interface': u'Ethernet1/16', u'description': u'to infra0-8-b505-9'})
ok: [fbs0-b505-10] => (item={u'interface': u'Ethernet1/47', u'stp_port_type': u'network', u'description': u'vpc peer link'})
ok: [fbs0-b505-9] => (item={u'interface': u'Ethernet1/47', u'stp_port_type': u'network', u'description': u'vpc peer link'})
ok: [fbs0-b505-10] => (item={u'interface': u'Ethernet1/48', u'stp_port_type': u'network', u'description': u'vpc peer link'})
ok: [fbs0-b505-9] => (item={u'interface': u'Ethernet1/48', u'stp_port_type': u'network', u'description': u'vpc peer link'})


TASK [Ensure vrrpv3 is applied for vlans that need it] ************************************************************************************************
Tuesday 24 October 2017  11:01:48 +0000 (0:00:11.191)       0:08:33.606 *******
skipping: [fbs0-b505-9] => (item={u'vrf': u'default', u'vlan_id': 999})
ok: [fbs0-b505-9] => (item={u'vrrpv3': {u'priority': u'102', u'address_family': u'ipv4', u'group_id': 23, u'description': u'storage-clients', u'address': u'10.23.128.5'}, u'vrf': u'STORAGE', u'address': u'10.23.128.1/24', u'interface': u'Vlan1923', u'extra_lines': [u'mtu 9216'], u'vlan_id': 1923})
ok: [fbs0-b505-9] => (item={u'vrrpv3': {u'priority': u'102', u'address_family': u'ipv4', u'group_id': 21, u'description': u'storage-services', u'address': u'10.21.128.5'}, u'vrf': u'STORAGE', u'address': u'10.21.128.1/24', u'interface': u'Vlan1921', u'extra_lines': [u'mtu 9216'], u'vlan_id': 1921})
ok: [fbs0-b505-9] => (item={u'interface': u'Vlan1911', u'vrrpv3': {u'priority': u'102', u'address_family': u'ipv4', u'group_id': 11, u'description': u'osmgmt', u'address': u'10.11.128.5'}, u'vrf': u'OSMGMT', u'vlan_id': 1911, u'address': u'10.11.128.1/24'})
ok: [fbs0-b505-9] => (item={u'interface': u'Vlan1931', u'vrrpv3': {u'priority': u'102', u'address_family': u'ipv4', u'group_id': 31, u'description': u'metal', u'address': u'10.31.128.5'}, u'vrf': u'METAL', u'vlan_id': 1931, u'address': u'10.31.128.1/24'})
ok: [fbs0-b505-9] => (item={u'interface': u'Vlan1932', u'vrrpv3': {u'priority': u'102', u'address_family': u'ipv4', u'group_id': 32, u'description': u'metal', u'address': u'10.32.128.5'}, u'vrf': u'METAL', u'vlan_id': 1932, u'address': u'10.32.128.1/24'})
failed: [fbs0-b505-9] (item={u'interface': u'Vlan1941', u'vrrpv3': {u'priority': u'102', u'address_family': u'ipv4', u'group_id': 41, u'description': u'tunnels', u'address': u'10.41.128.5'}, u'vrf': u'TUNNEL', u'vlan_id': 1941, u'address': u'10.41.128.1/24'}) => {"changed": false, "failed": true, "item": {"address": "10.41.128.1/24", "interface": "Vlan1941", "vlan_id": 1941, "vrf": "TUNNEL", "vrrpv3": {"address": "10.41.128.5", "address_family": "ipv4", "description": "tunnels", "group_id": 41, "priority": "102"}}, "msg": "interface Vlan1941\r\r\n               ^\r\n% Invalid command at '^' marker.\r\n\rfbs0-b505-9# "}

# Fixed code
def get_interface(intf, module):
    """Gets current config/state of interface
    Args:
        intf (string): full name of interface, i.e. Ethernet1/1, loopback10,
            port-channel20, vlan20
    Returns:
      dictionary that has relevant config/state data about the given
          interface based on the type of interface it is
    """
    base_key_map = {
        "interface": "interface",
        "admin_state": "admin_state",
        "desc": "description",
    }
    mode_map = {"eth_mode": "mode"}
    loop_map = {"state": "admin_state"}
    svi_map = {"svi_admin_state": "admin_state", "desc": "description"}
    mode_value_map = {
        "mode": {
            "access": "layer2",
            "trunk": "layer2",
            "routed": "layer3",
            "layer3": "layer3",
        }
    }

    key_map = {}
    interface = {}

    command = "show interface {0}".format(intf)
    try:
        body = execute_show_command(command, module)[0]
    except IndexError:
        return {}
    if body:
        try:
            interface_table = body["TABLE_interface"]["ROW_interface"]
        except KeyError:
            return {}
        if interface_table:
            if interface_table.get("eth_mode") == "fex-fabric":
                module.fail_json(
                    msg='nxos_interface does not support interfaces with mode "fex-fabric"'
                )
            intf_type = get_interface_type(intf)
            if intf_type in ["portchannel", "ethernet"]:
                if not interface_table.get("eth_mode"):
                    interface_table["eth_mode"] = "layer3"

            if intf_type == "ethernet":
                key_map.update(base_key_map)
                key_map.update(mode_map)
                temp_dict = apply_key_map(key_map, interface_table)
                temp_dict = apply_value_map(mode_value_map, temp_dict)
                interface.update(temp_dict)

            elif intf_type == "svi":
                key_map.update(svi_map)
                temp_dict = apply_key_map(key_map, interface_table)
                interface.update(temp_dict)
                attributes = get_manual_interface_attributes(intf, module)
                interface["admin_state"] = str(
                    attributes.get("admin_state", "nxapibug")
                )
                interface["description"] = str(
                    attributes.get("description", "nxapi_bug")
                )
                command = "show run interface {0}".format(intf)
                body = execute_show_command(command, module)[0]
                if "ip forward" in body:
                    interface["ip_forward"] = "enable"
                else:
                    interface["ip_forward"] = "disable"
                if "fabric forwarding mode anycast-gateway" in body:
                    interface["fabric_forwarding_anycast_gateway"] = True
                else:
                    interface["fabric_forwarding_anycast_gateway"] = False

            elif intf_type == "loopback":
                key_map.update(base_key_map)
                key_map.pop("admin_state")
                key_map.update(loop_map)
                temp_dict = apply_key_map(key_map, interface_table)
                if not temp_dict.get("description"):
                    temp_dict["description"] = "None"
                interface.update(temp_dict)

            elif intf_type == "management":
                key_map.update(base_key_map)
                temp_dict = apply_key_map(key_map, interface_table)
                interface.update(temp_dict)

            elif intf_type == "portchannel":
                key_map.update(base_key_map)
                key_map.update(mode_map)
                temp_dict = apply_key_map(key_map, interface_table)
                temp_dict = apply_value_map(mode_value_map, temp_dict)
                if not temp_dict.get("description"):
                    temp_dict["description"] = "None"
                interface.update(temp_dict)

            elif intf_type == "nve":
                key_map.update(base_key_map)
                temp_dict = apply_key_map(key_map, interface_table)
                if not temp_dict.get("description"):
                    temp_dict["description"] = "None"
                interface.update(temp_dict)

    return interface