Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: 'TimeoutError' object is not subscriptable in vsphere_file.py #2215

Open
CarstenGrohmann opened this issue Oct 22, 2024 · 0 comments

Comments

@CarstenGrohmann
Copy link
Contributor

SUMMARY

The handling of TimeoutError exceptions in vsphere_file.py causes an additional exception:

Traceback (most recent call last):
  File "/root/.ansible/tmp/ansible-tmp-1729501040.8020163-2149944-259712228090606/AnsiballZ_vsphere_file.py", line 107, in <module>
    _ansiballz_main()
  File "/root/.ansible/tmp/ansible-tmp-1729501040.8020163-2149944-259712228090606/AnsiballZ_vsphere_file.py", line 99, in _ansiballz_main
    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
  File "/root/.ansible/tmp/ansible-tmp-1729501040.8020163-2149944-259712228090606/AnsiballZ_vsphere_file.py", line 47, in invoke_module
    runpy.run_module(mod_name='ansible_collections.community.vmware.plugins.modules.vsphere_file', init_globals=dict(_module_fqn='ansible_collections.community.vmware.plugins.modules.vsphere_file', _modlib_path=modlib_path),
  File "<frozen runpy>", line 226, in run_module
  File "<frozen runpy>", line 98, in _run_module_code
  File "<frozen runpy>", line 88, in _run_code
  File "/tmp/ansible_community.vmware.vsphere_file_payload_03jc8ok7/ansible_community.vmware.vsphere_file_payload.zip/ansible_collections/community/vmware/plugins/modules/vsphere_file.py", line 350, in <module>
  File "/tmp/ansible_community.vmware.vsphere_file_payload_03jc8ok7/ansible_community.vmware.vsphere_file_payload.zip/ansible_collections/community/vmware/plugins/modules/vsphere_file.py", line 199, in main
TypeError: 'TimeoutError' object is not subscriptable

The related source code in our (older) version is:

196     except HTTPError as e:
197         r = e
198     except socket.error as e:
199         module.fail_json(msg=to_native(e), errno=e[0], reason=to_native(e), **result)
200     except Exception as e:
201         module.fail_json(msg=to_native(e), errno=dir(e), reason=to_native(e), **result)

In the current code the lines are a little bit moved:

# Check if the file/directory exists
try:
r = open_url(url, method='HEAD', timeout=timeout,
url_username=username, url_password=password,
validate_certs=validate_certs, force_basic_auth=True)
except HTTPError as e:
r = e
except socket.error as e:
module.fail_json(msg=to_native(e), errno=e[0], reason=to_native(e), **result)
except Exception as e:
module.fail_json(msg=to_native(e), errno=dir(e), reason=to_native(e), **result)

It looks like a simple programming error in handling OSError exceptions:

>>> f = TimeoutError()
>>> f[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'TimeoutError' object is not subscriptable

>>> try:
...     f = open("/not-existing")
... except Exception as e:
...     b = e
...
>>> b[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'FileNotFoundError' object is not subscriptable

An untested solution could be:

--- vsphere_file.py.orig        2024-06-03 14:25:54.869881375 +0200
+++ vsphere_file.py     2024-10-22 09:41:10.400988292 +0200
@@ -195,8 +195,8 @@
                      validate_certs=validate_certs, force_basic_auth=True)
     except HTTPError as e:
         r = e
-    except socket.error as e:
-        module.fail_json(msg=to_native(e), errno=e[0], reason=to_native(e), **result)
+    except OSError as e:
+        module.fail_json(msg=to_native(e), errno=e.errno, reason=to_native(e), **result)
     except Exception as e:
         module.fail_json(msg=to_native(e), errno=dir(e), reason=to_native(e), **result)

Since Python 3.3 socket.error is an alias of OSError.

ISSUE TYPE
  • Bug Report
COMPONENT NAME

vsphere_file.py

ANSIBLE VERSION
$ ansible --version
ansible [core 2.15.0]
  config file = /home/carsten/ansible.cfg
  configured module search path = ['/home/carsten/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules', '/home/carsten/project/modules']
  ansible python module location = /home/carsten/virtualenv/lib64/python3.10/site-packages/ansible
  ansible collection location = /home/carsten/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/carsten/virtualenv/bin/ansible
  python version = 3.10.13 (main, Sep 05 2023, 11:46:10) [GCC] (/home/carsten/virtualenv/bin/python3.10)
  jinja version = 3.1.2
  libyaml = True
COLLECTION VERSION
$ ansible-galaxy collection list community.vmware

# /home/carsten/.ansible/collections/ansible_collections
Collection       Version
---------------- -------
community.vmware 4.0.1

# /home/carsten/virtualenv/lib/python3.10/site-packages/ansible_collections
Collection       Version
---------------- -------
community.vmware 3.6.0

# /home/carsten/virtualenv/lib64/python3.10/site-packages/ansible_collections
Collection       Version
---------------- -------
community.vmware 3.6.0
CONFIGURATION
$ ansible-config dump --only-changed
ANSIBLE_NOCOLOR(env: ANSIBLE_NOCOLOR) = True
CALLBACKS_ENABLED(/home/carsten/project/ansible.cfg) = ['profile_tasks']
CONFIG_FILE() = /home/carsten/project/ansible.cfg
DEFAULT_HOST_LIST(/home/carsten/project/ansible.cfg) = ['/etc/ansible/hosts']
DEFAULT_MODULE_PATH(/home/carsten/project/ansible.cfg) = ['/home/carsten/project/.ansible/plugins/modules', '/usr>
DEFAULT_STDOUT_CALLBACK(env: ANSIBLE_STDOUT_CALLBACK) = debug
DEFAULT_VAULT_PASSWORD_FILE(env: ANSIBLE_VAULT_PASSWORD_FILE) = /home/carsten/.ansible_vault_password
DEPRECATION_WARNINGS(/home/carsten/project/ansible.cfg) = False
PAGER(env: PAGER) = less
OS / ENVIRONMENT
$ cat /etc/os-release
NAME="SLES"
VERSION="15-SP5"
VERSION_ID="15.5"
PRETTY_NAME="SUSE Linux Enterprise Server 15 SP5"
ID="sles"
ID_LIKE="suse"
ANSI_COLOR="0;32"
CPE_NAME="cpe:/o:suse:sles:15:sp5"
DOCUMENTATION_URL="https://documentation.suse.com/"
$ python --version
Python 3.10.13
STEPS TO REPRODUCE

see: summary

EXPECTED RESULTS

A detailed error message has been shown instead of the Python traceback.

see: summary

ACTUAL RESULTS

see: summary

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant