Skip to content

Install Ansible#

Problem#

How to install Ansible?

Solution#

We create a virtual environment in Python 3, one for each project on your control host.

In your console type:

mkdir myproject
cd myproject
python3 -m venv .venv
source ./venv/bin/activate

In the requirements.txt we define the dependencies of our Python 3 project, on pypi we find the current version.

requirements.txt
ansible==x.y.z
Ansible Community Package

The ansible package from pypi is often refered as Community Package, it also contains extensions (Collections) maintained by the community. To find more about how the package is built, visit gh/ansible-community/ansible-built-data.

And install the packages using pip, the Python Package Manager:

pip3 install --upgrade -r requirements.txt

After that you will find ansible in your PATH:

which ansible

/home/resmo/Projects/myproject/.venv/bin/ansible
And you can use ansible and on the CLI:

ansible --version

To leave a virtual environment:

deactivate
To reuse an existing virtual environment:

source ./venv/bin/activate

Make sure that the virtual environment is never included in your SCM using the following global git ignore configuration:

~/.config/git/ignore
.venv

Explanation#

Ansible is written in the Python programming language and requires Python (in version 3) on the control host, the machine from which you execute your playbooks.

Python recognises so-called virtual environments, under which Python packages can be installed without being dependent on system packages. This makes Python very easy to port to other platforms and operating systems. It facilitates collaboration with your colleagues who may be developing on a different operating system and generally offers a lot of flexibility and keeps the project maintainable.

Dependencies are visualised directly in requirements.txt. If you use a versioning tool like git, you can see the history of dependencies over time. It also allows you to monitor dependencies in requirements.txt to check your project for possible security-relevant errors (dependency scanning) or to alert you to important updates. Please make sure that you exclude the virtual environment from your version management and, in the case of git, include it in the .gitignore file or even globally on your workstation for all your projects.

You can delete and recreate the virtual environment at any time, everything you need for this is in requirements.txt.

So there is a lot to be said in favour of using ‘Python virtual environment’ for Ansible too.