Ansible in a Python virtual env

Requirements

I use an Ubuntu machine running within WSL2 (see also WSL2 on Windows 10)

Install requirements and

sudo apt install -y python3-venv

Create your venv

mkdir ~/virtualenvs
cd ~/virtualenvs
python3 -m venv ansible

restart Ubuntu running in WSL2.

Using a venv

Install ansible in your venv

source ~/virtualenvs/ansible/bin/activate
pip3 install wheel
pip3 install ansible

ansible --version

Use deactivate to leave the python virtual env

(ansible) user@HOST_NAME:~/virtualenvs$ deactivate
user@HOST_NAME:~/virtualenvs$

You can see the actual functionality of the venv with which python and in your $PATH variable. Python is now used from an alternate locaton bin folder and this is also placed in your path.

(ansible) user@HOST_NAME:~/virtualenvs$ which python
/home/user/virtualenvs/ansible/bin/python
(ansible) user@HOST_NAME:~/virtualenvs$ echo $PATH | tr ":" "\n" | grep virtualenv
/home/user/virtualenvs/ansible/bin
comments powered by Disqus