# Buggy code
def _populate_host_properties(self, vm_obj, current_host):
    # Load VM properties in host_vars
    vm_properties = [
        "name",
        "config.cpuHotAddEnabled",
        "config.cpuHotRemoveEnabled",
        "config.instanceUuid",
        "config.hardware.numCPU",
        "config.template",
        "config.name",
        "guest.hostName",
        "guest.ipAddress",
        "guest.guestId",
        "guest.guestState",
        "runtime.maxMemoryUsage",
        "customValue",
    ]
    field_mgr = self.pyv.content.customFieldsManager.field

    for vm_prop in vm_properties:
        if vm_prop == "customValue":
            for cust_value in vm_obj.obj.customValue:
                self.inventory.set_variable(
                    current_host,
                    [y.name for y in field_mgr if y.key == cust_value.key][0],
                    cust_value.value,
                )
        else:
            vm_value = self.pyv._get_object_prop(vm_obj.obj, vm_prop.split("."))
            self.inventory.set_variable(current_host, vm_prop, vm_value)


# Traceback (AttributeError)
task path: /ansible/esxi_facts.yml:24
<ixca-cm-esxi15.lbj.is.keysight.com> ESTABLISH LOCAL CONNECTION FOR USER: root
<ixca-cm-esxi15.lbj.is.keysight.com> EXEC /bin/sh -c 'echo ~root &amp;&amp; sleep 0'
<ixca-cm-esxi15.lbj.is.keysight.com> EXEC /bin/sh -c '( umask 77 &amp;&amp; mkdir -p "` echo /root/.ansible/tmp/ansible-tmp-1556918554.05-18123212362104 `" &amp;&amp; echo ansible-tmp-1556918554.05-18123212362104="` echo /root/.ansible/tmp/ansible-tmp-1556918554.05-18123212362104 `" ) &amp;&amp; sleep 0'
Using module file /ansible/lib/ansible/modules/cloud/vmware/vmware_vm_facts.py
<ixca-cm-esxi15.lbj.is.keysight.com> PUT /root/.ansible/tmp/ansible-local-21320DG41_n/tmpHfp0xu TO /root/.ansible/tmp/ansible-tmp-1556918554.05-18123212362104/AnsiballZ_vmware_vm_facts.py
<ixca-cm-esxi15.lbj.is.keysight.com> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1556918554.05-18123212362104/ /root/.ansible/tmp/ansible-tmp-1556918554.05-18123212362104/AnsiballZ_vmware_vm_facts.py &amp;&amp; sleep 0'
<ixca-cm-esxi15.lbj.is.keysight.com> EXEC /bin/sh -c '/usr/bin/python /root/.ansible/tmp/ansible-tmp-1556918554.05-18123212362104/AnsiballZ_vmware_vm_facts.py &amp;&amp; sleep 0'
<ixca-cm-esxi15.lbj.is.keysight.com> EXEC /bin/sh -c 'rm -f -r /root/.ansible/tmp/ansible-tmp-1556918554.05-18123212362104/ > /dev/null 2>&amp;1 &amp;&amp; sleep 0'
The full traceback is:
Traceback (most recent call last):
File "/root/.ansible/tmp/ansible-tmp-1556918554.05-18123212362104/AnsiballZ_vmware_vm_facts.py", line 125, in <module>
_ansiballz_main()
File "/root/.ansible/tmp/ansible-tmp-1556918554.05-18123212362104/AnsiballZ_vmware_vm_facts.py", line 117, in _ansiballz_main
invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
File "/root/.ansible/tmp/ansible-tmp-1556918554.05-18123212362104/AnsiballZ_vmware_vm_facts.py", line 54, in invoke_module
imp.load_module('__main__', mod, module, MOD_DESC)
File "/tmp/ansible_vmware_vm_facts_payload_8Q4A9E/__main__.py", line 251, in <module>
File "/tmp/ansible_vmware_vm_facts_payload_8Q4A9E/__main__.py", line 244, in main
File "/tmp/ansible_vmware_vm_facts_payload_8Q4A9E/__main__.py", line 153, in __init__
AttributeError: 'NoneType' object has no attribute 'field'

fatal: [ixca-cm-esxi15.lbj.is.keysight.com]: FAILED! => {
"changed": false,
"module_stderr": "Traceback (most recent call last):\n  File \"/root/.ansible/tmp/ansible-tmp-1556918554.05-18123212362104/AnsiballZ_vmware_vm_facts.py\", line 125, in <module>\n    _ansiballz_main()\n  File \"/root/.ansible/tmp/ansible-tmp-1556918554.05-18123212362104/AnsiballZ_vmware_vm_facts.py\", line 117, in _ansiballz_main\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n  File \"/root/.ansible/tmp/ansible-tmp-1556918554.05-18123212362104/AnsiballZ_vmware_vm_facts.py\", line 54, in invoke_module\n    imp.load_module('__main__', mod, module, MOD_DESC)\n  File \"/tmp/ansible_vmware_vm_facts_payload_8Q4A9E/__main__.py\", line 251, in <module>\n  File \"/tmp/ansible_vmware_vm_facts_payload_8Q4A9E/__main__.py\", line 244, in main\n  File \"/tmp/ansible_vmware_vm_facts_payload_8Q4A9E/__main__.py\", line 153, in __init__\nAttributeError: 'NoneType' object has no attribute 'field'\n",
"module_stdout": "",
"msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
"rc": 1
}

# Fixed code
def _populate_host_properties(self, vm_obj, current_host):
    # Load VM properties in host_vars
    vm_properties = [
        "name",
        "config.cpuHotAddEnabled",
        "config.cpuHotRemoveEnabled",
        "config.instanceUuid",
        "config.hardware.numCPU",
        "config.template",
        "config.name",
        "guest.hostName",
        "guest.ipAddress",
        "guest.guestId",
        "guest.guestState",
        "runtime.maxMemoryUsage",
        "customValue",
    ]
    field_mgr = []
    if self.content.customFieldsManager:
        field_mgr = self.pyv.content.customFieldsManager.field

    for vm_prop in vm_properties:
        if vm_prop == "customValue":
            for cust_value in vm_obj.obj.customValue:
                self.inventory.set_variable(
                    current_host,
                    [y.name for y in field_mgr if y.key == cust_value.key][0],
                    cust_value.value,
                )
        else:
            vm_value = self.pyv._get_object_prop(vm_obj.obj, vm_prop.split("."))
            self.inventory.set_variable(current_host, vm_prop, vm_value)