Configure Ansible Dynamic Inventory for VMware in Simple Steps
By Luca Berton · Published 2024-01-01 · Category: installation
Discover how to configure Ansible Dynamic Inventory for VMware to automate and manage virtual machines efficiently. Step-by-step guide with Playbook included.

Introduction
This guide shows you the best way to list all the virtual machines within your VMware infrastructure using Ansible. I'll provide a live Playbook and some simple Ansible code. Welcome to today's episode of Ansible Pilot—I'm Luca Berton.
See also: How to Add a Disk to a VMware VM Using Ansible Playbook
Ansible Dynamic Inventory for VMware
• Functionality: Dynamically retrieves virtual machines as inventory hosts from the VMware environment. • Plugin:community.vmware.vmware_vm_inventory
• Dependencies: Python pyVmomi, requests, and vSphere Automation SDK
The VMware dynamic inventory plugin interacts with VMware APIs to dynamically manage nodes with Ansible. The community.vmware.vmware_vm_inventory plugin, part of the community-supported collection, allows you to run Ansible automation across your VMware virtual machines.
Links
•community.vmware.vmware_vm_inventory
See also: Configure a Python Virtual Environment for Ansible VMware - ansible collection community.vmware
Demo
Let’s configure Ansible Dynamic Inventory for VMware to list all virtual machines in your VMware infrastructure as an Ansible inventory.Configuration Files
• ansible.cfg: [inventory]
enable_plugins = vmware_vm_inventory
• inventory.vmware.yml:
plugin: vmware_vm_inventory
strict: False
hostname: vmware.example.com
username: username@vsphere.local
password: MySecretPassword123
validate_certs: False
with_tags: False
groups:
VMs: True
Listing Hosts
• Command: $ ansible-inventory -i inventory.vmware.yml --list
• Output:
{
"VMs": {
"hosts": [
"myvm_42254893-3793-0e4f-9f61-7c37d244c2a8"
]
},
"_meta": {
"hostvars": {
"myvm_42254893-3793-0e4f-9f61-7c37d244c2a8": {
"config": {
...
},
...
}
}
},
"all": {
"children": [
"VMs",
...
]
},
...
}
Graphing Hosts
• Command: $ ansible-inventory -i inventory.vmware.yml --graph
• Output:
@all:
|--@VMs:
| |--myvm_42254893-3793-0e4f-9f61-7c37d244c2a8
|--@centos64Guest:
| |--myvm_42254893-3793-0e4f-9f61-7c37d244c2a8
|--@poweredOff:
| |--myvm_42254893-3793-0e4f-9f61-7c37d244c2a8
Conclusion
Now you know how to configure Ansible Dynamic Inventory for VMware, enabling you to automate and manage your VMware virtual machines efficiently.
See also: Stop a VMware vSphere Virtual Machine - Ansible module vmware_guest_powerstate
Related Articles
• building an Ansible inventoryComplete Inventory Configuration
# vmware_inventory.yml
plugin: community.vmware.vmware_vm_inventory
strict: false
hostname: vcenter.example.com
username: "{{ vault_vcenter_user }}"
password: "{{ vault_vcenter_password }}"
validate_certs: false
# Group VMs by properties
keyed_groups:
- key: config.guestId
separator: ""
- key: guest.guestFamily
prefix: family
- key: summary.runtime.powerState
prefix: power
# Filter VMs
filters:
- summary.runtime.powerState == "poweredOn"
# Compose variables
compose:
ansible_host: guest.ipAddress
ansible_user: "'ansible'"
datacenter: config.datacenter
Using the Dynamic Inventory
# Test the inventory
ansible-inventory -i vmware_inventory.yml --list
ansible-inventory -i vmware_inventory.yml --graph
# Run playbook with VMware inventory
ansible-playbook -i vmware_inventory.yml site.yml
# Target specific groups
ansible family_linuxGuest -i vmware_inventory.yml -m ansible.builtin.ping
Managing VMware VMs with Ansible
- name: Manage VMware infrastructure
hosts: localhost
connection: local
tasks:
- name: Create a VM from template
community.vmware.vmware_guest:
hostname: "{{ vcenter_hostname }}"
username: "{{ vault_vcenter_user }}"
password: "{{ vault_vcenter_password }}"
validate_certs: false
name: new-server
template: ubuntu-template
datacenter: DC1
cluster: Cluster1
folder: /DC1/vm/Linux
state: poweredon
hardware:
memory_mb: 4096
num_cpus: 2
networks:
- name: VM Network
ip: 10.0.0.100
netmask: 255.255.255.0
gateway: 10.0.0.1
FAQ
How do I install the VMware collection?
ansible-galaxy collection install community.vmware
pip install pyvmomi
Can I use tags to group VMs?
Yes. Use keyed_groups with config.tag or custom attributes to create dynamic groups based on VMware tags.
How do I handle vCenter certificates?
Set validate_certs: false for testing, or install the vCenter CA certificate and set validate_certs: true for production.
Category: installation
Watch the video: Configure Ansible Dynamic Inventory for VMware in Simple Steps — Video Tutorial