Skip to main content Logo (IEC resistor symbol)logo

Quis custodiet ipsos custodes?
Home | About | All pages | RSS Feed | Gopher

Ansible Inventory from VMWare

Published: 06-01-2014 | Author: Remy van Elst | Text only version of this article

Table of Contents

This is a simple script which lets you use VMWare vCenter as a dynamic inventoryfor Ansible.

Github repository
Official Website

If you like this article, consider sponsoring me by trying out a Digital OceanVPS. With this link you'll get $100 credit for 60 days). (referral link)


Install PySphere:

pip install -U pysphere

Edit the script's login data and VCenter server FQDN:

server_fqdn = ""server_username = "jdoe"server_password = "secure_passw0rd"

Test it:

Get all powered on VM's:

python2 --list


{    "no_group": {        "hosts": [            "vm0031",            "vm0032",            [...]            "vm0999"        ]    },    "local": [        ""    ]}

Get one VM:

python2 --host vm0032


{    "admin": "",    "source_database": "VMWare"}

Nonexistent VM:

python2 --host nonexist


[Error]: [Object Not Found]: Could not find a VM named 'notexist'

Do a simple ansible ping:

ANSIBLE_HOSTS="/home/remy/git/vmware-ansible/" ansible all -m ping


vm0032 | success >> {    "changed": false,    "ping": "pong"}vm0034 | success >> {    "changed": false,    "ping": "pong"}


Either export the ANSIBLE_HOSTS variable to always use the VMWare inventory:

export ANSIBLE_HOSTS="/home/remy/git/vmware-ansible/"

Or preface each ansible command:

ANSIBLE_HOSTS="/home/remy/git/vmware-ansible/" ansible all -m apt -a "upgrade=safe"


Tested with both vCenter 5.1 and 5.5, Python 2.

Does not support grouping by datacenter or resource group

Thanks to JP Mens's article for the inspiration.

Tags: ansible, deployment, devops, python, software, vmware