What you'll learn
- Introduction
- Ansible Dynamic Inventory for VMware
- Links
- Demo
- Configuration Files
- Listing Hosts
- Graphing Hosts
- Conclusion
- Related Articles
- Complete Inventory Configuration
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.
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`](https://docs.ansible.com/ansible/latest/collections/community/vmware/vmware_vm_inventory_inventory.html)
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**:
```yaml
[inventory]
enable_plugins = vmware_vm_inventory
```
- **inventory.vmware.yml**:
```yaml
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**:
```bash
$ ansible-inventory -i inventory.vmware.yml --list
```
- **Output**:
```bash
{
"VMs": {
"hosts": [
"myvm_42254893-3793-0e4f-9f61-7c37d244c2a8"
]
},
"_meta": {
"hostvars": {
"myvm_42254893-3793-0e4f-9f61-7c37d244c2a8": {
"config": {
...
},
...
}
}
},
"all": {
"children": [
"VMs",
...
]
},
...
}
```
Graphing Hosts
- **Command**:
```bash
$ ansible-inventory -i inventory.vmware.yml --graph
```
- **Output**:
```bash
@all:
|--@VMs:
| |--myvm_42254893-3793-0e4f-9f61-7c37d244c2a8
|--@centos64Guest:
| |--myvm_42254893-3793-0e4f-9f61-7c37d244c2a8
|--@poweredOff:
| |--myvm_42254893-3793-0e4f-9f61-7c37d244c2a8
```
[Code with ❤️ on GitHub](https://github.com/lucab85/ansible-pilot/)
Conclusion
Now you know how to configure Ansible Dynamic Inventory for VMware, enabling you to automate and manage your VMware virtual machines efficiently.
Related Articles
- [building an Ansible inventory](/articles/ansible-inventory-complete-guide)
- [How to Add a Disk to a VMware VM Using Ansible Playbook](/articles/add-a-new-hard-disk-to-vmware-vsphere-virtual-machine-ansible-module-vmware-guest-disk)
- [Configure a Python Virtual Environment for Ansible VMware - ansible collection community.vmware](/