# Buggy code
def download_screenshot_file(self, file_url, local_file_path, file_name):
    response = None
    download_size = 0
    # file is downloaded as local_file_name when specified, or use original file name
    if not local_file_path.startswith("/"):
        local_file_path = "/" + local_file_path
    if local_file_path.endswith(".png"):
        local_file_name = local_file_path.split("/")[-1]
        local_file_path = local_file_path.rsplit("/", 1)[0]
    else:
        local_file_name = file_name
    if not os.path.exists(local_file_path):
        try:
            os.makedirs(local_file_path)
        except OSError as err:
            self.module.fail_json(
                msg="Exception caught when create folder %s on local machine, with error %s"
                % (local_file_path, to_native(err))
            )
    local_file = os.path.join(local_file_path, local_file_name)
    with open(local_file, "wb") as handle:
        try:
            response = open_url(
                file_url,
                url_username=self.params.get("username"),
                url_password=self.params.get("password"),
                validate_certs=False,
            )
        except Exception as err:
            self.module.fail_json(
                msg="Download screenshot file from URL %s, failed due to %s"
                % (file_url, to_native(err))
            )
        if not response or response.getcode() >= 400:
            self.module.fail_json(
                msg="Download screenshot file from URL %s, failed with response %s, response code %s"
                % (file_url, response, response.getcode())
            )
        bytes_read = response.read(2**20)
        while bytes_read:
            handle.write(bytes_read)
            handle.flush()
            os.fsync(handle.fileno())
            download_size += len(bytes_read)
            bytes_read = response.read(2**20)

    return download_size


# 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 download_screenshot_file(self, file_url, local_file_path, file_name):
    response = None
    download_size = 0
    # file is downloaded as local_file_name when specified, or use original file name
    if local_file_path.endswith(".png"):
        local_file_name = local_file_path.split("/")[-1]
        local_file_path = local_file_path.rsplit("/", 1)[0]
    else:
        local_file_name = file_name
    if not os.path.exists(local_file_path):
        try:
            os.makedirs(local_file_path)
        except OSError as err:
            self.module.fail_json(
                msg="Exception caught when create folder %s on local machine, with error %s"
                % (local_file_path, to_native(err))
            )
    local_file = os.path.join(local_file_path, local_file_name)
    with open(local_file, "wb") as handle:
        try:
            response = open_url(
                file_url,
                url_username=self.params.get("username"),
                url_password=self.params.get("password"),
                validate_certs=False,
            )
        except Exception as err:
            self.module.fail_json(
                msg="Download screenshot file from URL %s, failed due to %s"
                % (file_url, to_native(err))
            )
        if not response or response.getcode() >= 400:
            self.module.fail_json(
                msg="Download screenshot file from URL %s, failed with response %s, response code %s"
                % (file_url, response, response.getcode())
            )
        bytes_read = response.read(2**20)
        while bytes_read:
            handle.write(bytes_read)
            handle.flush()
            os.fsync(handle.fileno())
            download_size += len(bytes_read)
            bytes_read = response.read(2**20)

    return download_size