# Buggy code
def gather_host_vmnic_facts(self):
    hosts_vmnic_facts = {}
    for host in self.hosts:
        host_vmnic_facts = dict(all=[], available=[], used=[])
        host_nw_system = host.configManager.networkSystem
        if host_nw_system:
            nw_config = host_nw_system.networkConfig
            host_vmnic_facts["all"] = [pnic.device for pnic in nw_config.pnic]

            vswitch_vmnics = []
            if nw_config.vswitch:
                for vswitch in nw_config.vswitch:
                    for vnic in vswitch.spec.bridge.nicDevice:
                        vswitch_vmnics.append(vnic)

            proxy_switch_vmnics = []
            if nw_config.vswitch:
                for proxy_config in nw_config.proxySwitch:
                    for proxy_nic in proxy_config.spec.bridge.nicDevice:
                        proxy_switch_vmnics.append(proxy_nic.pnicDevice)

            used_vmics = proxy_switch_vmnics + vswitch_vmnics
            host_vmnic_facts["used"] = used_vmics
            host_vmnic_facts["available"] = [
                pnic.device for pnic in nw_config.pnic if pnic.device not in used_vmics
            ]

        hosts_vmnic_facts[host.name] = host_vmnic_facts
    return hosts_vmnic_facts


# Traceback (AttributeError)
test-a01:ansible browne$ ansible -vvvv localhost -c local -m vmware_host_vmnic_facts -a "hostname=test-dev1.test.com username=root password=*** validate_certs=false esxi_hostname=test-dev1.test.com"
ansible 2.5.1
config file = None
configured module search path = [u'/Users/browne/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/ansible
executable location = /usr/local/bin/ansible
python version = 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 12:01:12) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]
No config file found; using defaults
setting up inventory plugins
[WARNING]: Unable to parse /etc/ansible/hosts as an inventory source

[WARNING]: No inventory was parsed, only implicit localhost is available

[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does
not match 'all'

Loading callback plugin minimal of type stdout, v2.0 from /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/ansible/plugins/callback/minimal.pyc
META: ran handlers
Using module file /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/ansible/modules/cloud/vmware/vmware_host_vmnic_facts.py
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: browne
<127.0.0.1> EXEC /bin/sh -c 'echo ~ &amp;&amp; sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 &amp;&amp; mkdir -p "` echo /Users/browne/.ansible/tmp/ansible-tmp-1533590044.29-60291700910997 `" &amp;&amp; echo ansible-tmp-1533590044.29-60291700910997="` echo /Users/browne/.ansible/tmp/ansible-tmp-1533590044.29-60291700910997 `" ) &amp;&amp; sleep 0'
<127.0.0.1> PUT /Users/browne/.ansible/tmp/ansible-local-77009lsSeG3/tmpCR7iZ5 TO /Users/browne/.ansible/tmp/ansible-tmp-1533590044.29-60291700910997/vmware_host_vmnic_facts.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /Users/browne/.ansible/tmp/ansible-tmp-1533590044.29-60291700910997/ /Users/browne/.ansible/tmp/ansible-tmp-1533590044.29-60291700910997/vmware_host_vmnic_facts.py &amp;&amp; sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python /Users/browne/.ansible/tmp/ansible-tmp-1533590044.29-60291700910997/vmware_host_vmnic_facts.py &amp;&amp; sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /Users/browne/.ansible/tmp/ansible-tmp-1533590044.29-60291700910997/ > /dev/null 2>&amp;1 &amp;&amp; sleep 0'
The full traceback is:
Traceback (most recent call last):
File "/var/folders/68/fjqs2pfn195_4z1jv1bpy1m80000gr/T/ansible_PIwNui/ansible_module_vmware_host_vmnic_facts.py", line 144, in <module>
main()
File "/var/folders/68/fjqs2pfn195_4z1jv1bpy1m80000gr/T/ansible_PIwNui/ansible_module_vmware_host_vmnic_facts.py", line 140, in main
module.exit_json(changed=False, hosts_vmnics_facts=host_vmnic_mgr.gather_host_vmnic_facts())
File "/var/folders/68/fjqs2pfn195_4z1jv1bpy1m80000gr/T/ansible_PIwNui/ansible_module_vmware_host_vmnic_facts.py", line 108, in gather_host_vmnic_facts
for vnic in vswitch.spec.bridge.nicDevice:
AttributeError: 'NoneType' object has no attribute 'nicDevice'

localhost | FAILED! => {
"changed": false,
"module_stderr": "Traceback (most recent call last):\n  File \"/var/folders/68/fjqs2pfn195_4z1jv1bpy1m80000gr/T/ansible_PIwNui/ansible_module_vmware_host_vmnic_facts.py\", line 144, in <module>\n    main()\n  File \"/var/folders/68/fjqs2pfn195_4z1jv1bpy1m80000gr/T/ansible_PIwNui/ansible_module_vmware_host_vmnic_facts.py\", line 140, in main\n    module.exit_json(changed=False, hosts_vmnics_facts=host_vmnic_mgr.gather_host_vmnic_facts())\n  File \"/var/folders/68/fjqs2pfn195_4z1jv1bpy1m80000gr/T/ansible_PIwNui/ansible_module_vmware_host_vmnic_facts.py\", line 108, in gather_host_vmnic_facts\n    for vnic in vswitch.spec.bridge.nicDevice:\nAttributeError: 'NoneType' object has no attribute 'nicDevice'\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 1
}

# Fixed code
def gather_host_vmnic_facts(self):
    hosts_vmnic_facts = {}
    for host in self.hosts:
        host_vmnic_facts = dict(all=[], available=[], used=[])
        host_nw_system = host.configManager.networkSystem
        if host_nw_system:
            nw_config = host_nw_system.networkConfig
            host_vmnic_facts["all"] = [pnic.device for pnic in nw_config.pnic]

            vswitch_vmnics = []
            proxy_switch_vmnics = []
            if nw_config.vswitch:
                for vswitch in nw_config.vswitch:
                    for vnic in vswitch.spec.bridge.nicDevice:
                        vswitch_vmnics.append(vnic)

            if nw_config.proxySwitch:
                for proxy_config in nw_config.proxySwitch:
                    for proxy_nic in proxy_config.spec.backing.pnicSpec:
                        proxy_switch_vmnics.append(proxy_nic.pnicDevice)

            used_vmics = proxy_switch_vmnics + vswitch_vmnics
            host_vmnic_facts["used"] = used_vmics
            host_vmnic_facts["available"] = [
                pnic.device for pnic in nw_config.pnic if pnic.device not in used_vmics
            ]

        hosts_vmnic_facts[host.name] = host_vmnic_facts
    return hosts_vmnic_facts