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.

May 11, 2022
Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons

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.

The Best Resources For Ansible

Certifications

Video Course

Printed Book

eBooks

demo

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 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=0

Install 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.3

Install community.vmware collection

$ ansible-galaxy install -r requirements.yml

Ansible code

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

code with ❤️ in GitHub

Recap

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

Academy

Learn the Ansible automation technology with some real-life examples in my

My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps

BUY the Complete PDF BOOK to easily Copy and Paste the 250+ Ansible code

Want to keep this project going? Please donate

Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons
Follow me

Subscribe not to miss any new releases