Ansible Pilot

Configure Ansible for VMware - ansible collection community.vmware

How to configure Ansible to interact with VMware infrastructure - datacenter, cluster, host system, and virtual machine - python PyVmomi, and collection community.vmware and vmware_guest_info Hello World demo.

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

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.


How to configure Ansible for VMware:

  1. Install pyVmomi

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

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

  2. 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

$ ansible-playbook -i inventory vm_info.yml
PLAY [info vm demo] *******************************************************************************
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'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=0

Install PyVmomi

$ sudo su
[[email protected] 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
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 install for PyVmomi ... done
Successfully installed PyVmomi-7.0.3

Install community.vmware collection

$ ansible-galaxy install -r requirements.yml

Ansible code

- name: info vm demo
  hosts: localhost
  become: false
  gather_facts: false
    - community.vmware
    - include_vars: vars.yml
    - name: get VM 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
        var: detailed_vm_info
vcenter_hostname: ""
vcenter_datacenter: "vmwaredatacenter"
vcenter_validate_certs: false
vcenter_username: "[email protected]"
vcenter_password: "MySecretPassword123"
vm_name: "myvm"
  - name: community.vmware

Ansible execution

$ ansible-playbook -i inventory vm_info.yml
PLAY [info vm demo] *******************************************************************************
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": [
            "hw_esxi_host": "",
            "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": [
            "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=0

code with ❤️ in GitHub


Now you know Configure Ansible for VMware. Subscribe to the YouTube channel, Medium, Website and Twitter to not miss the next episode of the Ansible Pilot.


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

Want to keep this project going? Please donate

Follow me

Subscribe not to miss any new releases

May 11, 2022

FREE Top 10 Best Practices

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