Ansible Pilot

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.

June 20, 2022
Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons

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

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.

The Best Resources For Ansible

Certifications

Video Course

Printed Book

eBooks

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

[inventory]
enable_plugins = vmware_vm_inventory
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

code with ❤️ in GitHub

Recap

Now you know how to Configure Ansible Dynamic Inventory For VMware.

Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) 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

BUY the Complete PDF BOOK to easily Copy and Paste the 250+ Ansible code

Want to keep this project going? Please donate

Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons
Follow me

Subscribe not to miss any new releases