Vmware.vmware.vms inventory plugin appears to be ignoring inventory cache

I figure I’d post here first before filing a bug report on GitHub as a sanity check. :slight_smile: Those who have been in the community longer, let me know if that isn’t the desired order of things when encountering a possible problem.

I currently use the community.vmware.vmware_vm_inventory inventory plugin in production, and it and its cache works like a charm. An initial inventory takes under 2 minutes to generate. It is then cached for an hour, and subsequent uses of ansible-inventory complete in seconds, which is expected behavior.

I’m working on transitioning to using vmware.vmware.vms now (part of my execution environment transition effort). The initial inventory takes a bit longer to generate, but it’s acceptable. However, the cache, though it is created, seems to be ignored. Thus, each subsequent run of ansible-inventory takes the same amount of time to complete.

Has anyone else experienced this behavior with vmware.vmware.vms? When comparing the documentation of vmware.vmware.vms and community.vmware.vmware_vm_inventory, I have noticed no difference in how caching should be configured.

Relevant Files and Information

Test results for community.vmware.vmware_vm_inventory

This shows the expected behavior with caching. The second run of ansible-inventory takes seconds.

# I use stat, ls, and sha256sum to show the cache is created and then unaltered after the first run

[myuser@myhost ansible]$ stat /tmp/ansible_inventory_cache
stat: cannot statx '/tmp/ansible_inventory_cache': No such file or directory

[myuser@myhost ansible]$ time ansible-inventory -i vmware_vm_inventory.yml --host my-inventory-item
[DEPRECATION WARNING]: community.vmware.vmware_vm_inventory has been deprecated. Use vmware.vmware.vms instead. This
feature will be removed from community.vmware in version 7.0.0. Deprecation warnings can be disabled by setting
deprecation_warnings=False in ansible.cfg.
[WARNING]: Found variable using reserved name: tags
{
    EXPECTED OUTPUT
}

real    1m44.901s
user    0m28.273s
sys     0m3.607s

[myuser@myhost ansible]$ ls -l /tmp/ansible_inventory_cache/
total 104
-rw-r--r--. 1 myuser myuser 103730 Apr 29 13:53 vcenter_community.vmware.vmware_vm_inventory_9de88s_5bf4b

[myuser@myhost ansible]$ sha256sum /tmp/ansible_inventory_cache/vcenter_community.vmware.vmware_vm_inventory_9de88s_5bf4b
d80d02ca853f13f04b5b5fcf45a9d47bff7586c527bc252b08d6d646ffdabc18  /tmp/ansible_inventory_cache/vcenter_community.vmware.vmware_vm_inventory_9de88s_5bf4b

[myuser@myhost ansible]$ time ansible-inventory -i vmware_vm_inventory.yml --host my-inventory-item
[DEPRECATION WARNING]: community.vmware.vmware_vm_inventory has been deprecated. Use vmware.vmware.vms instead. This
feature will be removed from community.vmware in version 7.0.0. Deprecation warnings can be disabled by setting
deprecation_warnings=False in ansible.cfg.
[WARNING]: Found variable using reserved name: tags
{
    EXPECTED OUTPUT
}

real    0m2.740s
user    0m2.327s
sys     0m0.165s

[myuser@myhost ansible]$ ls -l /tmp/ansible_inventory_cache/
total 104
-rw-r--r--. 1 myuser myuser 103730 Apr 29 13:53 vcenter_community.vmware.vmware_vm_inventory_9de88s_5bf4b

[myuser@myhost ansible]$ sha256sum /tmp/ansible_inventory_cache/vcenter_community.vmware.vmware_vm_inventory_9de88s_5bf4b
d80d02ca853f13f04b5b5fcf45a9d47bff7586c527bc252b08d6d646ffdabc18  /tmp/ansible_inventory_cache/vcenter_community.vmware.vmware_vm_inventory_9de88s_5bf4b

Test results for vmware.vmware.vms

This shows the behavior where the cache appears to be ignored. The second run of ansible-inventory takes nearly the same amount of time as the original run.

# I use stat, ls, and sha256sum to show the cache is created and then unaltered after the first run

[myuser@myhostansible]$ stat /tmp/ansible_inventory_cache/
stat: cannot statx '/tmp/ansible_inventory_cache/': No such file or directory

[myuser@myhost ansible]$ time ansible-inventory -i vmware_vms.yml --host my-inventory-item
[DEPRECATION WARNING]: community.vmware.vmware_vm_inventory has been deprecated. Use vmware.vmware.vms instead. This
feature will be removed from community.vmware in version 7.0.0. Deprecation warnings can be disabled by setting
deprecation_warnings=False in ansible.cfg.
[WARNING]: Found variable using reserved name: name
[WARNING]: Found variable using reserved name: tags
{
    EXPECTED OUTPUT
}

real    2m48.336s
user    0m49.858s
sys     0m6.079s

[myuser@myhost ansible]$ ls -l /tmp/ansible_inventory_cache/
total 740
-rw-r--r--. 1 myuser myuser 755901 Apr 29 13:37 vcenter_vmware.vmware.vms_051c7s_cd955

[myuser@myhost ansible]$ sha256sum /tmp/ansible_inventory_cache/vcenter_vmware.vmware.vms_051c7s_cd955
c61c1f5961bb7c309788a3cbcdceedf1329a303821c2f9cca1c9f51afc916e28  /tmp/ansible_inventory_cache/vcenter_vmware.vmware.vms_051c7s_cd955

[myuser@myhost ansible]$ time ansible-inventory -i vmware_vms.yml --host my-inventory-item
[DEPRECATION WARNING]: community.vmware.vmware_vm_inventory has been deprecated. Use vmware.vmware.vms instead. This
feature will be removed from community.vmware in version 7.0.0. Deprecation warnings can be disabled by setting
deprecation_warnings=False in ansible.cfg.
[WARNING]: Found variable using reserved name: name
[WARNING]: Found variable using reserved name: tags
{
    EXPECTED OUTPUT
}

real    2m39.877s
user    0m46.676s
sys     0m5.465s

[myuser@myhost ansible]$ ls -l /tmp/ansible_inventory_cache/
total 740
-rw-r--r--. 1 myuser myuser 755901 Apr 29 13:37 vcenter_vmware.vmware.vms_051c7s_cd955

[myuser@myhost ansible]$ sha256sum /tmp/ansible_inventory_cache/vcenter_vmware.vmware.vms_051c7s_cd955
c61c1f5961bb7c309788a3cbcdceedf1329a303821c2f9cca1c9f51afc916e28  /tmp/ansible_inventory_cache/vcenter_vmware.vmware.vms_051c7s_cd955

ansible --version

[myuser@myhost ansible]$ ansible --version
ansible [core 2.18.5]
  config file = /home/myuser/ansible/ansible.cfg
  configured module search path = ['/home/myuser/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /opt/venv-ansible/lib64/python3.12/site-packages/ansible
  ansible collection location = /home/myuser/ansible/collections
  executable location = /opt/venv-ansible/bin/ansible
  python version = 3.12.5 (main, Apr  2 2025, 00:00:00) [GCC 11.5.0 20240719 (Red Hat 11.5.0-5)] (/opt/venv-ansible/bin/python3.12)
  jinja version = 3.1.6
  libyaml = True

Relevant bits from ansible.cfg

[defaults]
# (pathlist) Comma separated list of Ansible inventory sources
inventory=./inventory/

# (pathspec) Colon separated paths in which Ansible will search for Inventory Plugins.
;inventory_plugins=~/.ansible/plugins/inventory:/usr/share/ansible/plugins/inventory

[inventory]
# (bool) Toggle to turn on inventory caching.
# This setting has been moved to the individual inventory plugins as a plugin option :ref:`inventory_plugins`.
# The existing configuration settings are still accepted with the inventory plugin adding additional options from inventory configuration.
# This message will be removed in 2.16.
;cache=False

# (string) The plugin for caching inventory.
# Each cache_ item has the same set of comments as above.
;cache_plugin=

# (string) The inventory cache connection.
# Each cache_ item has the same set of comments as above.
;cache_connection=

# (string) The table prefix for the cache plugin.
# Each cache_ item has the same set of comments as above.
;cache_prefix=ansible_inventory_

# (string) Expiration timeout for the inventory cache plugin data.
# Each cache_ item has the same set of comments as above.
;cache_timeout=3600

# (list) List of enabled inventory plugins, it also determines the order in which they are used.
;enable_plugins=host_list, script, auto, yaml, ini, toml
enable_plugins=vmware.vmware.vms, community.vmware.vmware_vm_inventory, host_list, script, auto, yaml, ini, toml

vmware.vmware.vms plugin configuration

---
plugin: vmware.vmware.vms
strict: true
hostname: vcenter.fq.dn
username: vcenter_user
password: vault-encrypted-password
validate_certs: False
cache: true
cache_connection: /tmp/ansible_inventory_cache/
cache_plugin: ansible.builtin.jsonfile
cache_prefix: vcenter_
cache_timeout: 3600
filters:
  - "tags['urn:vmomi:InventoryServiceTag:TAG_GUID:GLOBAL'] is not defined"
hostnames:
  - "name"
properties:
  - "name"
  - "guest.ipAddress"
keyed_groups: []
gather_tags: True
groups:
  datacenter1: "'/datacenter1' in path"
  datacenter2: "'/datacenter2' in path"
  datacenter3: "'/datacenter3' in path"
...

community.vmware.vmware_vm_inventory configuration

---
plugin: community.vmware.vmware_vm_inventory
strict: false
hostname: vcenter.fq.dn
username: vcenter_user
password: vault-encrypted-password
cache: true
cache_connection: /tmp/ansible_inventory_cache
cache_plugin: ansible.builtin.jsonfile
cache_prefix: vcenter_
cache_timeout: 3600
validate_certs: False
filters:
  - "(tag_category.MY_TAG_CATEGORY is defined) and ('MY_TAG' in tag_category.MY_TAG_CATEGORY)"
hostnames:
  - 'config.name'
properties:
  - 'guest.ipAddress'
  - 'config.name'
groups:
  datacenter1: '"Datacenters/datacenter1" in path'
  datacenter2: '"Datacenters/datacenter2" in path'
  datacenter3: '"Datacenters/datacenter3" in path'
with_sanitized_property_name: true
with_path: true
with_tags: true
...

Update:
I’ve noticed that running stat against the cache file shows that it appears to be accessed on subsequent ansible-inventory runs. I’m doing some testing with strace to try and confirm if it’s the plugin that’s accessing the cache file and somehow not understanding the file and thus querying vCenter again.

In the meantime, I did go ahead and make a bug report.

1 Like

Looks like this was an actual issue, and a fix is in the works. Thanks @mikemorency!

2 Likes