# Buggy code
def get_all_virtual_machines(self):
    """
    Function to get all virtual machines and related configurations information
    """
    virtual_machines = get_all_objs(self.content, [vim.VirtualMachine])
    _virtual_machines = {}

    for vm in virtual_machines:
        _ip_address = ""
        summary = vm.summary
        if summary.guest is not None:
            _ip_address = summary.guest.ipAddress
            if _ip_address is None:
                _ip_address = ""
        _mac_address = []
        all_devices = _get_vm_prop(vm, ("config", "hardware", "device"))
        if all_devices:
            for dev in all_devices:
                if isinstance(dev, vim.vm.device.VirtualEthernetCard):
                    _mac_address.append(dev.macAddress)

        net_dict = {}
        vmnet = _get_vm_prop(vm, ("guest", "net"))
        if vmnet:
            for device in vmnet:
                net_dict[device.macAddress] = dict()
                net_dict[device.macAddress]["ipv4"] = []
                net_dict[device.macAddress]["ipv6"] = []
                for ip_addr in device.ipAddress:
                    if "::" in ip_addr:
                        net_dict[device.macAddress]["ipv6"].append(ip_addr)
                    else:
                        net_dict[device.macAddress]["ipv4"].append(ip_addr)

        esxi_hostname = None
        if summary.runtime.host:
            esxi_hostname = summary.runtime.host.summary.config.name

        virtual_machine = {
            summary.config.name: {
                "guest_fullname": summary.config.guestFullName,
                "power_state": summary.runtime.powerState,
                "ip_address": _ip_address,  # Kept for backward compatibility
                "mac_address": _mac_address,  # Kept for backward compatibility
                "uuid": summary.config.uuid,
                "vm_network": net_dict,
                "esxi_hostname": esxi_hostname,
            }
        }

        vm_type = self.module.params.get("vm_type")
        if vm_type == "vm" and vm.config.template is False:
            _virtual_machines.update(virtual_machine)
        elif vm_type == "template" and vm.config.template:
            _virtual_machines.update(virtual_machine)
        elif vm_type == "all":
            _virtual_machines.update(virtual_machine)
    return _virtual_machines


# Traceback (AttributeError)
[admin@localhost testvmware]$ ansible-playbook -vvv test6.yml
ansible-playbook 2.5.2
config file = /home/admin/git/testvmware/ansible.cfg
configured module search path = [u'/home/admin/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /bin/ansible-playbook
python version = 2.7.5 (default, Aug  4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]
Using /home/admin/git/testvmware/ansible.cfg as config file
Parsed /etc/ansible/hosts inventory source with ini plugin
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'


PLAYBOOK: test6.yml ***************************************************************************************************************************************************************************************
1 plays in test6.yml

PLAY [localhost] ******************************************************************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************************************************************************
task path: /home/admin/git/testvmware/test6.yml:3
Using module file /usr/lib/python2.7/site-packages/ansible/modules/system/setup.py
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: admin
<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 /home/admin/.ansible/tmp/ansible-tmp-1530110941.74-249538605709865 `" &amp;&amp; echo ansible-tmp-1530110941.74-249538605709865="` echo /home/admin/.ansible/tmp/ansible-tmp-1530110941.74-249538605709865 `" ) &amp;&amp; sleep 0'
<127.0.0.1> PUT /home/admin/.ansible/tmp/ansible-local-25391Nuwi_p/tmpyUC8cE TO /home/admin/.ansible/tmp/ansible-tmp-1530110941.74-249538605709865/setup.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /home/admin/.ansible/tmp/ansible-tmp-1530110941.74-249538605709865/ /home/admin/.ansible/tmp/ansible-tmp-1530110941.74-249538605709865/setup.py &amp;&amp; sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/usr/bin/python2 /home/admin/.ansible/tmp/ansible-tmp-1530110941.74-249538605709865/setup.py &amp;&amp; sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /home/admin/.ansible/tmp/ansible-tmp-1530110941.74-249538605709865/ > /dev/null 2>&amp;1 &amp;&amp; sleep 0'
ok: [localhost]
META: ran handlers

TASK [Gather only registered virtual machine templates] ***************************************************************************************************************************************************
task path: /home/admin/git/testvmware/test6.yml:14
Using module file /usr/lib/python2.7/site-packages/ansible/modules/cloud/vmware/vmware_vm_facts.py
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: admin
<localhost> EXEC /bin/sh -c 'echo ~ &amp;&amp; sleep 0'
<localhost> EXEC /bin/sh -c '( umask 77 &amp;&amp; mkdir -p "` echo /home/admin/.ansible/tmp/ansible-tmp-1530110943.07-1048959467281 `" &amp;&amp; echo ansible-tmp-1530110943.07-1048959467281="` echo /home/admin/.ansible/tmp/ansible-tmp-1530110943.07-1048959467281 `" ) &amp;&amp; sleep 0'
<localhost> PUT /home/admin/.ansible/tmp/ansible-local-25391Nuwi_p/tmpEumyzc TO /home/admin/.ansible/tmp/ansible-tmp-1530110943.07-1048959467281/vmware_vm_facts.py
<localhost> EXEC /bin/sh -c 'chmod u+x /home/admin/.ansible/tmp/ansible-tmp-1530110943.07-1048959467281/ /home/admin/.ansible/tmp/ansible-tmp-1530110943.07-1048959467281/vmware_vm_facts.py &amp;&amp; sleep 0'
<localhost> EXEC /bin/sh -c '/usr/bin/python2 /home/admin/.ansible/tmp/ansible-tmp-1530110943.07-1048959467281/vmware_vm_facts.py &amp;&amp; sleep 0'
<localhost> EXEC /bin/sh -c 'rm -f -r /home/admin/.ansible/tmp/ansible-tmp-1530110943.07-1048959467281/ > /dev/null 2>&amp;1 &amp;&amp; sleep 0'
The full traceback is:
Traceback (most recent call last):
File "/tmp/ansible_gPTlZM/ansible_module_vmware_vm_facts.py", line 178, in <module>
main()
File "/tmp/ansible_gPTlZM/ansible_module_vmware_vm_facts.py", line 172, in main
_virtual_machines = vmware_vm_facts.get_all_virtual_machines()
File "/tmp/ansible_gPTlZM/ansible_module_vmware_vm_facts.py", line 156, in get_all_virtual_machines
elif vm_type == 'template' and vm.config.template:
AttributeError: 'NoneType' object has no attribute 'template'

fatal: [localhost -> localhost]: FAILED! => {
"changed": false,
"module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_gPTlZM/ansible_module_vmware_vm_facts.py\", line 178, in <module>\n    main()\n  File \"/tmp/ansible_gPTlZM/ansible_module_vmware_vm_facts.py\", line 172, in main\n    _virtual_machines = vmware_vm_facts.get_all_virtual_machines()\n  File \"/tmp/ansible_gPTlZM/ansible_module_vmware_vm_facts.py\", line 156, in get_all_virtual_machines\n    elif vm_type == 'template' and vm.config.template:\nAttributeError: 'NoneType' object has no attribute 'template'\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 1
}
to retry, use: --limit @/home/admin/git/testvmware/test6.retry

PLAY RECAP ************************************************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=1

# Fixed code
def get_all_virtual_machines(self):
    """
    Function to get all virtual machines and related configurations information
    """
    virtual_machines = get_all_objs(self.content, [vim.VirtualMachine])
    _virtual_machines = {}

    for vm in virtual_machines:
        _ip_address = ""
        summary = vm.summary
        if summary.guest is not None:
            _ip_address = summary.guest.ipAddress
            if _ip_address is None:
                _ip_address = ""
        _mac_address = []
        all_devices = _get_vm_prop(vm, ("config", "hardware", "device"))
        if all_devices:
            for dev in all_devices:
                if isinstance(dev, vim.vm.device.VirtualEthernetCard):
                    _mac_address.append(dev.macAddress)

        net_dict = {}
        vmnet = _get_vm_prop(vm, ("guest", "net"))
        if vmnet:
            for device in vmnet:
                net_dict[device.macAddress] = dict()
                net_dict[device.macAddress]["ipv4"] = []
                net_dict[device.macAddress]["ipv6"] = []
                for ip_addr in device.ipAddress:
                    if "::" in ip_addr:
                        net_dict[device.macAddress]["ipv6"].append(ip_addr)
                    else:
                        net_dict[device.macAddress]["ipv4"].append(ip_addr)

        esxi_hostname = None
        if summary.runtime.host:
            esxi_hostname = summary.runtime.host.summary.config.name

        virtual_machine = {
            summary.config.name: {
                "guest_fullname": summary.config.guestFullName,
                "power_state": summary.runtime.powerState,
                "ip_address": _ip_address,  # Kept for backward compatibility
                "mac_address": _mac_address,  # Kept for backward compatibility
                "uuid": summary.config.uuid,
                "vm_network": net_dict,
                "esxi_hostname": esxi_hostname,
            }
        }

        vm_type = self.module.params.get("vm_type")
        is_template = _get_vm_prop(vm, ("config", "template"))
        if vm_type == "vm" and not is_template:
            _virtual_machines.update(virtual_machine)
        elif vm_type == "template" and is_template:
            _virtual_machines.update(virtual_machine)
        elif vm_type == "all":
            _virtual_machines.update(virtual_machine)
    return _virtual_machines