Ansible for devops is an open source tool for IT configuration management, deployment and orchestration similar to Chef, Puppet, is extremely simple and easy to use because it uses SSH to connect to servers and run the configured Tasks instead of using agent.
In this tutorial, we are going to show you how to install EPEL Repository with Ansible on the remote hosts using playbook.
If you don’t install yet Ansible you can take a look to my first tutorial Getting Started With Ansible
1. Step Create the basic structure of the Role
First step to do is to create the basic structure of the Role, you should create all the following directories layout.
[ansadm@ansible ~]$ tree . ├── inventory.ini ├── role-repo.yml ├── roles │ ├── epel-repository │ │ ├── defaults │ │ │ └── main.yml │ │ ├── meta │ │ │ └── main.yml │ │ └── tasks │ │ └── main.yml 4 directories, 5 files
2. Step Create the Inventory file
In this file specify the list of the hosts to be managed by Ansible
[ansadm@ansible ~]# vi inventory.ini
[webservers] web01 web02 web03 web04 web05 [dbservers] db01 db02 db03
3. Step Create /meta/main.yml
The meta/main.yml is the file where you can provide a description, a list of supported platforms and any dependencies. In our case we don’t have any roles dependencies, however the file should look like this:
[ansadm@ansible ~]$ vi roles/epel-repository/meta/main.yml
--- dependencies: []
4. Step Create /defaults/main.yml
In this file, we’ll add some defaults variables to be used in this role, in our case we have created 3 default variables : EPEL Repository URL, GPG KEY URL and the repository file path.
[ansadm@ansible ~]$ vi roles/epel-repository/defaults/main.yml
--- epel_repo_url: "https://dl.fedoraproject.org/pub/epel/epel-release-latest-{{ ansible_distribution_major_version }}.noarch.rpm" epel_repo_gpg_key_url: "https://dl.fedoraproject.org/pub/epel/RPM-GPG-KEY-EPEL-{{ ansible_distribution_major_version }}" epel_repofile_path: "/etc/yum.repos.d/epel.repo"
5. Step Create /tasks/main.yml
In this file we going to add the tasks todo:
-First, we have to verify if EPEL Repository is installed or not and register the result.
-Second, if EPEL Repository not Installed we will going to install the EPEL Repository using the EPEL Repository URL.
-Last, we have to import the EPEL GPG Key.
[ansadm@ansible ~]$ vi roles/epel-repository/tasks/main.yml
--- - name: 1.Check if EPEL repo is already configured. stat: path={{ epel_repofile_path }} register: epel_repofile_result - name: 2.Install EPEL repo. yum: name: "{{ epel_repo_url }}" state: present register: result when: not epel_repofile_result.stat.exists - name: 3.Import EPEL GPG key. rpm_key: key: "{{ epel_repo_gpg_key_url }}" state: present when: not epel_repofile_result.stat.exists
6. Step Create role-repo.yml Playbook
In the same directory as the roles directory, we have to create a new yaml file where we going to define the hosts, remote user to connect and finally the directory name of the rool to be executed.
[ansadm@ansible ~]$ vi role-repo.yml
--- - hosts: all sudo: yes gather_facts: yes #IMPORTANT remote_user: root roles: - epel-repository
7. Step Run the Role
Let’s do a syntax check, which we should run before running the role:
[ansadm@ansible ~]$ ansible-playbook -i inventory.ini role-repo.yml --syntax-check
Finally you can run the role using this following command
[ansadm@ansible ~]$ ansible-playbook -i inventory.ini role-repo.yml --ask-pass
Note here, that you will be prompted to insert the ssh password of the root user
That’s it, I hope that you enjoy this tutorial and if you went to get the source code of this role visit this link Ansible Projects
1 comment
Change under roles/epel-repository/tasks/main.yml Point 3 with 2 then it works under RedHat8 and 9
—
– name: 1.Check if EPEL repo is already configured.
stat: path={{ epel_repofile_path }}
register: epel_repofile_result
– name: 2.Import EPEL GPG key.
rpm_key:
key: “{{ epel_repo_gpg_key_url }}”
state: present
when: not epel_repofile_result.stat.exists
– name: 3.Install EPEL repo.
yum:
name: “{{ epel_repo_url }}”
state: present
register: result
when: not epel_repofile_result.stat.exists