AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Configure Ansible Dynamic Inventory for VMware in Simple Steps

By Luca Berton · Published 2024-01-01 · Category: installation

Discover how to configure Ansible Dynamic Inventory for VMware to automate and manage virtual machines efficiently. Step-by-step guide with Playbook included.

Configure Ansible Dynamic Inventory for VMware in Simple Steps

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.

See also: How to Add a Disk to a VMware VM Using Ansible Playbook

Ansible Dynamic Inventory for VMware

Functionality: Dynamically retrieves virtual machines as inventory hosts from the VMware environment. • Plugin: community.vmware.vmware_vm_inventoryDependencies: 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

See also: Configure a Python Virtual Environment for Ansible VMware - ansible collection community.vmware

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:
  [inventory]
  enable_plugins = vmware_vm_inventory
  
inventory.vmware.yml:
  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:
  $ ansible-inventory -i inventory.vmware.yml --list
  
Output:
  {
      "VMs": {
          "hosts": [
            "myvm_42254893-3793-0e4f-9f61-7c37d244c2a8"
         ]
      },
      "_meta": {
          "hostvars": {
             "myvm_42254893-3793-0e4f-9f61-7c37d244c2a8": {
                  "config": {
                      ...
                  },
                  ...
              }
          }
      },
      "all": {
          "children": [
              "VMs",
              ...
         ]
      },
      ...
  }
  

Graphing Hosts

Command:
  $ ansible-inventory -i inventory.vmware.yml --graph
  
Output:
  @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

Conclusion

Now you know how to configure Ansible Dynamic Inventory for VMware, enabling you to automate and manage your VMware virtual machines efficiently.

See also: Stop a VMware vSphere Virtual Machine - Ansible module vmware_guest_powerstate

Related Articles

building an Ansible inventory

Complete Inventory Configuration

# vmware_inventory.yml
plugin: community.vmware.vmware_vm_inventory
strict: false
hostname: vcenter.example.com
username: "{{ vault_vcenter_user }}"
password: "{{ vault_vcenter_password }}"
validate_certs: false

# Group VMs by properties keyed_groups: - key: config.guestId separator: "" - key: guest.guestFamily prefix: family - key: summary.runtime.powerState prefix: power

# Filter VMs filters: - summary.runtime.powerState == "poweredOn"

# Compose variables compose: ansible_host: guest.ipAddress ansible_user: "'ansible'" datacenter: config.datacenter

Using the Dynamic Inventory

# Test the inventory
ansible-inventory -i vmware_inventory.yml --list
ansible-inventory -i vmware_inventory.yml --graph

# Run playbook with VMware inventory ansible-playbook -i vmware_inventory.yml site.yml

# Target specific groups ansible family_linuxGuest -i vmware_inventory.yml -m ansible.builtin.ping

Managing VMware VMs with Ansible

- name: Manage VMware infrastructure
  hosts: localhost
  connection: local
  tasks:
    - name: Create a VM from template
      community.vmware.vmware_guest:
        hostname: "{{ vcenter_hostname }}"
        username: "{{ vault_vcenter_user }}"
        password: "{{ vault_vcenter_password }}"
        validate_certs: false
        name: new-server
        template: ubuntu-template
        datacenter: DC1
        cluster: Cluster1
        folder: /DC1/vm/Linux
        state: poweredon
        hardware:
          memory_mb: 4096
          num_cpus: 2
        networks:
          - name: VM Network
            ip: 10.0.0.100
            netmask: 255.255.255.0
            gateway: 10.0.0.1

FAQ

How do I install the VMware collection?

ansible-galaxy collection install community.vmware
pip install pyvmomi

Can I use tags to group VMs?

Yes. Use keyed_groups with config.tag or custom attributes to create dynamic groups based on VMware tags.

How do I handle vCenter certificates?

Set validate_certs: false for testing, or install the vCenter CA certificate and set validate_certs: true for production.

Category: installation

Watch the video: Configure Ansible Dynamic Inventory for VMware in Simple Steps — Video Tutorial

Browse all Ansible tutorials · AnsiblePilot Home