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.

Join 50+ hours of courses in our exclusive community

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

$ 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

[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 install pyvmomi
Collecting pyvmomi
  Using cached pyvmomi-7.0.3.tar.gz (592 kB)
  Preparing metadata (setup.py) ... done
Requirement already satisfied: requests>=2.3.0 in ./venv/lib/python3.8/site-packages (from pyvmomi) (2.28.0)
Collecting six>=1.7.3
  Using cached six-1.16.0-py2.py3-none-any.whl (11 kB)
Requirement already satisfied: certifi>=2017.4.17 in ./venv/lib/python3.8/site-packages (from requests>=2.3.0->pyvmomi) (2022.6.15)
Requirement already satisfied: charset-normalizer~=2.0.0 in ./venv/lib/python3.8/site-packages (from requests>=2.3.0->pyvmomi) (2.0.12)
Requirement already satisfied: idna<4,>=2.5 in ./venv/lib/python3.8/site-packages (from requests>=2.3.0->pyvmomi) (3.3)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in ./venv/lib/python3.8/site-packages (from requests>=2.3.0->pyvmomi) (1.26.9)
Using legacy 'setup.py install' for pyvmomi, since package 'wheel' is not installed.
Installing collected packages: six, pyvmomi
  Running setup.py install for pyvmomi ... done
Successfully installed pyvmomi-7.0.3 six-1.16.0
(venv) [devops@demo ~]$ pip3.8 install --upgrade git+https://github.com/vmware/vsphere-automation-sdk-python.git
Collecting git+https://github.com/vmware/vsphere-automation-sdk-python.git
  Cloning https://github.com/vmware/vsphere-automation-sdk-python.git to /tmp/pip-req-build-_g3pilmu
  Running command git clone --filter=blob:none --quiet https://github.com/vmware/vsphere-automation-sdk-python.git /tmp/pip-req-build-_g3pilmu
  Resolved https://github.com/vmware/vsphere-automation-sdk-python.git to commit 19e521a2741dd0ca6e1a4fe671ff520bef615b32
  Preparing metadata (setup.py) ... done
Processing //tmp/pip-req-build-_g3pilmu/lib/vapi-runtime/vapi_runtime-2.34.0-py2.py3-none-any.whl
Processing //tmp/pip-req-build-_g3pilmu/lib/vapi-client-bindings/vapi_client_bindings-3.8.0-py2.py3-none-any.whl
Processing //tmp/pip-req-build-_g3pilmu/lib/vapi-common-client/vapi_common_client-2.34.0-py2.py3-none-any.whl
Processing //tmp/pip-req-build-_g3pilmu/lib/vmc-client-bindings/vmc_client_bindings-1.59.0-py2.py3-none-any.whl
Processing //tmp/pip-req-build-_g3pilmu/lib/nsx-python-sdk/nsx_python_sdk-3.1.5.0.0-py2.py3-none-any.whl
Processing //tmp/pip-req-build-_g3pilmu/lib/nsx-policy-python-sdk/nsx_policy_python_sdk-3.1.5.0.0-py2.py3-none-any.whl
Processing //tmp/pip-req-build-_g3pilmu/lib/nsx-vmc-policy-python-sdk/nsx_vmc_policy_python_sdk-3.1.5.0.0-py2.py3-none-any.whl
Processing //tmp/pip-req-build-_g3pilmu/lib/nsx-vmc-aws-integration-python-sdk/nsx_vmc_aws_integration_python_sdk-3.1.5.0.0-py2.py3-none-any.whl
Processing //tmp/pip-req-build-_g3pilmu/lib/vmc-draas-client-bindings/vmc_draas_client_bindings-1.19.0-py2.py3-none-any.whl
Collecting lxml>=4.3.0
  Using cached lxml-4.9.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_24_x86_64.whl (6.9 MB)
Requirement already satisfied: pyVmomi>=6.7 in ./venv/lib/python3.8/site-packages (from vSphere-Automation-SDK==1.77.0) (7.0.3)
Requirement already satisfied: requests>=2.3.0 in ./venv/lib/python3.8/site-packages (from pyVmomi>=6.7->vSphere-Automation-SDK==1.77.0) (2.28.0)
Requirement already satisfied: six>=1.7.3 in ./venv/lib/python3.8/site-packages (from pyVmomi>=6.7->vSphere-Automation-SDK==1.77.0) (1.16.0)
Requirement already satisfied: setuptools in ./venv/lib/python3.8/site-packages (from vapi-runtime@ file://localhost//tmp/pip-req-build-_g3pilmu/lib/vapi-runtime/vapi_runtime-2.34.0-py2.py3-none-any.whl->vSphere-Automation-SDK==1.77.0) (41.6.0)
Collecting pyOpenSSL>=18.0.0
  Using cached pyOpenSSL-22.0.0-py2.py3-none-any.whl (55 kB)
Collecting cryptography>=35.0
  Downloading cryptography-37.0.2-cp36-abi3-manylinux_2_24_x86_64.whl (4.0 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 4.0/4.0 MB 1.0 MB/s eta 0:00:00
Requirement already satisfied: certifi>=2017.4.17 in ./venv/lib/python3.8/site-packages (from requests>=2.3.0->pyVmomi>=6.7->vSphere-Automation-SDK==1.77.0) (2022.6.15)
Requirement already satisfied: charset-normalizer~=2.0.0 in ./venv/lib/python3.8/site-packages (from requests>=2.3.0->pyVmomi>=6.7->vSphere-Automation-SDK==1.77.0) (2.0.12)
Requirement already satisfied: idna<4,>=2.5 in ./venv/lib/python3.8/site-packages (from requests>=2.3.0->pyVmomi>=6.7->vSphere-Automation-SDK==1.77.0) (3.3)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in ./venv/lib/python3.8/site-packages (from requests>=2.3.0->pyVmomi>=6.7->vSphere-Automation-SDK==1.77.0) (1.26.9)
Collecting cffi>=1.12
  Downloading cffi-1.15.0-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (446 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 446.7/446.7 kB 1.2 MB/s eta 0:00:00
Collecting pycparser
  Downloading pycparser-2.21-py2.py3-none-any.whl (118 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 118.7/118.7 kB 936.3 kB/s eta 0:00:00
Using legacy 'setup.py install' for vSphere-Automation-SDK, since package 'wheel' is not installed.
Installing collected packages: pycparser, lxml, cffi, cryptography, pyOpenSSL, vapi-runtime, vapi-common-client, vapi-client-bindings, vmc-draas-client-bindings, vmc-client-bindings, nsx-vmc-policy-python-sdk, nsx-vmc-aws-integration-python-sdk, nsx-python-sdk, nsx-policy-python-sdk, vSphere-Automation-SDK
  Running setup.py install for vSphere-Automation-SDK ... done
Successfully installed cffi-1.15.0 cryptography-37.0.2 lxml-4.9.0 nsx-policy-python-sdk-3.1.5.0.0 nsx-python-sdk-3.1.5.0.0 nsx-vmc-aws-integration-python-sdk-3.1.5.0.0 nsx-vmc-policy-python-sdk-3.1.5.0.0 pyOpenSSL-22.0.0 pycparser-2.21 vSphere-Automation-SDK-1.77.0 vapi-client-bindings-3.8.0 vapi-common-client-2.34.0 vapi-runtime-2.34.0 vmc-client-bindings-1.59.0 vmc-draas-client-bindings-1.19.0
(venv) [devops@demo ~]$ pip3.8 freeze > requirements.txt
  • requirements.txt
certifi==2022.6.15
cffi==1.15.0
charset-normalizer==2.0.12
cryptography==37.0.2
idna==3.3
lxml==4.9.0
nsx-policy-python-sdk @ file://localhost//tmp/pip-req-build-pjtbl7ev/lib/nsx-policy-python-sdk/nsx_policy_python_sdk-3.1.5.0.0-py2.py3-none-any.whl
nsx-python-sdk @ file://localhost//tmp/pip-req-build-pjtbl7ev/lib/nsx-python-sdk/nsx_python_sdk-3.1.5.0.0-py2.py3-none-any.whl
nsx-vmc-aws-integration-python-sdk @ file://localhost//tmp/pip-req-build-pjtbl7ev/lib/nsx-vmc-aws-integration-python-sdk/nsx_vmc_aws_integration_python_sdk-3.1.5.0.0-py2.py3-none-any.whl
nsx-vmc-policy-python-sdk @ file://localhost//tmp/pip-req-build-pjtbl7ev/lib/nsx-vmc-policy-python-sdk/nsx_vmc_policy_python_sdk-3.1.5.0.0-py2.py3-none-any.whl
pycparser==2.21
pyOpenSSL==22.0.0
pyvmomi==7.0.3
requests==2.28.0
six==1.16.0
urllib3==1.26.9
vapi-client-bindings @ file://localhost//tmp/pip-req-build-pjtbl7ev/lib/vapi-client-bindings/vapi_client_bindings-3.8.0-py2.py3-none-any.whl
vapi-common-client @ file://localhost//tmp/pip-req-build-pjtbl7ev/lib/vapi-common-client/vapi_common_client-2.34.0-py2.py3-none-any.whl
vapi-runtime @ file://localhost//tmp/pip-req-build-pjtbl7ev/lib/vapi-runtime/vapi_runtime-2.34.0-py2.py3-none-any.whl
vmc-client-bindings @ file://localhost//tmp/pip-req-build-pjtbl7ev/lib/vmc-client-bindings/vmc_client_bindings-1.59.0-py2.py3-none-any.whl
vmc-draas-client-bindings @ file://localhost//tmp/pip-req-build-pjtbl7ev/lib/vmc-draas-client-bindings/vmc_draas_client_bindings-1.19.0-py2.py3-none-any.whl
vSphere-Automation-SDK==1.77.0

code with ❤️ in GitHub

Conclusion

Now you know how to configure a Python Virtual Environment for Ansible 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 Udemy 300+ Lessons Video Course.

BUY the Complete Udemy 300+ Lessons Video Course

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

Patreon Buy me a Pizza