# Buggy code
def compile_folder_path_for_object(vobj):
    """make a /vm/foo/bar/baz like folder path for an object"""

    paths = []
    if isinstance(vobj, vim.Folder):
        paths.append(vobj.name)

    thisobj = vobj
    while hasattr(thisobj, "parent"):
        thisobj = thisobj.parent
        if thisobj.name == "Datacenters":
            break
        if isinstance(thisobj, vim.Folder):
            paths.append(thisobj.name)
    paths.reverse()
    return "/" + "/".join(paths)


# Traceback (AttributeError)
TASK [create the vm] ******************************************************************************************************************************
task path: /home/e003120/t1.yml:9
Using module file //home/e003120/ansible/ansible/lib/ansible/modules/cloud/vmware/vmware_guest.py
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: e003120
<localhost> EXEC /bin/sh -c 'echo ~ &amp;&amp; sleep 0'
<localhost> EXEC /bin/sh -c '( umask 77 &amp;&amp; mkdir -p "` echo /home/e003120/.ansible/tmp/ansible-tmp-1504707292.19-259378902513195 `" &amp;&amp; echo ansible-tmp-1504707292.19-259378902513195="` echo /home/e003120/.ansible/tmp/ansible-tmp-1504707292.19-259378902513195 `" ) &amp;&amp; sleep 0'
<localhost> PUT /tmp/tmpDCLzu5 TO /home/e003120/.ansible/tmp/ansible-tmp-1504707292.19-259378902513195/vmware_guest.py
<localhost> EXEC /bin/sh -c 'chmod u+x /home/e003120/.ansible/tmp/ansible-tmp-1504707292.19-259378902513195/ /home/e003120/.ansible/tmp/ansible-tmp-1504707292.19-259378902513195/vmware_guest.py &amp;&amp; sleep 0'
<localhost> EXEC /bin/sh -c '/usr/bin/python /home/e003120/.ansible/tmp/ansible-tmp-1504707292.19-259378902513195/vmware_guest.py; rm -rf "/home/e003120/.ansible/tmp/ansible-tmp-1504707292.19-259378902513195/" > /dev/null 2>&amp;1 &amp;&amp; sleep 0'
The full traceback is:
Traceback (most recent call last):
File "/tmp/ansible_FAqrhr/ansible_module_vmware_guest.py", line 1503, in <module>
main()
File "/tmp/ansible_FAqrhr/ansible_module_vmware_guest.py", line 1494, in main
result = pyv.deploy_vm()
File "/tmp/ansible_FAqrhr/ansible_module_vmware_guest.py", line 1189, in deploy_vm
dcpath = compile_folder_path_for_object(datacenter)
File "/tmp/ansible_FAqrhr/ansible_modlib.zip/ansible/module_utils/vmware.py", line 237, in compile_folder_path_for_object
AttributeError: 'NoneType' object has no attribute 'name'

fatal: [localhost]: FAILED! => {
"changed": false,
"failed": true,
"module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_FAqrhr/ansible_module_vmware_guest.py\", line 1503, in <module>\n    main()\n  File \"/tmp/ansible_FAqrhr/ansible_module_vmware_guest.py\", line 1494, in main\n    result = pyv.deploy_vm()\n  File \"/tmp/ansible_FAqrhr/ansible_module_vmware_guest.py\", line 1189, in deploy_vm\n    dcpath = compile_folder_path_for_object(datacenter)\n  File \"/tmp/ansible_FAqrhr/ansible_modlib.zip/ansible/module_utils/vmware.py\", line 237, in compile_folder_path_for_object\nAttributeError: 'NoneType' object has no attribute 'name'\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 0
}

# Fixed code
def compile_folder_path_for_object(vobj):
    """make a /vm/foo/bar/baz like folder path for an object"""

    paths = []
    if isinstance(vobj, vim.Folder):
        paths.append(vobj.name)

    thisobj = vobj
    while hasattr(thisobj, "parent"):
        thisobj = thisobj.parent
        if thisobj._moId == "group-d1":
            break
        if isinstance(thisobj, vim.Folder):
            paths.append(thisobj.name)
    paths.reverse()
    return "/" + "/".join(paths)