# Buggy code
def main():
    spec = dict(gather_subset=dict(default=["!config"], type="list"))

    spec.update(nxos_argument_spec)

    module = AnsibleModule(argument_spec=spec, supports_check_mode=True)

    warnings = list()
    check_args(module, warnings)

    gather_subset = module.params["gather_subset"]

    runable_subsets = set()
    exclude_subsets = set()

    for subset in gather_subset:
        if subset == "all":
            runable_subsets.update(VALID_SUBSETS)
            continue

        if subset.startswith("!"):
            subset = subset[1:]
            if subset == "all":
                exclude_subsets.update(VALID_SUBSETS)
                continue
            exclude = True
        else:
            exclude = False

        if subset not in VALID_SUBSETS:
            module.fail_json(msg="Bad subset")

        if exclude:
            exclude_subsets.add(subset)
        else:
            runable_subsets.add(subset)

    if not runable_subsets:
        runable_subsets.update(VALID_SUBSETS)

    runable_subsets.difference_update(exclude_subsets)
    runable_subsets.add("default")

    facts = dict()
    facts["gather_subset"] = list(runable_subsets)

    instances = list()
    for key in runable_subsets:
        instances.append(FACT_SUBSETS[key](module))

    for inst in instances:
        inst.populate()
        facts.update(inst.facts)
        warnings.extend(inst.warnings)

    ansible_facts = dict()
    for key, value in iteritems(facts):
        # this is to maintain capability with nxos_facts 2.1
        if key.startswith("_"):
            ansible_facts[key[1:]] = value
        else:
            key = "ansible_net_%s" % key
            ansible_facts[key] = value

    module.exit_json(ansible_facts=ansible_facts, warnings=warnings)


# Traceback (TypeError)
<192.168.1.1> attempting to start connection
<192.168.1.1> using connection plugin network_cli
<192.168.1.1> found existing local domain socket, using it!
<192.168.1.1> updating play_context for connection
<192.168.1.1>
<192.168.1.1> local domain socket path is /.ansible/pc/d894e2db50
Using module file /usr/lib/python2.7/dist-packages/ansible/modules/network/nxos/nxos_facts.py
<192.168.1.1> ESTABLISH LOCAL CONNECTION FOR USER: ataylor
<192.168.1.1> EXEC /bin/sh -c '( umask 77 &amp;&amp; mkdir -p "` echo /.ansible/tmp/ansible-local-1557eZYAor/ansible-tmp-1529632472.4-112453209332856 `" &amp;&amp; echo ansible-tmp-1529632472.4-112453209332856="` echo /.ansible/tmp/ansible-local-1557eZYAor/ansible-tmp-1529632472.4-112453209332856 `" ) &amp;&amp; sleep 0'
<192.168.1.1> PUT /.ansible/tmp/ansible-local-1557eZYAor/tmpDQvtIM TO /.ansible/tmp/ansible-local-1557eZYAor/ansible-tmp-1529632472.4-112453209332856/nxos_facts.py
<192.168.1.1> EXEC /bin/sh -c 'chmod u+x /.ansible/tmp/ansible-local-1557eZYAor/ansible-tmp-1529632472.4-112453209332856/ /.ansible/tmp/ansible-local-1557eZYAor/ansible-tmp-1529632472.4-112453209332856/nxos_facts.py &amp;&amp; sleep 0'
<192.168.1.1> EXEC /bin/sh -c '/usr/bin/python /.ansible/tmp/ansible-local-1557eZYAor/ansible-tmp-1529632472.4-112453209332856/nxos_facts.py &amp;&amp; sleep 0'
<192.168.1.1> EXEC /bin/sh -c 'rm -f -r /.ansible/tmp/ansible-local-1557eZYAor/ansible-tmp-1529632472.4-112453209332856/ > /dev/null 2>&amp;1 &amp;&amp; sleep 0'
The full traceback is:
Traceback (most recent call last):
File "/tmp/ansible_dt6HU3/ansible_module_nxos_facts.py", line 602, in <module>
main()
File "/tmp/ansible_dt6HU3/ansible_module_nxos_facts.py", line 585, in main
inst.populate()
File "/tmp/ansible_dt6HU3/ansible_module_nxos_facts.py", line 255, in populate
self.facts['memtotal_mb'] = int(data['memory_usage_total']) / 1024
TypeError: string indices must be integers

fatal: [192.168.1.1]: FAILED! => {
"changed": false,
"module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_dt6HU3/ansible_module_nxos_facts.py\", line 602, in <module>\n    main()\n  File \"/tmp/ansible_dt6HU3/ansible_module_nxos_facts.py\", line 585, in main\n    inst.populate()\n  File \"/tmp/ansible_dt6HU3/ansible_module_nxos_facts.py\", line 255, in populate\n    self.facts['memtotal_mb'] = int(data['memory_usage_total']) / 1024\nTypeError: string indices must be integers\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 1

# Fixed code
def main():
    spec = dict(gather_subset=dict(default=["!config"], type="list"))

    spec.update(nxos_argument_spec)

    module = AnsibleModule(argument_spec=spec, supports_check_mode=True)

    capabilities = get_capabilities(module)
    if capabilities:
        os_version = capabilities["device_info"]["network_os_version"]
        os_version_major = int(os_version[0])
        if os_version_major < 7 and "6.0(2)A8" not in os_version:
            module.fail_json(
                msg="this module requires JSON structured output support on the NX-OS device"
            )

    warnings = list()
    check_args(module, warnings)

    gather_subset = module.params["gather_subset"]

    runable_subsets = set()
    exclude_subsets = set()

    for subset in gather_subset:
        if subset == "all":
            runable_subsets.update(VALID_SUBSETS)
            continue

        if subset.startswith("!"):
            subset = subset[1:]
            if subset == "all":
                exclude_subsets.update(VALID_SUBSETS)
                continue
            exclude = True
        else:
            exclude = False

        if subset not in VALID_SUBSETS:
            module.fail_json(msg="Bad subset")

        if exclude:
            exclude_subsets.add(subset)
        else:
            runable_subsets.add(subset)

    if not runable_subsets:
        runable_subsets.update(VALID_SUBSETS)

    runable_subsets.difference_update(exclude_subsets)
    runable_subsets.add("default")

    facts = dict()
    facts["gather_subset"] = list(runable_subsets)

    instances = list()
    for key in runable_subsets:
        instances.append(FACT_SUBSETS[key](module))

    for inst in instances:
        inst.populate()
        facts.update(inst.facts)
        warnings.extend(inst.warnings)

    ansible_facts = dict()
    for key, value in iteritems(facts):
        # this is to maintain capability with nxos_facts 2.1
        if key.startswith("_"):
            ansible_facts[key[1:]] = value
        else:
            key = "ansible_net_%s" % key
            ansible_facts[key] = value

    module.exit_json(ansible_facts=ansible_facts, warnings=warnings)