# Buggy code
def __init__(self, module):
    """
    Constructor
    """
    if not HAS_REQUESTS:
        module.fail_json(
            msg=missing_required_lib("requests"), exception=REQUESTS_IMP_ERR
        )

    if not HAS_PYVMOMI:
        module.fail_json(msg=missing_required_lib("PyVmomi"), exception=PYVMOMI_IMP_ERR)

    self.module = module
    self.params = module.params
    self.si = None
    self.current_vm_obj = None
    self.content = connect_to_api(self.module)


# 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 __init__(self, module):
    """
    Constructor
    """
    if not HAS_REQUESTS:
        module.fail_json(
            msg=missing_required_lib("requests"), exception=REQUESTS_IMP_ERR
        )

    if not HAS_PYVMOMI:
        module.fail_json(msg=missing_required_lib("PyVmomi"), exception=PYVMOMI_IMP_ERR)

    self.module = module
    self.params = module.params
    self.si = None
    self.current_vm_obj = None
    self.content = connect_to_api(self.module)
    self.custom_field_mgr = []
    if self.content.customFieldsManager:  # not an ESXi
        self.custom_field_mgr = self.content.customFieldsManager.field