6 ways to install Ansible on Ubuntu 20.04

Ansible is the most popular Configuration Management tool. There are several ways to install it on Ubuntu 20.04. Each with its advantages and disadvantages.

Every approach has its use case and side effects. Choose the one that fits your needs best.

Default repository

The quickest and easiest way to install Ansible on Ubuntu 20.04 is to install it from the default repository.

Use case: Trying Ansible for the first time. Simple Configuration Management.

Advantages: Simple, secure, stable

Disadvantages: Old version that misses new features

sudo apt install ansible

That command will make sure all python dependencies are installed as deb packages as well and everything will work out of the box. If we are trying Ansible for the first time this is the way to go.

However, a quick look with ansible --version shows that we installed version 2.9 that is not under active development anymore and we may run into the situation where we miss some new features that we read about on the internet.

Repository from newer release

If we prefer to install a newer version but want to pull it from an official Ubuntu repository (e.g. from Ubuntu 21.04) we can do that by adding the following lines to our sources.list

Use case: Need of a newer version (new features) but do not want to add third party repositories.

Advantages: Using an official Ubuntu repository, No need to add third party sources

Disadvantages: Can lead to problems with python dependencies on our system.

deb http://archive.ubuntu.com/ubuntu/ hirsute universe
deb http://archive.ubuntu.com/ubuntu/ hirsute-updates universe
deb http://security.ubuntu.com/ubuntu hirsute-security universe

After running apt update we can check which Ansible versions are available with

$ apt policy ansible
  Installed: (none)
  Candidate: 2.10.7-1
  Version table:
     2.10.7-1 500
        500 http://archive.ubuntu.com/ubuntu hirsute/universe amd64 Packages
        500 http://archive.ubuntu.com/ubuntu hirsute/universe i386 Packages
     2.9.6+dfsg-1 500
        500 http://archive.ubuntu.com/ubuntu focal/universe amd64 Packages
        500 http://archive.ubuntu.com/ubuntu focal/universe i386 Packages

The problem with adding the hirsute repository is that we can get unwanted side effects. A system upgrade may pull additional packages from that repository and we can easily mess things up in our systems with that approach.

One way to avoid these side effects is to remove the extra repositories from our sources.list after we installed Ansible. By doing that we do not get upgrades anymore.

Another better approach is to set Pin Priorities in /etc/apt/preferences.

Package: ansible* 
Pin: release v=21.04, l=Ubuntu
Pin-Priority: 500

Package: * 
Pin: release v=21.04, l=Ubuntu
Pin-Priority: -1

Official Ansible PPA

Use case: Need of the latest features

Advantages: Newest features, security upgrades

Disadvantages: Third party repository necessary

Instead of adding the hirsute Repository we can add the official PPA that allows us to install an even newer version that is tested with our Ubuntu release.

sudo add-apt-repository ppa:ansible/ansible
apt update
apt install ansible-base

Pip installation (global)

Use case: Need of latest features and flexibility.

Advantages: Get the newest features. Extra packages available that do not come pre-packages as deb

Disadvantages: No security upgrades with apt upgrade.

If we want to get the newest version of ansible (which is 4.1 as of today) we should install it via pip. That allows us to get the most recent features but comes at the cost of more complexity. We have to do some additional settings and we do not get security patches via apt anymore.

There are several ways to install ansible via pip. Doing it globally makes it available to all users.

sudo apt install python3-pip
pip3 install ansible

Executables go into /usr/local/bin which is in our $PATH by default.

Pip installation (per user)

Use case: Different users have their own ansible version independent from each other.

Advantages: Can try new features and install additional pip packages without interfering with other users

Disadvantages: Every user has to manage its installation separately.

Instead of a global installation it can also be installed in the user home so every user has its own version.

sudo apt install python3-pip
pip3 install --user ansible

Exectuables go into ~/.local/bin so we should add that to our $PATH.

Pip installation in a venv

Use case: Testing different setups. Testing playbooks with different versions. Preparing a version upgrade

Advantages: Can try new features and switch Ansible versions quickly

Disadvantages: Each venv has to be managed separately which incurrs a lot of overhead.

sudo apt install python3-pip
sudo apt install python3-virtualenv
virtualenv ansible41
source ~/ansible41/bin/activate
pip3 install ansible

Especially when it comes to managing multiple Ansible versions in separate venvs we should not do this manually. At may look stupid to have an Ansible playbook that installs ansible but when it comes to these kinds of setups it actually makes a lot of sense.