How to install Ansible on RHEL9 Step by Step

Mayank Jain
5 min readJul 30, 2023

--

Hello Folks, In this blog we will learn how to install Ansible on RHEL9 and run our first playbook using Ansible on the local machine. Later we will create and configure a managed node and run an Ansible playbook from the control node to the managed node.

What is Ansible?

Ansible is used for automating IT operations such as deploying applications, managing configurations, scaling the infrastructure, installing software, and other activities involving many repetitive tasks.

Ansible is a free and open-source automation and configuration tool. On RHEL 9, the ansible core package is available in the default repository (AppStream). This means we don’t need to enable additional repositories like Ansible Engine or EPEL on the RHEL9.

Prerequisites

RHEL9 OS, you can install on Virtual Box or if you are using the cloud then you can spin up the RHEL9 machine on it.

Sudo user with Admin right on OS.

Locally configure the yum repository or subscription of RHEL9

Step-1 Install Ansible

Check the os-release of your system by running below command

# cat /etc/os-release
OS Release Version

Open your terminal and run the below command to install Ansible.

# dnf install ansible-core

press y to continue the installation.

Once Ansible is installed check the version of Ansible by running the below command

# ansible --version
Ansible version

Step-2 Test Ansible

To test Ansible installation, we will be using one of our local RHEL9 Linux systems which is also our Ansible control node (RHEL 9). Later we will test with a remote machine/ manage node too.

First, we will generate the ssh-key for our user which is used to run ansible. Run the below command to generate ssh key.

# ssh-keygen
Key Generation

Once the key is generated let’s create an ansible configuration file. Create a directory and inside the directory create ansible.cfg file.

Now let’s create an inventory file in which we will store our host details like IP address. After creating the inventory file let’s try to use the ping command to test the connection from the control node to the inventory host/IP.

As you can see we are getting the above error unreachable. This is because we have not copied our control node key to the remote server/inventory server. Let’s copy the key and then try again to ping the server. Run the below command to copy the ssh key.

# ssh-copy-id mayank@127.0.0.1
Copy ssh key

After copying the key let’s run the ping command again to test the connection.

This time we get a different error which is Missing sudo password. This is because Ansible is ping through the sudo user. Let’s fix this error by giving sudo permission to mayank user. Run the below command to add mayank as a sudo user.

# echo "mayank ALL=(ALL) NOPASSWD:ALL" | sudo tee /etc/sudoers.d/mayank

Note: Run the above command on your manage node.

Now we will test again to ping the servers present in the inventory by using all in the ansible command which will test the ping for all the servers present in the inventory file. From the below screenshot, you can see our ping test is successful.

Step-3 Write and sample Playbook and test Ansible

In the sample playbook, we will install httpd to our manage node which is mentioned in our inventory file.

Create an install_httpd.yaml file and add the below contains.

---
- name: Playbook to Install and Start httpd
hosts: local
tasks:
- name: Install httpd
package:
name: httpd
state: present

- name: Start httpd Service
service:
name: httpd
state: started

After creating the file run the ansible playbook by running the below command.

# ansible-playbook install_httpd

It will install the httpd on the managed node. You will see a similar output as above. Later we will check the httpd service and package on the system by running the shell command to confirm.

Step-4 Adhoc command test

In this step, we will run the ansible ad hoc command. Ansible ad hoc commands are CLI commands used for simple and one-time tasks. One-time tasks include checking whether the nodes are reachable over SSH, shutting down all nodes, and so on.

Run the below command to check the status of the httpd service.

# ansible local -i inventory -m shell -a 'systemctl status httpd'

We will find the similar output mentioned below in screenshot for the above command.

Ad hoc command

Refer here to learn How to set up Puppet Master and Agent in a Virtual Box environment.

Refer here to learn How to write modules in Puppet.

Refer here to learn How to remove the old Linux kernel from RHEL8/CentOS8?

--

--

Mayank Jain
Mayank Jain

Written by Mayank Jain

Skilled DevOps Engineer with 7.5+ years of hands-on experience supporting, automating, & optimizing mission critical deployments in cloud.