Post

Ansible Part 0.6 - Use Python Virtual Environments

Preparing your environment to start using Ansible

Ansible Part 0.6 - Use Python Virtual Environments

Return to index

Getting started with Python Virtual Environments

You can use python virtual environments to isolate and configure your python environment to exactly what is required for your project and not interfere with other python projects (or let them interfere with your project).

Python Documentation: Virtual Environments

NOTE: It is not a good idea to commit your virtual environment to a repo, I would prefer providing set up instructions so the user can set up accordingly.

Set up a virtual environment on your control host

On your control host execute the following tasks.

1
2
3
4
5
6
7
# Install the necessary packages
sudo apt install python3 python3-venv

# Create your virtual environment
python3 -m venv <insert-venv-name>

# e.g. python3 -m venv learn-ansible-venv

Activate your virtual environment and install packages

1
2
3
4
5
6
7
# Activate the venv
source <insert-venv-name>/bin/activate

# e.g. source learn-ansible-venv/bin/activate

# install dependencies
pip install ansible-core --upgrade 

EXPECTED OUTPUT: when activated you should see your virtual environment in your terminal prompt.

1
2
3
4
5
user@mycomputer:~$

source learn-ansible-venv/bin/activate

(learn-ansible-venv) user@mycomputer:~$ 
This post is licensed under CC BY 4.0 by the author.

Trending Tags