How to configure Ansible for VMware?

Ansible provides various modules to manage VMware infrastructure, which includes data center, cluster, host system, and virtual machine.

I'll show you step by step how to prepare your Ansible controller to interact with the VMware infrastructure.

This initial configuration sometimes is a roadblock for some VMware users to start using Ansible.

I'm Luca Berton and welcome to today's episode of Ansible Pilot.

Configure Ansible for VMware

  • vSphere 6.0, 5.5, 5.1 and 5.0
  • Python pyVmomi supports 2.7.x and 3.4+
  • Ansible collection community.vmware

The supported nodes include all the modern releases of VMware vSphere.

The full list includes vSphere 6.0, 5.5, 5.1, and 5.0.

Ansible VMware modules are written on top of pyVmomi. pyVmomi is the Python SDK for the VMware vSphere API that allows users 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+.

The Ansible collection community.vmware of modules and plugins manages various operations related to virtual machines in the given ESXi or vCenter server.

As the name suggests, this resource is provided with only Community Support so it's not maintained directly by the Ansible Engineer Team.

Links

  • [Introduction to Ansible for VMware](https://docs.ansible.com/ansible/latest/collections/community/vmware/docsite/vmware_scenarios/vmware_intro.html)
  • [community.vmware.vmware_guest_info](https://docs.ansible.com/ansible/latest/collections/community/vmware/vmware_guest_info_module.html)

## Playbook

How to configure Ansible for VMware:

1. Install pyVmomi

First of all, you need to install pyVmomi - the VMware vSphere API Python Bindings.

2. Install community.vmware collection

Second, you need to install the Ansible community.vmware collection.

3. Inventory & Playbook

Once everything is done on the node you could configure the Ansible inventory on the Ansible Controller machine and run your first Ansible Playbook with vmware_guest_info module to verify the successful configuration.

missing Python PyVmomi library error

```bash

$ ansible-playbook -i inventory vm_info.yml

PLAY [info vm Playbook] *

TASK [include_vars] *

ok: [localhost]

TASK [get VM info]

An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ModuleNotFoundError: No module named 'pyVim'

fatal: [localhost]: FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"}, "changed": false, "msg": "Failed to import the required Python library (PyVmomi) on demo.example.com's Python