Ansible Pilot

Ansible Dynamic Inventory For VMware - Ansible vmware_vm_inventory

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

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

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.

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"
            },
            "nashok_4225a66b-752c-cb97-133c-336179ba101c": {
                "config": {
                    "cpuHotAddEnabled": false,
                    "cpuHotRemoveEnabled": false,
                    "guestId": "rhel6Guest",
                    "hardware": {
                        "numCPU": 1
                    },
                    "instanceUuid": "5025b220-fffd-2aba-8b40-17bc68164cea",
                    "name": "nashok",
                    "template": false,
                    "uuid": "4225a66b-752c-cb97-133c-336179ba101c"
                },
                "config.cpuHotAddEnabled": false,
                "config.cpuHotRemoveEnabled": false,
                "config.guestId": "rhel6Guest",
                "config.hardware.numCPU": 1,
                "config.instanceUuid": "5025b220-fffd-2aba-8b40-17bc68164cea",
                "config.name": "nashok",
                "config.template": false,
                "config.uuid": "4225a66b-752c-cb97-133c-336179ba101c",
                "guest": {
                    "guestState": "notRunning"
                },
                "guest.guestState": "notRunning",
                "name": "nashok",
                "runtime": {
                    "connectionState": "connected"
                },
                "runtime.connectionState": "connected",
                "summary": {
                    "runtime": {
                        "powerState": "poweredOff"
                    }
                },
                "summary.runtime.powerState": "poweredOff"
            },
            "natesh-rhel7_42255ccf-1b8f-87e2-de81-76f01fefe2a8": {
                "config": {
                    "cpuHotAddEnabled": false,
                    "cpuHotRemoveEnabled": false,
                    "guestId": "rhel7_64Guest",
                    "hardware": {
                        "numCPU": 2
                    },
                    "instanceUuid": "5025aa8c-d547-72b2-b7f0-dcc8bf6a7c62",
                    "name": "natesh-rhel7",
                    "template": false,
                    "uuid": "42255ccf-1b8f-87e2-de81-76f01fefe2a8"
                },
                "config.cpuHotAddEnabled": false,
                "config.cpuHotRemoveEnabled": false,
                "config.guestId": "rhel7_64Guest",
                "config.hardware.numCPU": 2,
                "config.instanceUuid": "5025aa8c-d547-72b2-b7f0-dcc8bf6a7c62",
                "config.name": "natesh-rhel7",
                "config.template": false,
                "config.uuid": "42255ccf-1b8f-87e2-de81-76f01fefe2a8",
                "guest": {
                    "guestState": "notRunning"
                },
                "guest.guestState": "notRunning",
                "name": "natesh-rhel7",
                "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:
  |[email protected]:
  |  |--myvm_42254893-3793-0e4f-9f61-7c37d244c2a8
  |[email protected]:
  |  |--myvm_42254893-3793-0e4f-9f61-7c37d244c2a8
  |[email protected]:
  |  |--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, Website and 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: 100+ Automation Examples For Linux and Windows System Administrator and DevOps

Access the Complete Book and Learn Quick Ansible by 200+ Examples

Want to keep this project going? Please donate

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

Subscribe not to miss any new releases

June 20, 2022

FREE Top 10 Best Practices

Top 10 Best Practices of Ansible Automation: save time, reduce errors and stress