Dynamic inventory scripts for ansible and vcenter. This script provides example how to use pyvmomi api. Nowadays Ansible provides production-ready inventory plugin vmware_inventory.py
Based on:
- vmware/pyvmomi-community-samples
- vmware/pyvmomi api documentation
- ansible-vmware inventory script based on old pyshphere package
sudo dnf install python-pyvmomi PyYAML
- Copy vcenter_inv.yml.dist to vcenter_inv.yml and edit hostname, user, password. Or you can use one yaml config file with variables for ansible vsphere_guest module and vcenter_inv.py (just create symlink to group_vars/all, for example). Also, you can put configuration file to any place and set VCENTER_INV_CFG environment variable to it.
- Run
./vcenter_inv.py --list
:
Example output:
{
"vcenter": {
"children": [
"centos64Guest",
"sles11_64Guest"
]
},
"sles11_64Guest": {
"hosts": [
"192.168.1.13",
]
},
"centos64Guest": {
"hosts": [
"192.168.1.14",
"192.168.1.15"
]
}
}
- Run ansible ping with '-i' options:
# export VCENTER_INV_CFG=~/.ansible/vcenter_inv.py
ansible -i path/to/vcenter_inv.py -m ping
or add to ansible.cfg:
[defaults]
inventory = path/to/inventory/vcenter_inv.py