A Comprehensive Guide to Install Ansible on AlmaLinux 9 / RHEL 9

This guide intends to teach you to Install Ansible on AlmaLinux 9 / RHEL 9. As you may know, Ansible is an automation tool that is available for free and open-source. It allows Administrators to configure and control many nodes from a central server without needing to install agents. Ansible will use the SSH protocol to communicate with the remote nodes. Also, it has a simple installation and configuration. You can follow the steps below to start your Ansible installation on AlmaLinux 9 and RHEL 9 including Rocky Linux 9.

Complete Guide To Install Ansible on AlmaLinux 9 / RHEL 9

Before you start your Ansible installation, you must have access to your server as a non-root user with sudo privileges. For this purpose, you can check this guide on Initial Server Setup with AlmaLinux 9. Also, you can read Initial Server Setup with Rocky Linux 9.

At this point, we use three AlmaLinux OS to show the installation steps. One for the Control node server and two others used for host servers. We use the orca-admin user with sudo privileges for our non-root user.

You can Install Ansible on AlmaLinux 9 / RHEL 9 by using the DNF and EPEL release, and also you can use the pip to install it. To do this, follow the steps below.

Step 1 – Ansible Installation with DNF on AlmaLinux 9 / RHEL 9

Here you can run the system update and install the Epel repository by using the following commands:

# sudo dnf update -y
# sudo dnf install epel-release -y

Then, use the DNF package manager to Install Ansible on AlmaLinux 9 / RHEL 9:

sudo dnf install ansible -y

When your installation is completed, verify it by checking its version:

ansible --version
Output
ansible [core 2.14.2]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.11/site-packages/ansible
  ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible
  python version = 3.11.2 (main, Sep 12 2023, 00:00:00) [GCC 11.3.1 20221121 (Red Hat 11.3.1-4)] (/usr/bin/python3.11)
  jinja version = 3.1.2
  libyaml = True

Step 2 – Ansible Installation with pip on AlmaLinux 9 / RHEL 9

If you want to get the latest version of Ansible, it is recommended to install it with pip Python. To do this, you must install Python and pip by using the following commands:

# sudo dnf update -y
# sudo dnf install python3-pip -y
# sudo pip3 install --upgrade pip

Then, very your Python and pip installation by checking their versions:

python3 -V
Output
Python 3.9.16
pip3 --version
Output
pip 23.3.1 from /usr/local/lib/python3.9/site-packages/pip (python 3.9)

At this point, you can use pip to Install Ansible on AlmaLinux 9 / RHEL 9:

# sudo pip3 install setuptools-rust wheel
# sudo python3 -m pip install ansible
Output
...
Successfully installed MarkupSafe-2.1.3 ansible-8.5.0 ansible-core-2.15.5 cffi-1.16.0 cryptography-41.0.5 importlib-resources-5.0.7 jinja2-3.1.2 packaging-23.2 pycparser-2.21 resolvelib-1.0.1

Step 3 – Configure Ansible on AlmaLinux 9 / RHEL 9

If you install Ansible by using the DNF package manager, the default ansible configuration file which is ansible.cfg, is automatically created under the /etc/ansible directory. But if you install Ansible with Pip Python, you should create the configuration file manually.

It is recommended to have a config file for each project. To do this, you can follow the steps below:

First, create a project directory and switch to it with the commands below:

# mkdir ansible-project
# cd ansible-project

Then, create the Ansible configuration file with your desired text editor like Vi editor or Nano editor:

vi ansible.cfg

Add the following content to the file with your values:

[defaults]
inventory      = /home/orca-admin/ansible-project/inventory
remote_user = orca-admin
host_key_checking = False

[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=False

When you are done, save and close the file.

Next, you should create the inventory file under your Ansible project directory:

vi inventory

Add your host servers with their IP address as shown below:

[Hostserver1]
198.30.2.186

[Hostserver2]
198.30.2.187

When you are done, save and close the file.

Step 4 – Generate SSH Keys for Ansible Remote User

At this point, you must create the SSH keys for your remote user and share them with your host servers. To do this, run the following command:

ssh-keygen
Output
Generating public/private rsa key pair.
Enter file in which to save the key (/home/orca-admin/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/orca-admin/.ssh/id_rsa
Your public key has been saved in /home/orca-admin/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:CtgS+zM1MHkHXrEWnzKMw8ikGz4VeiO+4Swj6pfKTHQ orca-admin@alma.li
The key's randomart image is:
+---[RSA 3072]----+
|    o . +.       |
|   = * = + .     |
|  * X * B o      |
| o @ = + o       |
| .XEo o S        |
|.o.* o o         |
|+.+ = .          |
|*o o o           |
|o=o              |
+----[SHA256]-----+

Now you can share your SSH keys with your host server by using the commands below:

# ssh-copy-id orca-admin@198.30.2.186
# ssh-copy-id orca-admin@198.30.2.187

Next, you can run the following command on your Host Servers to run all the commands without prompting a password.

echo "orca-admin ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/orca-admin

Step 5 – Verify Ansible Remote Connection on AlmaLinux 9 / RHEL 9

At this point, you can use the ping module to verify the connectivity from the control node to your host servers:

ansible -i inventory all -m ping
Output
198.30.2.186 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
198.30.2.187 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}

Step 6 – Test Ansible by Creating a Sample PlayBook on AlmaLinux 9 / RHEL 9

At this point, we want to show you how to create a sample playbook for Ansible and test the working on host servers. For example, create a web.yaml file under the folder ansible project directory:

# cd ansible-project
# vi web.yaml

Add the following content to the file which installs Nginx and PHP:

- name: Play to Packages
  hosts:
    - Hostserver1
    - Hostserver2
  tasks:
  - name: Install php and nginx
    package:
      name:
        - php
        - nginx
      state: present

When you are done, save and close the file.

Then, run the Ansible playbook with the command below:

ansible-playbook -i inventory web.yaml
Output
PLAY [Play to Packages] *************************************************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************************************************
ok: [198.30.2.186]
ok: [198.30.2.187]

TASK [Install php and nginx] ********************************************************************************************************************************************
changed: [198.30.2.186]
changed: [198.30.2.187]

PLAY RECAP **************************************************************************************************************************************************************
198.30.2.186            : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
198.30.2.187            : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

This means your Ansible is working correctly on your RHEL 9 server. To get more information, you can visit the Documentation page.

Conclusion

At this point, you have learned to Install Ansible on AlmaLinux 9 / RHEL 9. As you saw, you can easily install Ansible by using DNF and PIP and configure it. Then, you should generate and copy the SSH keys to your host servers to start the communications. Next, you can easily test your Ansible by creating a Sample playbook.

Hope you enjoy using Ansible. You may also interested in these articles:

Python 3.12 Installation on AlmaLinux 9 / RHEL 9

5 Best Free Alternatives To Python

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!