Home » Guides » Install Ansible On Linux: Ubuntu, Debian, CentOS, Arch Linux, RHEL, Fedora, etc.

Install Ansible On Linux: Ubuntu, Debian, CentOS, Arch Linux, RHEL, Fedora, etc.

Ansible is a smart automation tool meant for various IT tasks. It is primarily used to manage IT infrastructures, streamline operations, and automate system deployments. As such, it can be an essential tool for your organization. It can be useful whether you are a system admin or a developer. All you have to do is install Ansible on your UNIX system, and it will be good to go. As such, we have come up with the following guide to help readers install Ansible on their Linux systems, irrespective of the distribution they are running. If you want to learn more about the app, you should continue reading.

What Is Ansible?

Ansible is a popular automation tool meant for simplifying IT infrastructure management. Since it is an open-source project, it is completely free to use. The software utilizes declarative language to help describe system configurations and then automates repetitive tasks accordingly. It can even automate tasks on multiple devices and remote servers simultaneously. 

The software is widely used when developing programs. In DevOps, it is used for integration and delivery, configuration management and coding too. It also supports a wide variety of operating systems. Thus, Ansible is pretty versatile when it comes to IT infrastructure management. 

Ansible
Ansible

Features Of Ansible

When talking about Ansible, you have to also take into account the features it has to offer. 

Supports Agentless Architecture

The app is noteworthy because it can support agentless architectures just fine. This means you don’t have to install anything on the client servers to interact with Ansible. This allows for a lot more freedom for the developers, making the app software a more attractive option. Since it uses SSH for communication, it is also lightweight enough to set up easily on any device. 

Uses YAML

Next, the app uses YAML for its playbook, which is a human-readable format. This allows for a much more comprehensive rule book for users, when it comes to the desired state of systems and how to get there. Thus, even beginners can use the software without much hassle. 

Modular Structure

The software also has a vast library of modules that can be task-specific. This makes the software much more versatile, as it can be used to tackle a number of tasks. Furthermore, you can combine these modules into the playbook to create complex automation workflows. You can even create custom modules. 

Supports Different Roles

Lastly, the software also supports different roles, which is another way of organizing the playbook into custom tasks. Due to these, users can reuse codes, and the software can also help with complex management tasks by breaking them into smaller components.

Installation Requirement

There aren’t many stringent requirements for running Ansible on a machine, as long as you are using a UNIX-based system, such as RHEL, Debian, Ubuntu, BSD, or even macOS. Furthermore, you will also be able to run it as long as the device has Python installed on it. 

How To Install Ansible On Linux?

While there are many ways to install Ansible on Linux, it is recommended to use Pip or Pipx for the installation process. However, we have listed a few different methods here, such as:

Method 1. Installing Ansible On Linux with Pipx

If you are having trouble installing Ansible with Pip (we have mentioned the process in the following segment) you can try it with Pipx. Here’s how:

Step 1. Start by installing Pipx on your device:

Ubuntu 

sudo apt update
sudo apt install pipx
pipx ensurepath
sudo pipx ensurepath --global # optional to allow pipx actions with --global argument

Fedora

sudo dnf install pipx
pipx ensurepath
sudo pipx ensurepath --global # optional to allow pipx actions with --global argument

Arch Linux

sudo pacman -S python-pipx
pipx ensurepath
sudo pipx ensurepath --global # optional to allow pipx actions with --global argument

Other Linux Distros

python3 -m pip install --user pipx
python3 -m pipx ensurepath
sudo pipx ensurepath --global # optional to allow pipx actions with --global argument

Step 2. Then, you have to also install additional Python dependencies 

pipx inject ansible argcomplete

Step 3. Lastly, install Ansible

pipx install --include-deps ansible

Method 2. Installing Ansible With Pip

Now, this is the recommended method. All you have to do is:

Step 1. First, verify whether pip is already installed on your system:

python3 -m pip -V

Step 2. If it’s not, you will see an error like “No module named pip, you will need to install pip under your chosen Python interpreter before proceeding.” Thus, you will need to install an additional OS package or try installing the latest version of Pip directly from the Python Packaging Authority:

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py 
python3 get-pip.py --user

Step 3. Then, you will need additional configurations:

python3 -m pip install -r requirements.txt

Step 4. Finally, use pip to install the Ansible package:

python3 -m pip install --user ansible

Method 3. Running Ansible In Container Using EE

This method is somewhat non-traditional and is likely meant for developers or advanced users. This is when you can run Ansible within a container. All you have to do is use the Ansible container image, also called Execution Environments or EE. This image contains the following packages:

  • ansible-core
  • ansible-runner
  • Python
  • Ansible dependencies

Now, to build Ansible EE, complete the following steps to set up a local environment:

Step 1. First, install the following packages on your device:

Ubuntu/Debian

sudo apt get install podman python3

Fedora

sudo dnf install -y podman python3

Step 2. Now, install the ansible-navigator:

pip3 install ansible-navigator

Note that, this will let you run EE in the terminal. However, if you do not want this, simply run the following:

pip3 install ansible-builder

Step 3. Next, verify your environment:

ansible-navigator --version
ansible-builder --version

Step 4. Now, build your EE:

Step 4.1- For this, create a project folder on your system.

mkdir my_first_ee && cd my_first_ee

Step 4.2- Then, create a execution-environment.yml file with the specifies dependencies:

version: 3
images:
  base_image:
    name: quay.io/fedora/fedora:latest
dependencies:
  ansible_core:
    package_pip: ansible-core
  ansible_runner:
    package_pip: ansible-runner
  system:
  - openssh-clients
  - sshpass
  galaxy:
    collections:
    - name: community.postgresql

Step 4.3- Finally, build an EE container image called postgresql_ee.

ansible-builder build --tag postgresql_ee

Step 5. After that, you have to list the container images for verification:

podman image list
localhost/postgresql_ee          latest      2e866777269b  6 minutes ago  1.11 GB
less context/Containerfile

Step 6. Now, all that’s left is running the EE

Run On A Local Host

Step 6.1- First, create a test_localhost.yml playbook.

- name: Gather and print local facts
  hosts: localhost
  become: true
  gather_facts: true
  tasks:
   - name: Print facts
     ansible.builtin.debug:
      var: ansible_facts

Step 6.2- Then, run the playbook inside the postgresql_ee EE.

ansible-navigator run test_localhost.yml --execution-environment-image postgresql_ee --mode stdout --pull-policy missing --container-options='--user=0'

Run On A Remote Host

Step 6.3- For this, you have to ensure the following:

  • At least one IP address or resolvable hostname for a remote target.
  • Valid credentials for the remote host.
  • A user with sudo permissions on the remote host.

Step 6.4- Once again, create a directory for inventory files.

mkdir inventory

Step 6.5- Then, create the hosts.yml file.

all:
  hosts:
    192.168.0.2  (Replace with the IP of your host)

Step 6.6- Finally, create a test_remote.yml playbook.

- name: Gather and print facts
  hosts: all
  become: true
  gather_facts: true
  tasks:
   - name: Print facts
     ansible.builtin.debug:
      var: ansible_facts

Step 6.7- Lastly, run the playbook inside the EE.

ansible-navigator run test_remote.yml -i inventory --execution-environment-image postgresql_ee:latest --mode stdout --pull-policy missing --enable-prompts -u student -k -K

Method 4. Compile Ansible From Source

Then, you can always compile the software from source, for this, all you have to do is:

Step 1. Get the file from GitHub:

python3 -m pip install --user https://github.com/ansible/ansible/archive/devel.tar.gz

Step 2. Then, clone the ansible-core repository:

git clone https://github.com/ansible/ansible.git
cd ./ansible

Step 3. Now, setup the Ansible environment

With Bash

source ./hacking/env-setup

With Fish

source ./hacking/env-setup.fish
source ./hacking/env-setup -q

Step 4. Next, you have to install Python dependencies so that Ansible can run:

python3 -m pip install --user -r ./requirements.txt

Step 5. After that, you will also need to update the ansible-core:

git pull --rebase

Step 6. Finally, test that Ansible is installed correctly:

ansible --version

Method 5. Install Ansible On Linux Using Native Package Managers

You can use the native package managers on various distributions to install Ansible:

Fedora

sudo dnf install ansible

Step 1. First, enable the EPEL repository:

CentOS Stream 9

dnf config-manager --set-enabled crb && dnf install https://dl.fedoraproject.org/pub/epel/epel{,-next}-release-latest-9.noarch.rpm

RHEL 9

subscription-manager repos --enable codeready-builder-for-rhel-9-$(arch)-rpms && dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm

Other RHEL 9 compatible distributions

dnf config-manager --set-enabled crb && dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm

Step 2. Finally, install Ansible

sudo dnf install ansible

OpenSUSE Tumbleweed/Leap

sudo zypper install ansible

Ubuntu/Debian

Step 1. First, install wget and gpg:

sudo apt install wget gpg

Step 2. Then, run the following commands to add the Ansible repository and install it:

wget -O- "https://keyserver.ubuntu.com/pks/lookup?fingerprint=on&op=get&search=0x6125E2A8C77F2818FB7BD15B93C4A3FD7BB9C367" | sudo gpg --dearmour -o /usr/share/keyrings/ansible-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/ansible-archive-keyring.gpg] http://ppa.launchpad.net/ansible/ansible/ubuntu $UBUNTU_CODENAME main" | sudo tee /etc/apt/sources.list.d/ansible.list
sudo apt update && sudo apt install ansible

Arch Linux

sudo pacman -S ansible

Method 5. Install Ansible On Ubuntu From a PPA

Lastly, you can always use a PPA to install Ansible, although it will only work on Ubuntu and supported distributions:

sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository --yes --update ppa:ansible/ansible
sudo apt install ansible
Gilbert John Avatar