Skip to content

How to set up and use Python virtual environments for Ansible

Avatar photo

https://www.linkedin.com/in/gineesh/ https://twitter.com/GiniGangadharan

It’s vital to test new technology before rolling it out into your production environment. I like to use Python virtual environments provided by the venv module for developing and testing Ansible playbooks and features. Instead of using the default Python and Ansible commands installed on your system, you can easily set up multiple Python environments and test different versions of Ansible and its component libraries.

This article was originally published in Red Hat Sysadmin Blog. Follow Red Hat Sysadmin Blog for more articles.

Why use Python virtual environments for Ansible development?

Virtual environments are like a virtual machine or Linux chroot, but they create an isolated structure of lightweight directories separated from actual Python system directories. That means you can have different sets of Python environments, each with different versions of modules, files, or configurations.

Ansible is based on Python and leverages many Python modules and plugins. When you test something new, it could require you to update a module that your Ansible installation depends on. Upgrading a component that your production environment depends on defeats the purpose of testing. Still, you can have different versions of Ansible and other important Python modules in a dedicated test directory with a virtual environment.

Watch the LIVE Videos session (Recorded)

Get set up

I wrote this article for a Linux computer with Python 3.x. In this scenario, this is your Ansible development machine. First, verify the installed Python version and path:

# check Python version
$ python3 -V
Python 3.6.8

$ which python3
/usr/bin/python3

I recommend setting up a directory for the virtual environment:

$ mkdir python-venv
$ cd !$

You can skip this part if you want to keep it somewhere random, but I find it helpful to keep my consolidated test directories together.

Create a new Python virtual environment

Create a virtual environment using the python3 -m venv <environment-name> command. You can give any name to your Python virtual environment. I want to try the Ansible 2.9 version, so I named it in a way to identify the directory easily:

$ python3 -m venv ansible2.9
$ ls
ansible2.9

[ Did you know Ansible Tower uses a Python virtual environment for executing tasks? Learn more in 5 tips for configuring virtualenvs with Ansible Tower. ]

Activate a Python virtual environment

After creating a virtual environment, you must enter the environment manually. This changes your active environment variables from your current shell to those required for Python to create a virtual environment:

$ source ansible2.9/bin/activate
(ansible2.9)$ python3 -V
Python 3.6.8

Next, upgrade the preferred installer program (pip) inside your virtual environment:

(ansible2.9)$ python3 -m pip install --upgrade pip

Install Ansible in a virtual environment

With your virtual environment set up and active, you can install a dedicated version of Ansible into it. This example installs version 2.9, but you can install any version that’s current (or in development):

(ansible2.9)$ python3 -m pip install ansible==2.9
(ansible2.9)$ which ansible
~/python-venv/ansible2.9/bin/ansible

Verify your new installation:

(ansible2.9)$ ansible --version
ansible 2.9.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/devops/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/devops/python-venv/ansible2.9/lib64/python3.6/site-packages/ansible
  executable location = /home/devops/python-venv/ansible2.9/bin/ansible
  python version = 3.6.8 (default, Aug 24 2020, 17:57:11) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]

You can see that the Ansible version and path inside your Python virtual environment are different from your system installation.

Test Python modules

You can now install any libraries or modules within your virtual environment without affecting the rest of your system:

(ansible2.9)$ python3 -m pip install dnspython
Collecting dnspython
  Downloading dnspython-2.1.0-py3-none-any.whl (241 kB)
     |โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ| 241 kB 8.9 MB/s 
Installing collected packages: dnspython
Successfully installed dnspython-2.1.0
(ansible2.9)$ python3 -m pip list
Package      Version
------------ -------
ansible      2.9.0
cffi         1.14.6
cryptography 3.4.7
dnspython    2.1.0
Jinja2       3.0.1
MarkupSafe   2.0.1
pip          21.2.1
pycparser    2.20
PyYAML       5.4.1
setuptools   39.2.0

Install Ansible roles or collections

You can also install Ansible roles and collections into a Python virtual environment path for testing. For example, to test a specific Ansible Kubernetes.Core collection:

(ansible2.9)$ ansible-galaxy collection install \
  kubernetes.core:==1.2.1 -p collections

Deactivate a Python virtual environment

Once you’re finished working inside your Python virtual environment (or it’s time to switch to another Python virtual environment), you can deactivate it:

(ansible2.9)$ deactivate  

Create another Python virtual environment for Ansible 3.0

After deactivating the first virtual environment, try creating another Python virtual environment to understand the power this technology grants you. In this new environment, you can install Ansible 3.0 (or any version)โ€”even if you have a different version installed on your systemโ€”and yet another version in a separate Python virtual environment:

$ python3 -m venv ansible3.0
$ ls -1
ansible2.9
ansible3.0
$ source ansible3.0/bin/activate
(ansible3.0)$ which python
~/python-venv/ansible3.0/bin/python
(ansible3.0)$ python3 -m pip install --upgrade pip
(ansible3.0)$ python3 -m pip install ansible==3.0

Wrap up

Python virtual environments give you the freedom to test whatever version of modules, plugins, and Python packages you need. That includes vital sysadmin software like Ansible, so you can test new Ansible features without disturbing your system install.

This article was originally published in Red Hat Sysadmin Blog. Follow Red Hat Sysadmin Blog for more articles.

Disclaimer:

The views expressed and the content shared in all published articles on this website are solely those of the respective authors, and they do not necessarily reflect the views of the author’s employer or the techbeatly platform. We strive to ensure the accuracy and validity of the content published on our website. However, we cannot guarantee the absolute correctness or completeness of the information provided. It is the responsibility of the readers and users of this website to verify the accuracy and appropriateness of any information or opinions expressed within the articles. If you come across any content that you believe to be incorrect or invalid, please contact us immediately so that we can address the issue promptly.

Avatar photo


https://www.linkedin.com/in/gineesh/ https://twitter.com/GiniGangadharan
Gineesh Madapparambath is the founder of techbeatly and he is the co-author of The Kubernetes Bible, Second Edition. and the author of ๐—”๐—ป๐˜€๐—ถ๐—ฏ๐—น๐—ฒ ๐—ณ๐—ผ๐—ฟ ๐—ฅ๐—ฒ๐—ฎ๐—น-๐—Ÿ๐—ถ๐—ณ๐—ฒ ๐—”๐˜‚๐˜๐—ผ๐—บ๐—ฎ๐˜๐—ถ๐—ผ๐—ป. He has worked as a Systems Engineer, Automation Specialist, and content author. His primary focus is on Ansible Automation, Containerisation (OpenShift & Kubernetes), and Infrastructure as Code (Terraform). (aka Gini Gangadharan - iamgini.com)

Comments

1 Response

  1. […] As the world progresses, it also calls for newer and more advanced skills to be taught to the younger generation. It is to better prepare them with the essential abilities that are in demand in the current job marketโ€”one of which is: coding using python. […]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.