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

vmware_vm_info module throws AttributeError: 'NoneType' object has no attribute 'datastoreUrl' #1407

Closed
homandr opened this issue Jul 21, 2022 · 3 comments · Fixed by #1408
Closed
Labels
bug This issue/PR relates to a bug needs_triage Needs a first human triage before being processed. python3 traceback

Comments

@homandr
Copy link

homandr commented Jul 21, 2022

SUMMARY

Module vmware_vm_info chokes on objects without datastoreUrl attribute. Most likely this is not an issue under normal circumstances, and the playbook worked for me in the past. It took some time to figure out our vCenter had a couple of inaccessible VMs, which obviously don't have a lot of attributes.

I barely know anything about Python but was able to fix the issue based on examples of similar past issues.

diff -u vmware_vm_info.py.bak vmware_vm_info.py
--- vmware_vm_info.py.bak       2022-07-21 09:09:41.482721250 -0500
+++ vmware_vm_info.py   2022-07-21 10:12:08.529573282 -0500
@@ -350,7 +350,8 @@
             datacenter = get_parent_datacenter(vm)
             datastore_url = list()
             datastore_attributes = ('name', 'url')
-            if vm.config.datastoreUrl:
+            has_datastoreurl = _get_vm_prop(vm, ('config', 'datastoreUrl'))
+            if has_datastoreurl:
                 for entry in vm.config.datastoreUrl:
                     datastore_url.append({key: getattr(entry, key) for key in dir(entry) if key in datastore_attributes})
             virtual_machine = {
ISSUE TYPE
  • Bug Report
COMPONENT NAME

vmware_vm_info

ANSIBLE VERSION
ansible --version
ansible [core 2.12.4]
  config file = /etc/ansible/ansible.cfg
  ansible python module location = /usr/local/lib/python3.9/site-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 3.9.7 (default, Sep 13 2021, 08:18:39) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3)]
  jinja version = 3.1.1
  libyaml = True
COLLECTION VERSION
Collection       Version
---------------- -------
community.vmware 2.7.0

STEPS TO REPRODUCE
  - name: Get all VMs
    community.vmware.vmware_vm_info:
      hostname: "{{ vcc_hostname }}"
      username: "{{ vcc_user }}"
      password: "{{ vcc_pass }}"
      validate_certs: false
      vm_type: vm
    register: all_vms
EXPECTED RESULTS

Get a list of all VMs.

ACTUAL RESULTS
MODULE_STDERR:

Traceback (most recent call last):
  File "/home/user/.ansible/tmp/ansible-tmp-1658346852.8637314-2616787-129994423621433/AnsiballZ_vmware_vm_info.py", line 107, in <module>
    _ansiballz_main()
  File "/home/user/.ansible/tmp/ansible-tmp-1658346852.8637314-2616787-129994423621433/AnsiballZ_vmware_vm_info.py", line 99, in _ansiballz_main
    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
  File "/home/user/.ansible/tmp/ansible-tmp-1658346852.8637314-2616787-129994423621433/AnsiballZ_vmware_vm_info.py", line 47, in invoke_module
    runpy.run_module(mod_name='ansible_collections.community.vmware.plugins.modules.vmware_vm_info', init_globals=dict(_module_fqn='ansible_collections.community.vmware.plugins.modules.vmware_vm_info', _modlib_path=modlib_path),
  File "/usr/lib64/python3.9/runpy.py", line 210, in run_module
    return _run_module_code(code, init_globals, run_name, mod_spec)
  File "/usr/lib64/python3.9/runpy.py", line 97, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "/usr/lib64/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/tmp/ansible_community.vmware.vmware_vm_info_payload_dk_rqyae/ansible_community.vmware.vmware_vm_info_payload.zip/ansible_collections/community/vmware/plugins/modules/vmware_vm_info.py", line 409, in <module>
  File "/tmp/ansible_community.vmware.vmware_vm_info_payload_dk_rqyae/ansible_community.vmware.vmware_vm_info_payload.zip/ansible_collections/community/vmware/plugins/modules/vmware_vm_info.py", line 403, in main
  File "/tmp/ansible_community.vmware.vmware_vm_info_payload_dk_rqyae/ansible_community.vmware.vmware_vm_info_payload.zip/ansible_collections/community/vmware/plugins/modules/vmware_vm_info.py", line 353, in get_virtual_machines
AttributeError: 'NoneType' object has no attribute 'datastoreUrl'

@ansibullbot
Copy link

Files identified in the description:
None

If these files are inaccurate, please update the component name section of the description or use the !component bot command.

click here for bot help

@ansibullbot ansibullbot added bug This issue/PR relates to a bug needs_triage Needs a first human triage before being processed. python3 traceback labels Jul 21, 2022
@mariolenz
Copy link
Collaborator

Thanks for reporting this @homandr! I've opened a PR to fix it, just didn't find the time to test it yet.

One minor point, though. has_datastoreurl sounds like a Boolean. However, I think _get_vm_prop returns vm.config.datastoreUrl if it exists and None otherwise, not True / False:

def _get_vm_prop(vm, attributes):
"""Safely get a property or return None"""
result = vm
for attribute in attributes:
try:
result = getattr(result, attribute)
except (AttributeError, IndexError):
return None
return result

This might be a bit misleading for someone reading and trying to understand the code. Therefor, I'm using vm_datastore_urls in my fix similar to the way network stuff is handled:

vmnet = _get_vm_prop(vm, ('guest', 'net'))
if vmnet:
for device in vmnet:

softwarefactory-project-zuul bot pushed a commit that referenced this issue Jul 25, 2022
#1408)

vmware_vm_info: Fix NoneType object has no attribute datastoreUrl for inaccessible VMs

SUMMARY
The module fails on inaccessible VMs with 'NoneType' object has no attribute 'datastoreUrl'.
Fixes #1407
ISSUE TYPE

Bugfix Pull Request

COMPONENT NAME
vmware_vm_info
ADDITIONAL INFORMATION
@homandr
Copy link
Author

homandr commented Jul 25, 2022

Thanks for fixing it and educating me in the process :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue/PR relates to a bug needs_triage Needs a first human triage before being processed. python3 traceback
Projects
None yet
3 participants