Ansible Dynamic Inventory For VMware - Ansible vmware_vm_inventory
How to list all the virtual machines in your VMware Infrastructure and use it as an Ansible inventory.


How to configure Ansible Dynamic Inventory for VMware?
This is the best way to list all the virtual machines inside your VMware infrastructure. I’m going to show you a live demo with some simple Ansible code. I’m Luca Berton and welcome to today’s episode of Ansible Pilot.
Ansible Dynamic Inventory For VMware
- Get virtual machines as inventory hosts from the VMware environment
community.vmware.vmware_vm_inventory
- Python pyVmomi, requests, vSphere Automation SDK
The VMware dynamic inventory plugin dynamically queries VMware APIs and tells Ansible what nodes can be managed.
The purpose is to get virtual machines as inventory hosts from the VMware environment.
In this way, you could execute your Ansible automation to all your virtual machines for example.
The full name is community.vmware.vmware_vm_inventory
, which means that is part of the collection of modules to interact with VMware, community-supported.
This plugin requires the pyVmomi
Python SDK for the VMware vSphere API that allows your application to manage ESX, ESXi, and vCenter infrastructure.
This library interacts with the VMware vSphere API that allows you to manage ESX, ESXi, and vCenter in order to execute some Ansible code.
The pyVmomi
Python library supports Python 2.7.x and 3.4+.
Other requirements are Python requests library and vSphere Automation SDK only for tag feature.
Please note that the inventory YAML configuration file MUST ends with vmware.yml, vmware.yaml, vmware_vm_inventory.yml, or vmware_vm_inventory.yaml.
Links
The Best Resources For Ansible
Video Course
Printed Book
eBooks
- Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
- Ansible For Windows By Examples: 50+ Automation Examples For Windows System Administrator And DevOps
- Ansible For Linux by Examples: 100+ Automation Examples For Linux System Administrator and DevOps
- Ansible Linux Filesystem By Examples: 40+ Automation Examples on Linux File and Directory Operation for Modern IT Infrastructure
- Ansible For Containers and Kubernetes By Examples: 20+ Automation Examples To Automate Containers, Kubernetes and OpenShift
- Ansible For Security by Examples: 100+ Automation Examples to Automate Security and Verify Compliance for IT Modern Infrastructure
- Ansible Tips and Tricks: 10+ Ansible Examples to Save Time and Automate More Tasks
- Ansible Linux Users & Groups By Examples: 20+ Automation Examples on Linux Users and Groups Operation for Modern IT Infrastructure
- Ansible For PostgreSQL by Examples: 10+ Examples To Automate Your PostgreSQL database
- Ansible For Amazon Web Services AWS By Examples: 10+ Examples To Automate Your AWS Modern Infrastructure
- Ansible Automation Platform By Example: A step-by-step guide for the most common user scenarios
demo
How to Configure the Ansible Dynamic Inventory For VMware to list all the virtual machines in your VMware Infrastructure as an Ansible inventory.
code
- ansible.cfg
[inventory]
enable_plugins = vmware_vm_inventory
- inventory.vmware.yml
plugin: vmware_vm_inventory
strict: False
hostname: vmware.example.com
username: [email protected]
password: MySecretPassword123
validate_certs: False
with_tags: False
groups:
VMs: True
list hosts
$ ansible-inventory -i inventory.vmware.yml --list
{
"VMs": {
"hosts": [
"myvm_42254893-3793-0e4f-9f61-7c37d244c2a8"
]
},
"_meta": {
"hostvars": {
"myvm_42254893-3793-0e4f-9f61-7c37d244c2a8": {
"config": {
"cpuHotAddEnabled": false,
"cpuHotRemoveEnabled": false,
"guestId": "centos64Guest",
"hardware": {
"numCPU": 1
},
"instanceUuid": "5025d3e9-6c26-30b5-d29a-2c1be5fa3862",
"name": "myvm",
"template": false,
"uuid": "42254893-3793-0e4f-9f61-7c37d244c2a8"
},
"config.cpuHotAddEnabled": false,
"config.cpuHotRemoveEnabled": false,
"config.guestId": "centos64Guest",
"config.hardware.numCPU": 1,
"config.instanceUuid": "5025d3e9-6c26-30b5-d29a-2c1be5fa3862",
"config.name": "myvm",
"config.template": false,
"config.uuid": "42254893-3793-0e4f-9f61-7c37d244c2a8",
"guest": {
"guestState": "notRunning"
},
"guest.guestState": "notRunning",
"name": "myvm",
"runtime": {
"connectionState": "connected"
},
"runtime.connectionState": "connected",
"summary": {
"runtime": {
"powerState": "poweredOff"
}
},
"summary.runtime.powerState": "poweredOff"
}
}
},
"all": {
"children": [
"VMs",
"centos64Guest",
"poweredOff",
"ungrouped"
]
},
"centos64Guest": {
"hosts": [
"myvm_42254893-3793-0e4f-9f61-7c37d244c2a8"
]
},
"poweredOff": {
"hosts": [
"myvm_42254893-3793-0e4f-9f61-7c37d244c2a8"
]
}
}
graph hosts
$ ansible-inventory -i inventory.vmware.yml --graph
@all:
|--@VMs:
| |--myvm_42254893-3793-0e4f-9f61-7c37d244c2a8
|--@centos64Guest:
| |--myvm_42254893-3793-0e4f-9f61-7c37d244c2a8
|--@poweredOff:
| |--myvm_42254893-3793-0e4f-9f61-7c37d244c2a8
Recap
Now you know how to Configure Ansible Dynamic Inventory For VMware.
Subscribe to the YouTube channel, Medium, Website, Twitter, and Substack to not miss the next episode of the Ansible Pilot.Academy
Learn the Ansible automation technology with some real-life examples in my
My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Donate
Want to keep this project going? Please donate