# Buggy code
def generate_http_access_url(self, file_path):
    # e.g., file_path is like this format: [datastore0] test_vm/test_vm-1.png
    # from file_path generate URL
    url_path = None
    if not file_path:
        return url_path

    path = "/folder/%s" % quote(file_path.split()[1])
    params = dict(dsName=file_path.split()[0].strip("[]"))
    if not self.is_vcenter():
        datacenter = "ha-datacenter"
    else:
        if self.params.get("datacenter"):
            datacenter = self.params["datacenter"]
        else:
            datacenter = self.get_parent_datacenter(self.current_vm_obj).name
        datacenter = datacenter.replace("&", "%26")
    params["dcPath"] = datacenter
    url_path = "https://%s%s?%s" % (self.params["hostname"], path, urlencode(params))

    return url_path


# Traceback (AttributeError)
TASK [vmware_guest_screenshot : take screenshot of virtual machine's console and download to local] **********************************************
task path: /home/goneri/.ansible/test/tmp/vmware_guest_screenshot-yyx29421-ÅÑŚÌβŁÈ/test/integration/targets/vmware_guest_screenshot/tasks/main.yml:38
<testhost> ESTABLISH LOCAL CONNECTION FOR USER: goneri
<testhost> EXEC /bin/sh -c 'echo ~goneri &amp;&amp; sleep 0'
<testhost> EXEC /bin/sh -c '( umask 77 &amp;&amp; mkdir -p "` echo /home/goneri/.ansible/tmp/ansible-tmp-1565778524.0166469-178908954277069 `" &amp;&amp; echo ansible-tmp-1565778524.0166469-178908954277069="` echo /home/goneri/.ansible/tmp/ansible-tmp-1565778524.0166469-178908954277069 `" ) &amp;&amp; sleep 0'
Using module file /home/goneri/git_repos/ansible_projects/c/lib/ansible/modules/cloud/vmware/vmware_guest_screenshot.py
<testhost> PUT /home/goneri/.ansible/tmp/ansible-local-3806uqeztqz4/tmplgf85opc TO /home/goneri/.ansible/tmp/ansible-tmp-1565778524.0166469-178908954277069/AnsiballZ_vmware_guest_screenshot.py
<testhost> EXEC /bin/sh -c 'chmod u+x /home/goneri/.ansible/tmp/ansible-tmp-1565778524.0166469-178908954277069/ /home/goneri/.ansible/tmp/ansible-tmp-1565778524.0166469-178908954277069/AnsiballZ_vmware_guest_screenshot.py &amp;&amp; sleep 0'
<testhost> EXEC /bin/sh -c 'ESXI1_HOSTNAME=root ESXI1_PASSWORD='"'"'!234AaAa56'"'"' ESXI1_USERNAME=esxi1.test ESXI2_HOSTNAME=root ESXI2_PASSWORD='"'"'!234AaAa56'"'"' ESXI2_USERNAME=esxi2.test VCENTER_HOSTNAME=vcenter.test VCENTER_PASSWORD='"'"'!234AaAa56'"'"' VCENTER_USERNAME=administrator@vsphere.local VMWARE_PROXY_HOST='"'"''"'"' VMWARE_PROXY_PORT=0 VMWARE_VALIDATE_CERTS=False /home/goneri/git_repos/ansible_projects/c/.virtualenv/py37/bin/python /home/goneri/.ansible/tmp/ansible-tmp-1565778524.0166469-178908954277069/AnsiballZ_vmware_guest_screenshot.py &amp;&amp; sleep 0'
<testhost> EXEC /bin/sh -c 'rm -f -r /home/goneri/.ansible/tmp/ansible-tmp-1565778524.0166469-178908954277069/ > /dev/null 2>&amp;1 &amp;&amp; sleep 0'
The full traceback is:
Traceback (most recent call last):
File "/home/goneri/.ansible/tmp/ansible-tmp-1565778524.0166469-178908954277069/AnsiballZ_vmware_guest_screenshot.py", line 139, in <module>
_ansiballz_main()
File "/home/goneri/.ansible/tmp/ansible-tmp-1565778524.0166469-178908954277069/AnsiballZ_vmware_guest_screenshot.py", line 131, in _ansiballz_main
invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
File "/home/goneri/.ansible/tmp/ansible-tmp-1565778524.0166469-178908954277069/AnsiballZ_vmware_guest_screenshot.py", line 65, in invoke_module
spec.loader.exec_module(module)
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/tmp/ansible_vmware_guest_screenshot_payload_ka1817jv/__main__.py", line 293, in <module>
File "/tmp/ansible_vmware_guest_screenshot_payload_ka1817jv/__main__.py", line 285, in main
File "/tmp/ansible_vmware_guest_screenshot_payload_ka1817jv/__main__.py", line 251, in take_vm_screenshot
File "/tmp/ansible_vmware_guest_screenshot_payload_ka1817jv/__main__.py", line 169, in generate_http_access_url
AttributeError: 'PyVmomiHelper' object has no attribute 'get_parent_datacenter'

fatal: [testhost]: FAILED! => {
"changed": false,
"module_stderr": "Traceback (most recent call last):\n  File \"/home/goneri/.ansible/tmp/ansible-tmp-1565778524.0166469-178908954277069/AnsiballZ_vmware_guest_screenshot.py\", line 139, in <module>\n    _ansiballz_main()\n  File \"/home/goneri/.ansible/tmp/ansible-tmp-1565778524.0166469-178908954277069/AnsiballZ_vmware_guest_screenshot.py\", line 131, in _ansiballz_main\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n  File \"/home/goneri/.ansible/tmp/ansible-tmp-1565778524.0166469-178908954277069/AnsiballZ_vmware_guest_screenshot.py\", line 65, in invoke_module\n    spec.loader.exec_module(module)\n  File \"<frozen importlib._bootstrap_external>\", line 728, in exec_module\n  File \"<frozen importlib._bootstrap>\", line 219, in _call_with_frames_removed\n  File \"/tmp/ansible_vmware_guest_screenshot_payload_ka1817jv/__main__.py\", line 293, in <module>\n  File \"/tmp/ansible_vmware_guest_screenshot_payload_ka1817jv/__main__.py\", line 285, in main\n  File \"/tmp/ansible_vmware_guest_screenshot_payload_ka1817jv/__main__.py\", line 251, in take_vm_screenshot\n  File \"/tmp/ansible_vmware_guest_screenshot_payload_ka1817jv/__main__.py\", line 169, in generate_http_access_url\nAttributeError: 'PyVmomiHelper' object has no attribute 'get_parent_datacenter'\n",
"module_stdout": "",
"msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
"rc": 1
}

# Fixed code
def generate_http_access_url(self, file_path):
    # e.g., file_path is like this format: [datastore0] test_vm/test_vm-1.png
    # from file_path generate URL
    url_path = None
    if not file_path:
        return url_path

    path = "/folder/%s" % quote(file_path.split()[1])
    params = dict(dsName=file_path.split()[0].strip("[]"))
    if not self.is_vcenter():
        datacenter = "ha-datacenter"
    else:
        datacenter = get_parent_datacenter(self.current_vm_obj).name.replace("&", "%26")
    params["dcPath"] = datacenter
    url_path = "https://%s%s?%s" % (self.params["hostname"], path, urlencode(params))

    return url_path