Simplifying Ansible Playbooks with Roles

November 14, 2022 | Comments(0) |

TABLE OF CONTENT

1. Overview
2. Introduction
3. Creating a Role
4. Deploy Web Server on AWS through Ansible Roles
5. Conclusion
6. About CloudThat
7. FAQs

 

Overview

After completion of this blog, you should be able to work with a role in a playbook’s project directory and run it as part of one of the plays in the playbook.

As you work more on the playbook, you will probably discover that you have many opportunities to reuse code from the playbook that is already written. Perhaps a play to configure a web server with different versions or configure MySQL database for different usernames, passwords, and hostnames.

Introduction

Ansible Roles make it easier to reuse Ansible code. You can package in a directory structure containing all the tasks, variables, files, templates, and other resources needed to configure software, applications, etc. Copy that role from project to project simply by copying the directory. You can then simply call that role from a play to execute it.

Roles are a supplemental playbook feature that can be utilized on its own. Under no circumstances can the role be performed directly. Top-level playbooks serve as a link between the hosts in the user’s inventory file and the responsibilities that should be applicable to them.

In comparison to running numerous ad-hoc commands, Ansible Playbooks offer more sophisticated capabilities for sending tasks to managed hosts and are made up of one or more plays.
Yet Another Markup Language (YAML), which is simpler to understand than a JSON or XML file, is used to write the tasks in Ansible playbooks. Each playbook task is carried out sequentially for each host in the inventory file.

Ansible roles benefits:

  1. Allowing easy sharing of code with others
  2. Make larger projects more manageable
  3. Can use community-supported roles from Ansible Galaxy
  4. Can use rhel-system-roles as part of Red Hat Enterprise Linux

Understanding The Ansible Role Structure

Ansible role is defined by a common structure of subdirectories and files. The top-level directory defines the name of the role itself. Files are organized into subdirectories that are named according to the purpose of the role, such as tasks and vars. The templates and files subdirectories contain files referenced by tasks in other YAML files.

Ansible1

Ansible role subdirectories and their Functions

Ansible2

Creating a Role

ansible-galaxy command is used to create and manage Ansible roles. You can run ansible-galaxy init to create the directory structure for a new role. Specify the name of the role as an argument to the command. To use the roles, we need to add these roles to the playbook.

ansible-galaxy init  <ansible_role_name>

Ansible3

Role directory created now we can use this according to our requirements.

Ansible4

To use any role, we create one playbook and from there we run the roles.

Ansible5

Deploy Web Server on AWS through Ansible Roles

I’ll be configuring Apache Webserver on this system itself. We’ll be following three steps

  1. Download the software
  2. Add the code on the document root
  3. Start the service
  • Use ansible-galaxy init to start your role and then remove any directories and files that you do not need.

Ansible6

  • tasks/main.yml it’s the same as writing tasks in the playbook.

Ansible7

  • Defining variable name and service name in vars/main.yml

Ansible8

  • This code will be sent to the document’s root location

Ansible9

  • Here we are achieving idempotency which means only restarting the service when there is a change in file name index.html instead of restarting it whenever the playbook runs.

Ansible10

  • Using the Role in a Playbook

Ansible11

  • Adding role to the playbook

Ansible12

  • Let’s run the playbook containing the role in it.

$ ansible-playbook <name_of_playbook>

Ansible13

  • Let’s try to hit the webserver from the terminal and then the browser.

Ansible14

Ansible15

Conclusion

We learned about roles, how to work with roles, and utilizing the concept of role to configure Apache webserver. Roles structure Ansible code in a way that allows reusability of the code. Ansible Galaxy is a public library where a lot of roles are written by Ansible users. It’s good practice to go through the library to check for the roles instead of creating them from scratch.

About CloudThat

CloudThat is also the official AWS (Amazon Web Services) Advanced Consulting Partner and Training partner and Microsoft gold partner, helping people develop knowledge of the cloud and help their businesses aim for higher goals using best in industry cloud computing practices and expertise. We are on a mission to build a robust cloud computing ecosystem by disseminating knowledge on technological intric1acies within the cloud space. Our blogs, webinars, case studies, and white papers enable all the stakeholders in the cloud computing sphere.

Drop a query if you have any questions regarding Ansible and I will get back to you quickly.

To get started, go through our Consultancy page and Managed Services Package that is CloudThat’s offerings.

FAQs

Q1. What is the difference between an Ansible role and a playbook?

A. The playbook is a script containing all the tasks. Whereas the role is the same as a playbook with tasks or requirements divided into multiple files and folders.

Q2. Why are there roles in Ansible?

A. Roles improve the readability and management of code. And We can use community-created roles as well from Ansible galaxy.

Q3. Where are Ansible roles stored?

A. /etc/ansible/roles are the default location of roles.


Leave a Reply