Configuring Ansible for VMware: Complete Setup Guide & Playbook
By Luca Berton · Published 2024-01-01 · Category: installation
Learn how to set up Ansible for VMware with pyVmomi and the community.vmware collection. Follow our guide to configure and run your first VMware playbook.

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.See also: Gather VMware ESX/ESXi Host Info with Ansible
Configure Ansible for VMware
- vSphere 8.0 and 7.0 (currently supported releases)
- Python 3.9+ with the
pyVmomiSDK - Ansible collection
community.vmware
community.vmware collection targets the vSphere releases that VMware (now Broadcom) still supports: vSphere 8.0 and 7.0. Earlier releases such as 6.7, 6.5, and the 5.x line have all reached end of general support, so plan an upgrade if you are still running them.
Ansible VMware modules are written on top of pyVmomi, the Python SDK for the VMware vSphere API that lets you manage ESXi and vCenter infrastructure.
This library talks to the vSphere API so your Ansible code can manage ESXi hosts, clusters, datacenters, and virtual machines.
Current pyVmomi releases require Python 3.x; the collection itself is tested against Python 3.9 and newer.
The community.vmware collection of modules and plugins manages a wide range of operations on the target ESXi or vCenter server.
As the name suggests, it is community-maintained rather than maintained directly by the core Ansible engineering team.
> Note: A REST-based alternative, the vmware.vmware_rest collection, talks to the vSphere REST API and does not need pyVmomi. For new automation, evaluate both: community.vmware has the widest module coverage, while vmware.vmware_rest avoids the pyVmomi dependency.
Links
## Playbook
How to configure Ansible for VMware:
- Install pyVmomi
pyVmomi - the VMware vSphere API Python Bindings.
- Install
community.vmwarecollection
community.vmware collection.
- Inventory & Playbook
vmware_guest_info module to verify the successful configuration.
missing Python PyVmomi library error
$ 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 /usr/libexec/platform-python. Please read module documentation and install in the appropriate location. If the required library is installed, but Ansible is using the wrong Python interpreter, please consult the documentation on ansible_python_interpreter"}
PLAY RECAP ****************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0Install PyVmomi
$ sudo su
[root@demo vmware]# pip3 install PyVmomi
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead.
Collecting PyVmomi
Using cached https://files.pythonhosted.org/packages/d5/d1/effec9e03f9c0a0eba9c03ba8708807bad7b335341bf755cd88d110ce29d/pyvmomi-7.0.3.tar.gz
Requirement already satisfied: requests>=2.3.0 in /usr/lib/python3.6/site-packages (from PyVmomi)
Requirement already satisfied: six>=1.7.3 in /usr/lib/python3.6/site-packages (from PyVmomi)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /usr/lib/python3.6/site-packages (from requests>=2.3.0->PyVmomi)
Requirement already satisfied: idna<2.8,>=2.5 in /usr/lib/python3.6/site-packages (from requests>=2.3.0->PyVmomi)
Requirement already satisfied: urllib3<1.25,>=1.21.1 in /usr/lib/python3.6/site-packages (from requests>=2.3.0->PyVmomi)
Installing collected packages: PyVmomi
Running setup.py install for PyVmomi ... done
Successfully installed PyVmomi-7.0.3Install community.vmware collection
$ ansible-galaxy install -r requirements.ymlAnsible code
- vm_info.yml
---
- name: info vm Playbook
hosts: localhost
become: false
gather_facts: false
pre_tasks:
- include_vars: vars.yml
tasks:
- name: get VM info
community.vmware.vmware_guest_info:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
datacenter: "{{ vcenter_datacenter }}"
validate_certs: "{{ vcenter_validate_certs }}"
name: "{{ vm_name }}"
register: detailed_vm_info
- name: print VM info
ansible.builtin.debug:
var: detailed_vm_info- vars.yml
---
vcenter_hostname: "vmware.example.com"
vcenter_datacenter: "vmwaredatacenter"
vcenter_validate_certs: false
vcenter_username: "username@vsphere.local"
vcenter_password: "MySecretPassword123"
vm_name: "myvm"- inventory
localhost- requirements.yml
---
collections:
- name: community.vmwareAnsible execution
$ ansible-playbook -i inventory vm_info.yml
PLAY [info vm Playbook] *******************************************************************************
TASK [include_vars] *******************************************************************************
ok: [localhost]
TASK [get VM info] ********************************************************************************
ok: [localhost]
TASK [print VM info] ******************************************************************************
ok: [localhost] => {
"detailed_tag_info": {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"failed": false,
"instance": {
"annotation": "",
"current_snapshot": null,
"customvalues": {},
"guest_consolidation_needed": false,
"guest_question": null,
"guest_tools_status": "guestToolsNotRunning",
"guest_tools_version": "0",
"hw_cluster": "prod-cluster",
"hw_cores_per_socket": 1,
"hw_datastores": [
"Datastore-1"
],
"hw_esxi_host": "vmware.example.com",
"hw_eth0": {
"addresstype": "assigned",
"ipaddresses": null,
"label": "Network adapter 1",
"macaddress": "00:50:56:a5:fd:4a",
"macaddress_dash": "00-50-56-a5-fd-4a",
"portgroup_key": null,
"portgroup_portkey": null,
"summary": "VM Network"
},
"hw_files": [
"[Datastore-1] myvm/myvm.vmx",
"[Datastore-1] myvm/myvm.vmsd",
"[Datastore-1] myvm/myvm.vmdk"
],
"hw_folder": "/vmwaredatacenter/vm/myvm",
"hw_guest_full_name": null,
"hw_guest_ha_state": null,
"hw_guest_id": null,
"hw_interfaces": [
"eth0"
],
"hw_is_template": false,
"hw_memtotal_mb": 1024,
"hw_name": "myvm",
"hw_power_status": "poweredOff",
"hw_processor_count": 1,
"hw_product_uuid": "422549b9-7e76-fb2b-da34-e9c6c8b071de",
"hw_version": "vmx-11",
"instance_uuid": "5025d22d-cea7-4d1c-41f9-5cd80b9603dc",
"ipv4": null,
"ipv6": null,
"module_hw": true,
"moid": "vm-17923",
"snapshots": [],
"vimref": "vim.VirtualMachine:vm-17923",
"vnc": {}
}
}
}
PLAY RECAP ****************************************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0See also: How to Add a Disk to a VMware VM Using Ansible Playbook
Conclusion
Now you know Configure Ansible for VMware.Related Articles
Category: installation
Watch the video: Configuring Ansible for VMware: Complete Setup Guide & Playbook — Video Tutorial