Installing Ansible

Ansible is an open-source automation tool for configuration management, application deployment and routine administration. It is agentless: it connects to managed hosts over SSH, so you only need to install it on one control node. This guide shows how to install Ansible on Red Hat Enterprise Linux (RHEL) and compatible systems such as Rocky Linux, AlmaLinux and CentOS Stream.

Choose the method that matches your environment. On RHEL 8 and 9, the package-manager method is the simplest and is supported by Red Hat. Use the pip method when you need a specific or newer Ansible release.

Prerequisites

  • A RHEL 8 or RHEL 9 system (or a compatible distribution) to act as the control node.
  • A user with sudo privileges.
  • Internet access, or access to a local repository mirror.
  • For RHEL, an active subscription so that the system can use Red Hat repositories.

Start by bringing the system up to date:

sudo dnf -y update

Method 1: Install with the package manager (recommended)

Option A: ansible-core from the AppStream repository

RHEL 8.6 and later, and RHEL 9, include ansible-core in the standard AppStream repository. It provides the ansible command-line tools and the built-in modules.

sudo dnf -y install ansible-core

Option B: the full Ansible package from EPEL

The community ansible package adds a large set of collections (modules for cloud platforms, network devices, databases and more). It is available from EPEL (Extra Packages for Enterprise Linux), a repository maintained by the Fedora Project community that provides add-on packages not included in RHEL.

On RHEL 9, enable the CodeReady Builder repository first, because some EPEL packages depend on it:

sudo subscription-manager repos --enable codeready-builder-for-rhel-9-$(arch)-rpms

Then install the EPEL release package and Ansible:

sudo dnf -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm
sudo dnf -y install ansible

On Rocky Linux or AlmaLinux, you can enable EPEL with sudo dnf -y install epel-release and enable CRB with sudo dnf config-manager --set-enabled crb. For RHEL 8, replace 9 with 8 in the repository name and URL.

Method 2: Install with pip in a Python virtual environment

Installing with pip gives you the latest Ansible release from the Python Package Index (PyPI). Using a virtual environment keeps Ansible and its Python libraries separate from the packages the operating system depends on.

Step 1: Install Python and development tools

The Development Tools group provides compilers and build utilities that some Python packages need when they are built from source.

sudo dnf -y groupinstall "Development Tools"
sudo dnf -y install python3 python3-pip python3-devel

Step 2: Create and activate a virtual environment

python3 -m venv ~/ansible-venv
source ~/ansible-venv/bin/activate

Step 3: Upgrade pip and setuptools

pip install --upgrade pip setuptools

Step 4: Install Ansible

pip install ansible

Avoid running sudo pip install. Installing Python packages system-wide with pip can overwrite files managed by dnf and break system tools. Use a virtual environment, or python3 -m pip install --user ansible for a single user.

Remember to activate the environment (source ~/ansible-venv/bin/activate) in each new terminal session before running Ansible.

Verify the installation

Check the installed version:

ansible --version

The output shows the Ansible core version, the configuration file in use and the Python version. It looks similar to this (your version numbers will differ):

ansible [core 2.x.x]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/user/.ansible/plugins/modules', ...]
  ansible python module location = /usr/lib/python3.x/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.x.x

Run a quick connectivity test against the local machine:

ansible localhost -m ping

A successful result returns pong:

localhost | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

Upgrade Ansible

Use the same tool you installed with.

For a package-manager installation:

sudo dnf -y upgrade ansible-core ansible

For a pip installation (with the virtual environment activated):

pip install --upgrade ansible

Legacy systems: RHEL 6 and RHEL 7

RHEL 6 and RHEL 7 have reached the end of their standard support lifecycle, and their EPEL repositories have been moved to the Fedora archive. The download links used in older guides no longer work, and current Ansible releases require a newer Python than these systems provide. Plan to run your Ansible control node on RHEL 8 or 9. Older servers can still be managed by a modern control node.

Next steps

  • Create an inventory file that lists the servers you want to manage.
  • Set up SSH key-based authentication from the control node to those servers.
  • Write and run your first playbook with ansible-playbook.

Thank you for reading. For more articles on Unix and Linux administration, visit Unix Wikis.

For training enquiries, email ibhagat@outlook.com.

Comments