Bootstrap FreeKB - Ansible - Install Ansible in a Python virtual environment
Ansible - Install Ansible in a Python virtual environment

Updated:   |  Ansible articles

Ansible can be installed in a Python virtual environment.

For example, to create a python2 virtual environment.

python2 -m virtualenv python2_virtual_environment 

 

Or a python3 virtual environment.

python3 -m venv python3_virtual_environment 

 

Activate the virtual environment.

source python3_virtual_environment/bin/activate

 

Upgrade pip in the virtual environment.

python3_virtual_environment/bin/python3 -m pip install --upgrade pip

 

Use pip list to list the packages in the virtual environment.

~]$ python3_virtual_environment/bin/python3 -m pip list
Package    Version
---------- -------
pip        23.3.1
setuptools 56.0.0

 

If the wheel package is not installed, something like this will be returned. Ansible will still be installed.

Using legacy 'setup.py install' for ansible, since package 'wheel' is not installed.

 

However, so as to avoid using legacy setup.py, let's install the wheel package.

python3_virtual_environment/bin/python3 -m pip install wheel

 

pip list should now include wheel.

~]$ python3_virtual_environment/bin/python3 -m pip list
Package    Version
---------- -------
pip        23.3.1
setuptools 56.0.0
wheel      0.41.3

 

The pip install command can be used to install Ansible in your virtual environment.

python3_virtual_environment/bin/python3 -m pip install ansible-core

 

pip list should now include a number of packages.

~]$ python3_virtual_environment/bin/python3 -m pip list
Package             Version
------------------- -------
ansible-core        2.15.6
cffi                1.16.0
cryptography        41.0.5
importlib-resources 5.0.7
Jinja2              3.1.2
MarkupSafe          2.1.3
packaging           23.2
pip                 23.3.1
pycparser           2.21
PyYAML              6.0.1
resolvelib          1.0.1
setuptools          56.0.0
wheel               0.41.3

 

And the Ansible CLI's should be in the bin directory.

~]$ ll python3_virtual_environment/bin/
total 88
-rw-r--r--. 1 john.doe users 1939 Nov 15 04:22 activate
-rw-r--r--. 1 john.doe users  888 Nov 15 04:22 activate.csh
-rw-r--r--. 1 john.doe users 2028 Nov 15 04:22 activate.fish
-rw-r--r--. 1 john.doe users 8834 Nov 15 04:22 Activate.ps1
-rwxr-xr-x. 1 john.doe users  253 Nov 15 04:25 ansible
-rwxr-xr-x. 1 john.doe users  254 Nov 15 04:25 ansible-config
-rwxr-xr-x. 1 john.doe users  283 Nov 15 04:25 ansible-connection
-rwxr-xr-x. 1 john.doe users  255 Nov 15 04:25 ansible-console
-rwxr-xr-x. 1 john.doe users  251 Nov 15 04:25 ansible-doc
-rwxr-xr-x. 1 john.doe users  254 Nov 15 04:25 ansible-galaxy
-rwxr-xr-x. 1 john.doe users  257 Nov 15 04:25 ansible-inventory
-rwxr-xr-x. 1 john.doe users  256 Nov 15 04:25 ansible-playbook
-rwxr-xr-x. 1 john.doe users  252 Nov 15 04:25 ansible-pull
-rwxr-xr-x. 1 john.doe users 1737 Nov 15 04:25 ansible-test
-rwxr-xr-x. 1 john.doe users  253 Nov 15 04:25 ansible-vault
-rwxr-xr-x. 1 john.doe users  258 Nov 15 04:23 pip
-rwxr-xr-x. 1 john.doe users  258 Nov 15 04:23 pip3
-rwxr-xr-x. 1 john.doe users  258 Nov 15 04:23 pip3.11
-rwxr-xr-x. 1 john.doe users  258 Nov 15 04:23 pip3.9
lrwxrwxrwx. 1 john.doe users    7 Nov 15 04:22 python -> python3
lrwxrwxrwx. 1 john.doe users   16 Nov 15 04:22 python3 -> /usr/bin/python3
lrwxrwxrwx. 1 john.doe users    7 Nov 15 04:22 python3.9 -> python3
-rwxr-xr-x. 1 john.doe users  245 Nov 15 04:24 wheel

 

You may want to also want to create a directory for collections.

mkdir python3_virtual_environment/collections

 

And place a requirements.txt file in the collections directory.

python3_virtual_environment/collections/requirements.yml

 

Let's say the requirements.yml file contains the following collections.

---
collections:
  - ansible.posix
  - community.general

 

You would then use the ansible-galaxy collection install command with the -r or --requirements-file option following by the path to your requirements.yml file.

ansible-galaxy collection install -r python3_virtual_environment/collections/requirements.yml

 

Then the ansible-galaxy collection list command can be used to list the installed collections.

~]$ ansible-galaxy collection list --collections-path python3_virtual_environment/collections
Collection        Version
----------------- -------
amazon.aws        6.1.0
community.docker  1.9.0
community.general 4.0.2

 

Last but not least, use the deactivate command to deactivate your Python virtual environment.

deactivate

 

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter a47132 in the box below so that we can be sure you are a human.