# 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 isinstance(thisobj, vim.Folder):
            paths.append(thisobj.name)
    paths.reverse()
    if paths[0] == "Datacenters":
        paths.remove("Datacenters")
    return "/" + "/".join(paths)


# Traceback (IndexError)
Using module file /root/ansible/lib/ansible/modules/cloud/vmware/vmware_guest_find.py
<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 /root/.ansible/tmp/ansible-tmp-1498091429.1572943-39737921190087 `" &amp;&amp; echo ansible-tmp-1498091429.1572943-39737921190087="` echo /root/.ansible/tmp/ansible-tmp-1498091429
.1572943-39737921190087 `" ) &amp;&amp; sleep 0'
<127.0.0.1> PUT /tmp/tmpy3hvylfc TO /root/.ansible/tmp/ansible-tmp-1498091429.1572943-39737921190087/vmware_guest_find.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1498091429.1572943-39737921190087/ /root/.ansible/tmp/ansible-tmp-1498091429.1572943-39737921190087/vmware_guest_find.py &amp;&amp; sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/tmp/ansible-test-coverage-kgav7mhr/coverage/injector.py /root/.ansible/tmp/ansible-tmp-1498091429.1572943-39737921190087/vmware_guest_find.py; rm -rf "/root/.ansible/tmp/ansible-tmp-1498091429.1572943-
39737921190087/" > /dev/null 2>&amp;1 &amp;&amp; sleep 0'
The full traceback is:
Traceback (most recent call last):
File "/tmp/ansible_nybjxzyy/ansible_module_vmware_guest_find.py", line 281, in <module>
main()
File "/tmp/ansible_nybjxzyy/ansible_module_vmware_guest_find.py", line 267, in main
uuid=module.params['uuid']
File "/tmp/ansible_nybjxzyy/ansible_module_vmware_guest_find.py", line 120, in getvm_folder_paths
folderpath = self.compile_folder_path_for_object(vobj)
File "/tmp/ansible_nybjxzyy/ansible_module_vmware_guest_find.py", line 203, in compile_folder_path_for_object
while hasattr(thisobj, 'parent'):
File "/usr/local/lib/python3.5/dist-packages/pyVmomi/VmomiSupport.py", line 574, in __call__
return self.f(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/pyVmomi/VmomiSupport.py", line 394, in _InvokeAccessor
return self._stub.InvokeAccessor(self, info)
File "/usr/local/lib/python3.5/dist-packages/pyVmomi/StubAdapterAccessorImpl.py", line 43, in InvokeAccessor
objectContent = result.objects[0]
IndexError: list index out of range

failed: [testhost] (item=/F0/DC0/vm/F0/DC0_H0_VM1) => {
"changed": false,
"failed": true,
"item": "/F0/DC0/vm/F0/DC0_H0_VM1",
"module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_nybjxzyy/ansible_module_vmware_guest_find.py\", line 281, in <module>\n    main()\n  File \"/tmp/ansible_nybjxzyy/ansible_module_vmware_guest_find.py\"
, line 267, in main\n    uuid=module.params['uuid']\n  File \"/tmp/ansible_nybjxzyy/ansible_module_vmware_guest_find.py\", line 120, in getvm_folder_paths\n    folderpath = self.compile_folder_path_for_object(vobj)\n  File \"/tmp/a
nsible_nybjxzyy/ansible_module_vmware_guest_find.py\", line 203, in compile_folder_path_for_object\n    while hasattr(thisobj, 'parent'):\n  File \"/usr/local/lib/python3.5/dist-packages/pyVmomi/VmomiSupport.py\", line 574, in __ca
ll__\n    return self.f(*args, **kwargs)\n  File \"/usr/local/lib/python3.5/dist-packages/pyVmomi/VmomiSupport.py\", line 394, in _InvokeAccessor\n    return self._stub.InvokeAccessor(self, info)\n  File \"/usr/local/lib/python3.5/
dist-packages/pyVmomi/StubAdapterAccessorImpl.py\", line 43, in InvokeAccessor\n    objectContent = result.objects[0]\nIndexError: list index out of range\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.name == "Datacenters":
            break
        if isinstance(thisobj, vim.Folder):
            paths.append(thisobj.name)
    paths.reverse()
    return "/" + "/".join(paths)