Dynamic Inventory for vSphere 9 with Ansible – A Practical Deep Dive

In the first part of this series, we laid the groundwork for using Ansible to automate VMware vSphere 9 environments. Now that we have our Ubuntu control node, VS Code environment, and connection to vCenter ready, it’s time to make things dynamic.

Why Use Dynamic Inventory?

Static inventory files are fine for small labs — but once your vSphere environment scales, maintaining inventory.yml manually becomes tedious and error-prone.
This is where the vmware.vmware.vms inventory Plugin comes in. It automatically pulls all your virtual machines from vCenter and makes them available to Ansible.

Let’s walk through the configuration and usage — step by step.


Step 1: Configure for Dynamic Inventory

Add an ansible.cfg in your working folder:

[defaults]
inventory = inventory/vmware_vms.yaml

Step 2: Create the Dynamic Inventory Config

Inside your project folder, create a directory called inventory/ and place a file named vmware_vms.yaml inside:

plugin: vmware.vmware.vms
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
validate_certs: False
with_tags: True

Save credentials securely using Ansible Vault or environment variables (recommended). You can also use .ini or .env files with lookup plugins.

Pro tip: For safety and security, never hardcode your vSphere credentials!


Step 3: Test the Inventory Plugin

Use the ansible-inventory command to test if your dynamic inventory is working:

ansible-inventory -i inventory/vmware_vms.yaml --list

If everything is set up correctly, you’ll see a JSON output of all your virtual machines grouped by power state and tags.


What You Learned

You’ve now replaced static inventory with a live dynamic view of your vSphere infrastructure. This is a crucial step toward scalable and resilient Ansible automation.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert