Install Ansible on Python Virtual Environment in Ubuntu 22.04

Introduction:

In the world of IT automation, Ansible is a powerful tool that allows you to manage and configure multiple systems effortlessly. To ensure a clean and isolated installation, setting up Ansible within a Python virtual environment is a recommended practice. In this concise guide, we’ll explore what a Python virtual environment is, its advantages, and the step-by-step process of installing Ansible within it on Ubuntu.

What is a Python Virtual Environment?

A Python virtual environment is a self-contained directory that encapsulates a specific Python interpreter along with its libraries and scripts. It allows you to work on Python projects with dependencies isolated from the system-wide Python installation. This ensures that your project’s dependencies won’t interfere with other Python projects.

Advantages of Using Python Virtual Environment:

  1. Isolation: Each virtual environment is independent, preventing conflicts between different projects with different dependencies.
  2. Clean Environment: It helps maintain a clean and consistent development environment for your projects.
  3. Dependency Management: Easily manage and isolate project dependencies without affecting the global Python installation.

Installation Process:

Step 1: Install Python3-venv Package

Ensure that you have Python3-venv installed, which provides support for creating virtual environments.

root@Ubuntu22:/home# apt install python3-venv
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  python3-pip-whl python3-setuptools-whl python3.10-venv
The following NEW packages will be installed:
  python3-pip-whl python3-setuptools-whl python3-venv python3.10-venv
0 upgraded, 4 newly installed, 0 to remove and 55 not upgraded.
Need to get 2,474 kB of archives.
After this operation, 2,890 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://bd.archive.ubuntu.com/ubuntu jammy-updates/universe amd64 python3-p                                                                             ip-whl all 22.0.2+dfsg-1ubuntu0.4 [1,680 kB]
Get:2 http://bd.archive.ubuntu.com/ubuntu jammy-updates/universe amd64 python3-s                                                                             etuptools-whl all 59.6.0-1.2ubuntu0.22.04.1 [788 kB]
Get:3 http://bd.archive.ubuntu.com/ubuntu jammy-updates/universe amd64 python3.1                                                                             0-venv amd64 3.10.12-1~22.04.3 [5,716 B]
Get:4 http://bd.archive.ubuntu.com/ubuntu jammy-updates/universe amd64 python3-v                                                                             env amd64 3.10.6-1~22.04 [1,038 B]
Fetched 2,474 kB in 12s (204 kB/s)
Selecting previously unselected package python3-pip-whl.
(Reading database ... 208666 files and directories currently installed.)
Preparing to unpack .../python3-pip-whl_22.0.2+dfsg-1ubuntu0.4_all.deb ...
Unpacking python3-pip-whl (22.0.2+dfsg-1ubuntu0.4) ...
Selecting previously unselected package python3-setuptools-whl.
Preparing to unpack .../python3-setuptools-whl_59.6.0-1.2ubuntu0.22.04.1_all.deb                                                                              ...
Unpacking python3-setuptools-whl (59.6.0-1.2ubuntu0.22.04.1) ...
Selecting previously unselected package python3.10-venv.
Preparing to unpack .../python3.10-venv_3.10.12-1~22.04.3_amd64.deb ...
Unpacking python3.10-venv (3.10.12-1~22.04.3) ...
Selecting previously unselected package python3-venv.
Preparing to unpack .../python3-venv_3.10.6-1~22.04_amd64.deb ...
Unpacking python3-venv (3.10.6-1~22.04) ...
Setting up python3-setuptools-whl (59.6.0-1.2ubuntu0.22.04.1) ...
Setting up python3-pip-whl (22.0.2+dfsg-1ubuntu0.4) ...
Setting up python3.10-venv (3.10.12-1~22.04.3) ...
Setting up python3-venv (3.10.6-1~22.04) ...

Step 2: Create a Virtual Environment

Navigate to the desired directory and create a virtual environment named ‘ansible-venv’.

root@Ubuntu22:/home# mkdir cisco
root@Ubuntu22:/home# ls
cisco 
root@Ubuntu22:/home# cd cisco/

root@Ubuntu22:/home/cisco# python3 -m venv cisco_project
root@Ubuntu22:/home/cisco# ls -l
total 4
drwxr-xr-x 5 root root 4096      07:37 cisco_project

Step 3: Activate the Virtual Environment

Activate the virtual environment. You should see your command prompt change to indicate the active environment.

root@Ubuntu22:/home/cisco# source cisco_project/bin/activate

Step 4: Install Ansible With the virtual environment active, install Ansible using pip.

(cisco_project) root@Ubuntu22:/home/cisco# pip install ansible
Collecting ansible
  Downloading ansible-9.1.0-py3-none-any.whl (48.1 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 48.1/48.1 MB 198.8 kB/s eta 0:00:00
Collecting ansible-core~=2.16.1
  Downloading ansible_core-2.16.2-py3-none-any.whl (2.2 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.2/2.2 MB 227.9 kB/s eta 0:00:00
Collecting jinja2>=3.0.0
  Downloading Jinja2-3.1.2-py3-none-any.whl (133 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 133.1/133.1 KB 333.4 kB/s eta 0:00:00
Collecting cryptography
  Downloading cryptography-41.0.7-cp37-abi3-manylinux_2_28_x86_64.whl (4.4 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 4.4/4.4 MB 199.1 kB/s eta 0:00:00
Collecting packaging
  Downloading packaging-23.2-py3-none-any.whl (53 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 53.0/53.0 KB 360.5 kB/s eta 0:00:00
Collecting PyYAML>=5.1
  Downloading PyYAML-6.0.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (705 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 705.5/705.5 KB 243.3 kB/s eta 0:00:00
Collecting resolvelib<1.1.0,>=0.5.3
  Downloading resolvelib-1.0.1-py2.py3-none-any.whl (17 kB)
Collecting MarkupSafe>=2.0
  Downloading MarkupSafe-2.1.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (25 kB)
Collecting cffi>=1.12
  Downloading cffi-1.16.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (443 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 443.9/443.9 KB 203.4 kB/s eta 0:00:00
Collecting pycparser
  Downloading pycparser-2.21-py2.py3-none-any.whl (118 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 118.7/118.7 KB 292.0 kB/s eta 0:00:00
Installing collected packages: resolvelib, PyYAML, pycparser, packaging, MarkupSafe, jinja2, cffi, cryptography, ansible-core, ansible
Successfully installed MarkupSafe-2.1.3 PyYAML-6.0.1 ansible-9.1.0 ansible-core-2.16.2 cffi-1.16.0 cryptography-41.0.7 jinja2-3.1.2 pac                      kaging-23.2 pycparser-2.21 resolvelib-1.0.1

Step 5: Verify Installation

Check if Ansible is successfully installed within the virtual environment. We can check Ansible Packages using Pip list and Version using ansible –version command.

(cisco_project) root@Ubuntu22:/home/cisco# pip list
Package      Version
------------ -------
ansible      9.1.0
ansible-core 2.16.2
cffi         1.16.0
cryptography 41.0.7
Jinja2       3.1.2
MarkupSafe   2.1.3
packaging    23.2
pip          22.0.2
pycparser    2.21
PyYAML       6.0.1
resolvelib   1.0.1
setuptools   59.6.0

(cisco_project) root@Ubuntu22:/home/cisco# ansible --version
ansible [core 2.16.2]
  config file = None
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/cisco/cisco_project/lib/python3.10/site-packages/ansible
  ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/cisco/cisco_project/bin/ansible
  python version = 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] (/home/cisco/cisco_project/bin/python3)
  jinja version = 3.1.2
  libyaml = True

Step 6: Deactivate the Virtual Environment

When you’re done working in the virtual environment, deactivate it.

(cisco_project) root@Ubuntu22:/home/cisco# deactivate
root@Ubuntu22:/home/cisco#

Conclusion:

Setting up Ansible within a Python virtual environment on Ubuntu is a straightforward process that ensures a clean and isolated development environment. With the advantages of isolation, cleanliness, and efficient dependency management, using virtual environments becomes a best practice in Python development. Now, you’re ready to leverage Ansible’s capabilities for streamlined IT automation within your projects.

A network engineer specializing in routing, switching, and security in multi-vendor environments.He writes easy-to-understand articles about networking like switching, routing, network setup, protocols, and security. He shares his knowledge and experience through his blog and is a mentor to many in the field of network engineering.

Leave a Comment