How to configure a Python Virtual Environment for Ansible VMware?

Using a Python Virtual Environment is a convenient way to maintain up-to-date the Python dependency of the Ansible collection community.vmware without interfere with your Linux system/

This initial configuration sometimes is a roadblock for some VMware users to start using Ansible.

I'm Luca Berton and welcome in today's episode of Ansible Pilot.

Links

  • [Ansible collection community.vmware](https://docs.ansible.com/ansible/latest/collections/community/vmware/index.html)
  • [Python pyVmomi](https://github.com/vmware/pyvmomi)
  • [VMware vSphere Automation SDK for Python](https://github.com/vmware/vsphere-automation-sdk-python)

Playbook

Configure a Python Virtual Environment for Ansible VMware:

  • pyVmomi
  • requests
  • VMware vSphere Automation SDK for Python

How to Python Virtual Environment for Ansible VMware.

I'm going to show you how to configure a Python Virtual Environment for Ansible VMware to successfully use the Ansible collection community.vmware of modules and plugins to manages various operations related to virtual machines in the given ESXi or vCenter server.

Ansible VMware modules are written on top of pyVmomi Python SDK for the VMware vSphere API that allows user to manage ESX, ESXi, and vCenter infrastructure.

Other useful libraries are requests and VMware vSphere Automation SDK for Python used for some additional features such as list tags in the Ansible Dynamic Inventory vmware_vm_inventory plugin.

code

``bash

$ python3.8 -m venv venv

$ source venv/bin/activate

(venv) $ pip3.8 install --upgrade pip

(venv) $ pip3.8 install requests

(venv) $ pip3.8 install pyvmomi

(venv) $ pip3.8 install --upgrade git+https://github.com/vmware/vsphere-automation-sdk-python.git

`

execution

``bash

[devops@demo ~]$ python3.8 -m venv venv

[devops@demo ~]$ source venv/bin/activate

(venv) [devops@demo ~]$ pip install -U pip

Collecting pip

Using cached https://files.pythonhosted.org/packages/96/2f/caec18213f6a67852f6997fb0673ae08d2e93d1b81573edb93ba4ef06970/pip-22.1.2-py3-none-any.whl

Installing collected packages: pip

Found existing installation: pip 19.3.1

Uninstalling pip-19.3.1:

Successfully uninstalled pip-19.3.1

Successfully installed pip-22.1.2

(venv) [devops@demo ~]$ pip install requests

Collecting requests

Using cached requests-2.28.0-py3-none-any.whl (62 kB)

Collecting idna<4,>=2.5

Using cached idna-3.3-py3-none-any.whl (61 kB)

Collecting urllib3<1.27,>=1.21.1

Using cached urllib3-1.26.9-py2.py3-none-any.whl (138 kB)

Collecting charset-normalizer~=2.0.0

Using cached charset_normalizer-2.0.12-py3-none-any.whl (39 kB)

Collecting certifi>=2017.4.17

Using cached certifi-2022.6.15-py3-none-any.whl (160 kB)

Installing collected packages: urllib3, idna, charset-normalizer, certifi, requests

Successfully installed certifi-2022.6.15 charset-normalizer-2.0.12 idna-3.3 requests-2.28.0 urllib3-1.26.9

(venv) [devops@demo ~]$ pip ins