# Buggy code
def run_commands(self, commands, check_rc=True):
    """Run list of commands on remote device and return results"""
    responses = list()

    for item in to_list(commands):
        if item["output"] == "json" and not is_json(item["command"]):
            cmd = "%s | json" % item["command"]
        elif item["output"] == "text" and is_json(item["command"]):
            cmd = item["command"].rsplit("|", 1)[0]
        else:
            cmd = item["command"]

        rc, out, err = self.exec_command(cmd)
        try:
            out = to_text(out, errors="surrogate_or_strict")
        except UnicodeError:
            self._module.fail_json(
                msg="Failed to decode output from %s: %s" % (cmd, to_text(out))
            )

        if check_rc and rc != 0:
            self._module.fail_json(msg=to_text(err))

        if not check_rc and rc != 0:
            try:
                out = self._module.from_json(err)
            except ValueError:
                out = to_text(err).strip()
        else:
            try:
                out = self._module.from_json(out)
            except ValueError:
                out = to_text(out).strip()

        responses.append(out)
    return responses


# 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 run_commands(self, commands, check_rc=True):
    """Run list of commands on remote device and return results"""
    responses = list()

    for item in to_list(commands):
        if item["output"] == "json" and not is_json(item["command"]):
            cmd = "%s | json" % item["command"]
        elif item["output"] == "text" and is_json(item["command"]):
            cmd = item["command"].rsplit("|", 1)[0]
        else:
            cmd = item["command"]

        rc, out, err = self.exec_command(cmd)
        try:
            out = to_text(out, errors="surrogate_or_strict")
        except UnicodeError:
            self._module.fail_json(
                msg="Failed to decode output from %s: %s" % (cmd, to_text(out))
            )

        if check_rc and rc != 0:
            self._module.fail_json(msg=to_text(err))

        if not check_rc and rc != 0:
            try:
                out = self._module.from_json(err)
            except ValueError:
                out = to_text(err).strip()
        else:
            try:
                out = self._module.from_json(out)
            except ValueError:
                out = to_text(out).strip()

        if item["output"] == "json" and isinstance(out, string_types):
            self._module.fail_json(
                msg="failed to retrieve output of %s in json format" % item["command"]
            )

        responses.append(out)
    return responses