AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Ansible VMware Dynamic Inventory: Complete Guide (2026)

By Luca Berton · Published 2024-01-01 · Category: installation

Complete guide to Ansible VMware dynamic inventory. Configure vmware.vmware.vms plugin, filter VMs, migrate from deprecated community.vmware inventory.

Overview

VMware dynamic inventory plugins automatically discover virtual machines from vSphere and populate your Ansible inventory. This eliminates manual host management and keeps your inventory in sync with your virtual infrastructure.

See also: Ansible VMware Automation: Manage vSphere, ESXi, and Virtual Machines at Scale

Important: Plugin Deprecation

The community.vmware.vmware_vm_inventory plugin was deprecated in v5.4.0 and will be removed in v7.0.0. Migrate to vmware.vmware.vms for a future-proof setup.

| Plugin | Status | Collection | |--------|--------|------------| | community.vmware.vmware_vm_inventory | ⚠️ Deprecated (remove in 7.0.0) | community.vmware | | vmware.vmware.vms | ✅ Active | vmware.vmware |

Install Collections

# New recommended collection
ansible-galaxy collection install vmware.vmware

# Legacy (still works, deprecated) ansible-galaxy collection install community.vmware

# Python dependency pip install pyVmomi>=8.0

See also: Configure Ansible Dynamic Inventory for VMware in Simple Steps

New Plugin: vmware.vmware.vms

Basic Configuration

# vmware_inventory.yml (filename must end in .vmware.yml or .vmware.yaml)
plugin: vmware.vmware.vms
hostname: vcenter.example.com
username: ansible@vsphere.local
password: "{{ lookup('env', 'VMWARE_PASSWORD') }}"
validate_certs: false

With Filters

plugin: vmware.vmware.vms
hostname: vcenter.example.com
username: ansible@vsphere.local
password: "{{ lookup('env', 'VMWARE_PASSWORD') }}"
validate_certs: false

filters: - runtime.powerState == "poweredOn" - config.guestId is not match('windows*') - config.name is match('prod-*')

keyed_groups: - key: config.guestId prefix: os - key: guest.net[0].network prefix: network - key: config.annotation | default('untagged') prefix: tag

compose: ansible_host: guest.ipAddress ansible_user: "'ansible'"

Group by Datacenter/Cluster

plugin: vmware.vmware.vms
hostname: vcenter.example.com
username: ansible@vsphere.local
password: "{{ lookup('env', 'VMWARE_PASSWORD') }}"

keyed_groups: - key: datacenter prefix: dc - key: cluster prefix: cluster - key: folder separator: ""

hostnames: - config.name

Legacy Plugin: community.vmware

# legacy_vmware.yml
plugin: community.vmware.vmware_vm_inventory
server: vcenter.example.com
username: ansible@vsphere.local
password: "{{ lookup('env', 'VMWARE_PASSWORD') }}"
validate_certs: false

with_nested_properties: true properties: - config.name - config.guestId - guest.ipAddress - runtime.powerState - config.createDate

filters: - runtime.powerState == "poweredOn" - config.name is match('web-*')

keyed_groups: - key: config.guestId prefix: os compose: ansible_host: guest.ipAddress

See also: Ansible on VMware ESXi 8 Automation Complete Guide

Migration Guide

Step 1: Install New Collection

ansible-galaxy collection install vmware.vmware

Step 2: Update Inventory File

# OLD (deprecated)
plugin: community.vmware.vmware_vm_inventory
server: vcenter.example.com

# NEW plugin: vmware.vmware.vms hostname: vcenter.example.com

Step 3: Update Property References

Some property names differ between plugins. Test with:

ansible-inventory -i vmware_inventory.yml --list | head -50

Step 4: Validate Groups

ansible-inventory -i vmware_inventory.yml --graph

Filtering VMs by Date

A common requirement is filtering VMs by creation date (e.g., VMs created in the last week):

# Static date filter (works)
filters:
  - config.createDate >= "2026-03-31T00:00:00Z"

# Dynamic date filtering requires a workaround # The inventory plugin evaluates filters before Jinja2 date math # Solution: Use a wrapper script or group_vars post-filter

Workaround: Post-Filter with Playbook

# Filter VMs by age in your playbook
- hosts: all
  tasks:
    - set_fact:
        vm_age_days: "{{ ((ansible_date_time.epoch | int) - (hostvars[inventory_hostname]['config.createDate'] | to_datetime('%Y-%m-%dT%H:%M:%SZ')).strftime('%s') | int)) / 86400 }}"

- debug: msg: "VM {{ inventory_hostname }} is {{ vm_age_days | int }} days old" when: vm_age_days | int <= 7

Using with AWX/AAP

Create a VMware vCenter credential in AWX Create an inventory with Source = Sourced from a project Point to your vmware_inventory.yml file in the project Enable Update on Launch for always-fresh inventory

Credential Security

# Option 1: Environment variables
# export VMWARE_HOST=vcenter.example.com
# export VMWARE_USER=ansible@vsphere.local
# export VMWARE_PASSWORD=secret

plugin: vmware.vmware.vms hostname: "{{ lookup('env', 'VMWARE_HOST') }}" username: "{{ lookup('env', 'VMWARE_USER') }}" password: "{{ lookup('env', 'VMWARE_PASSWORD') }}"

# Option 2: Ansible Vault # ansible-vault encrypt_string 'secret' --name vmware_password

Troubleshooting

No hosts returned

# Check connectivity
python3 -c "from pyVim.connect import SmartConnect; print('OK')"

# Verbose inventory listing ansible-inventory -i vmware_inventory.yml --list -vvv

SSL certificate errors

validate_certs: false  # Development only!

# Production: add CA cert # export VMWARE_VALIDATE_CERTS=true # export REQUESTS_CA_BUNDLE=/etc/pki/tls/certs/vcenter-ca.pem

Slow inventory refresh

# Limit properties fetched
properties:
  - config.name
  - guest.ipAddress
  - runtime.powerState

# Cache results cache: true cache_plugin: jsonfile cache_connection: /tmp/vmware_inventory_cache cache_timeout: 3600

FAQ

When will community.vmware inventory be removed?

In community.vmware collection version 7.0.0 (expected late 2026). Start migrating now.

Can I filter by custom attributes?

Yes — use config.annotation for notes, or custom attributes via the properties list.

How often does dynamic inventory refresh?

Every time you run a playbook, unless you enable caching. In AWX/AAP, configure sync schedules.

Related Articles

Ansible Inventory Complete GuideAnsible AWS Complete GuideList Tags in VMware DatastoreCheck VMware VM SnapshotsGet VMware VM UUIDExpand Virtual Disk in VMware

Category: installation

Browse all Ansible tutorials · AnsiblePilot Home