Ansible troubleshooting - VMware Failed to Import PyVmomi
By Luca Berton · Published 2024-01-01 · Category: installation
Let’s troubleshoot together the Ansible fatal error “Failed to import the required Python library (PyVmomi)” to find the root cause, install the required.

Ansible troubleshooting — VMware Failed to Import PyVmomi
Today we’re going to talk about Ansible troubleshooting, specifically about the “Failed to import the required Python library (PyVmomi)” message and enable Ansible For VMware.
This fatal error message happens when we are trying to execute some code against your VMware Infrastructure without the necessary Python SDK for the VMware vSphere API.
These circumstances are usually related to the configuration of your Ansible Controller node and usually are not related to Ansible Playbook.
I’m Luca Berton and welcome to today’s episode of Ansible Pilot.
## Playbook
The best way of talking about Ansible troubleshooting is to jump in a live Playbook to show you practically the “Failed to import the required Python library (PyVmomi)” and how to solve it!
In this Playbook, I’m going to reproduce the error and fix using the PIP, the Python Package Manager on a demo machine.
error execution
$ 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=0
fix code
[devops@demo vmware]$ 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
fix 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=0
See also: Ansible troubleshooting - AWS Failed to import the required Python library (botocore or boto3)
Conclusion
Now you know better how to troubleshoot the Ansible “Failed to import the required Python library (PyVmomi)” message and move forward with your Ansible For VMware project.
Related Articles
• Jinja2 templating in Ansible • organizing hosts with Ansible inventorySee also: Ansible troubleshooting - VMware Unknown error while connecting to vCenter or ESXi
Root Cause
The error Failed to import the required Python library (PyVmomi) means the pyvmomi Python package is not installed in the Python environment Ansible is using.
Fix
# Install PyVmomi
pip install pyvmomi
# If using a virtual environment
source ~/ansible-venv/bin/activate
pip install pyvmomi
# Verify installation
python3 -c "import pyVmomi; print('PyVmomi OK')"
See also: Ansible troubleshooting - VMware certificate verify failed connecting to vCenter or ESXi
Common Pitfalls
Wrong Python Environment
# Check which Python Ansible uses
ansible --version | grep python
# Install in THAT Python's pip
/usr/bin/python3 -m pip install pyvmomi
# Or set interpreter in inventory
ansible_python_interpreter: /usr/bin/python3
Version Compatibility
# For older vSphere (6.x), pin pyvmomi version
pip install 'pyvmomi>=7.0,<8.0'
# For vSphere 8.x
pip install pyvmomi
Install All VMware Dependencies
# Install the full VMware stack
pip install pyvmomi requests
ansible-galaxy collection install community.vmware
Verification Playbook
- name: Verify VMware connectivity
hosts: localhost
connection: local
tasks:
- name: Gather VMware VM info
community.vmware.vmware_vm_info:
hostname: "{{ vcenter_hostname }}"
username: "{{ vault_vcenter_user }}"
password: "{{ vault_vcenter_password }}"
validate_certs: false
register: vm_info
- name: Show VMs
ansible.builtin.debug:
msg: "{{ vm_info.virtual_machines | length }} VMs found"
FAQ
Do I need PyVmomi on the target host?
No. VMware modules run on the control node (localhost) and connect to vCenter via API. Install PyVmomi on the machine running Ansible.
Which modules need PyVmomi?
All community.vmware.* modules require PyVmomi. This includes vmware_guest, vmware_vm_info, vmware_datastore_info, etc.
Category: installation
Watch the video: Ansible troubleshooting - VMware Failed to Import PyVmomi — Video Tutorial