Ansible troubleshooting - VMware Failed to Import PyVmomi
Let’s troubleshoot together the Ansible fatal error “Failed to import the required Python library (PyVmomi)” to find the root cause, install the required library using PIP, and successfully run our Ansible For VMware Playbook code.


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.
The Best Resources For Ansible
Video Course
Printed Book
eBooks
- Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
- Ansible For Windows By Examples: 50+ Automation Examples For Windows System Administrator And DevOps
- Ansible For Linux by Examples: 100+ Automation Examples For Linux System Administrator and DevOps
- Ansible Linux Filesystem By Examples: 40+ Automation Examples on Linux File and Directory Operation for Modern IT Infrastructure
- Ansible For Containers and Kubernetes By Examples: 20+ Automation Examples To Automate Containers, Kubernetes and OpenShift
- Ansible For Security by Examples: 100+ Automation Examples to Automate Security and Verify Compliance for IT Modern Infrastructure
- Ansible Tips and Tricks: 10+ Ansible Examples to Save Time and Automate More Tasks
- Ansible Linux Users & Groups By Examples: 20+ Automation Examples on Linux Users and Groups Operation for Modern IT Infrastructure
- Ansible For PostgreSQL by Examples: 10+ Examples To Automate Your PostgreSQL database
- Ansible For Amazon Web Services AWS By Examples: 10+ Examples To Automate Your AWS Modern Infrastructure
demo
The best way of talking about Ansible troubleshooting is to jump in a live demo to show you practically the “Failed to import the required Python library (PyVmomi)” and how to solve it!
In this demo, 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 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
fix code
[[email protected] vmware]$ 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 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 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
Recap
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.
Subscribe to the YouTube channel, Medium, Website, Twitter, and Substack 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
Donate
Want to keep this project going? Please donate