Installing Ansible correctly

Desert

The Ansible controller requires a UNIX-based OS and Python. Since Ansible runs in Python, it can (and should) be installed through pip – the package manager for Python. The optimal way to manage Python packages is by first creating and enabling a virtual environment; a disposable and OS-isolated environment locked to a specific Python interpreter.

$ python3 -m venv .venv
$ source .venv/bin/activate
$ pip install ansible-core

I source this virtual environment in my ~/.bashrc, because I’m lazy and don’t have any other virtual environments.

source_file() {
  if [ -r "$1" ]; then
    source "$1"
  fi
}

source_file "$HOME/.venv/bin/activate"

A lot of Linux distributions ship with an old version of Python. In RHEL9, the default Python implementation is version 3.9. I recommend installing the latest version of Python that Ansible supports. For example, Python 3.11 offers a significant performance increase compared to Python 3.9.

There is also an “all batteries included” version of Ansible simply called “ansible”, that can be installed with pip. This package contains ansible-core and a bunch of commonly used and verified collections. However, when building an execution environment for AAP or AWX, you must explicitly install ansible-core and whatever collections and Python packages you need – meaning you are likely better off doing the same thing on your Ansible controller.


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *