Loading...

Watch: 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.

## 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

``bash

$ 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

`bash

[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/effec9e03f9c0a0eba9c03ba870

Read the full tutorial: Ansible troubleshooting - VMware Failed to Import PyVmomi