Ansible

How to Enable EPEL Repository on CentOS with Ansible

In this article, I will show you how to enable EPEL repository on CentOS using Ansible. So, let’s get started.

Prerequisites

To enable EPEL repository on CentOS 7 or CentOS 8 machines with Ansible:

  1. You must have Ansible installed on your computer.
  2. You must have CentOS 7 or CentOS 8 machines configured for Ansible automation.

There are many articles on LinuxHint dedicated to Installing Ansible and configuring hosts for Ansible automation. You may want to check these articles out if necessary, for your purposes.

Enabling EPEL Repository on CentOS Hosts

First, create a project directory ~/project with the following command:

$ mkdir -pv ~/project/playbooks

Navigate to the ~/project directory with the following command:

$ cd ~/project

Create a new file hosts in the project directory and open it with the nano text editor as follows:

$ nano hosts

 

A blank file hosts should be created and opened with the nano text editor.

Type in the IP addresses or DNS names of the target CentOS 7 and CentOS 8 machines (where you want to enable EPEL repository) in the centos section of the hosts file, as follows:

[centos]
192.168.20.169
192.168.20.222

Here, 192.168.20.169 is the IP address of my CentOS 8 virtual machine and 192.168.20.222 is the IP address of my CentOS 7 virtual machine. These will be different for you. Make sure to replace the sample IP addresses with your own list from now on.

NOTE: You can find the IP addresses of your CentOS hosts with the following command:

$ hostname -I

If your CentOS hosts have DNS names configured, you should be able to find them with the following command:

$ hostname -A

The final hosts file should look as shown in the screenshot below.

Now, save the hosts file by pressing <Ctrl> + X followed by Y and <Enter>.

Create an Ansible configuration file ansible.cfg as follows:

$ nano ansible.cfg

Type the following lines in the ansible.cfg file:

[defaults]
inventory=./hosts

Once you have completed this step, save the file by pressing <Ctrl> + X followed by Y and <Enter>.

Now, create a new Ansible playbook enable_epel_repo.yaml in the playbooks/ directory as follows:

$ nano playbooks/enable_epel_repo.yaml

Next, type the following codes in the enable_epel_repo.yaml file:

- hosts: centos
user: ansible
tasks:
- name: Enable EPEL Repository on CentOS 8
dnf:
name: epel-release
state: latest
become: True
when: ansible_facts['os_family'] == 'RedHat' and ansible_facts
['distribution_major_version'] == '8'
- name: Enable EPEL Repository on CentOS 7
yum:
name: epel-release
state: latest
become: True
when: ansible_facts['os_family'] == 'RedHat' and ansible_facts
['distribution_major_version'] == '7'

In this code:

hosts: centos, selects only the hosts in the centos group from the hosts file.

user: ansible, the SSH username of the hosts (where Ansible will run the tasks) will be ansible.

I have defined 2 tasks here. One for CentOS 8 hosts and one for CentOS 7 hosts. The reason I have done it this way is because the default package manager for CentOS 8 is DNF, and CentOS 7 is YUM. One task (first task) will use the DNF package manager and will run only on CentOS 8 hosts. The other task (last task) will use the YUM package manager and will run only on CentOS 7 hosts.

These two tasks are almost identical. The only differences are the package manager modules (dnf and yum) used in the tasks and the CentOS version checking code.

dnf and yum Ansible modules accept the same parameters.

Here, name: epel-release, the package to be installed is the epel-release.

state: latest, the package epel-release should be installed. If the package is already installed and an updated version is available, then the package will be updated.

when: condition, if the condition is true, then the task will run. Otherwise, the task will not run.

ansible_facts, used to access the Ansible host variables.

ansible_facts[‘os_family’] == ‘RedHat’, checks whether the host OS is CentOS or RedHat.

ansible_facts[‘distribution_major_version’] == ‘8’, checks whether the host OS version is 8 (CentOS 8 or RedHat 8, in this case).

ansible_facts[‘distribution_major_version’] == ‘7’, checks whether the host OS version is 7 (CentOS 7 or RedHat 7, in this case).

Then, save enable_epel_repo.yaml file by pressing <Ctrl> + X followed by Y and <Enter>.

Now, you can run the Ansible playbook as follows:

$ ansible-playbook playbooks/enable_epel_repo.yaml

The playbook should run without any errors and the EPEL repository should be enabled on both the CentOS 7 and CentOS 8 hosts.

As you can see, EPEL repository is enabled in my CentOS 8 host.

As you can see, EPEL repository is enabled in my CentOS 7 host.

So, that is how you enable EPEL repository on CentOS using Ansible. Thanks for reading this article.

About the author

Shahriar Shovon

Freelancer & Linux System Administrator. Also loves Web API development with Node.js and JavaScript. I was born in Bangladesh. I am currently studying Electronics and Communication Engineering at Khulna University of Engineering & Technology (KUET), one of the demanding public engineering universities of Bangladesh.