Voiced by Amazon Polly |
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.
Customized Cloud Solutions to Drive your Business Success
- Cloud Migration
- Devops
- AIML & IoT
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:
- Allowing easy sharing of code with others
- Make larger projects more manageable
- Can use community-supported roles from Ansible Galaxy
- 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.
Ansible role subdirectories and their Functions
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>
Role directory created now we can use this according to our requirements.
To use any role, we create one playbook and from there we run the roles.
Deploy Web Server on AWS through Ansible Roles
I’ll be configuring Apache Webserver on this system itself. We’ll be following three steps
- Download the software
- Add the code on the document root
- Start the service
- Use ansible-galaxy init to start your role and then remove any directories and files that you do not need.
- tasks/main.yml it’s the same as writing tasks in the playbook.
- Defining variable name and service name in vars/main.yml
- This code will be sent to the document’s root location
- 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.
- Using the Role in a Playbook
- Adding role to the playbook
- Let’s run the playbook containing the role in it.
$ ansible-playbook <name_of_playbook>
- Let’s try to hit the webserver from the terminal and then the browser.
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.
Get your new hires billable within 1-60 days. Experience our Capability Development Framework today.
- Cloud Training
- Customized Training
- Experiential Learning
About CloudThat
CloudThat is a leading provider of Cloud Training and Consulting services with a global presence in India, the USA, Asia, Europe, and Africa. Specializing in AWS, Microsoft Azure, GCP, VMware, Databricks, and more, the company serves mid-market and enterprise clients, offering comprehensive expertise in Cloud Migration, Data Platforms, DevOps, IoT, AI/ML, and more.
CloudThat is the first Indian Company to win the prestigious Microsoft Partner 2024 Award and is recognized as a top-tier partner with AWS and Microsoft, including the prestigious ‘Think Big’ partner award from AWS and the Microsoft Superstars FY 2023 award in Asia & India. Having trained 850k+ professionals in 600+ cloud certifications and completed 500+ consulting projects globally, CloudThat is an official AWS Advanced Consulting Partner, Microsoft Gold Partner, AWS Training Partner, AWS Migration Partner, AWS Data and Analytics Partner, AWS DevOps Competency Partner, AWS GenAI Competency Partner, Amazon QuickSight Service Delivery Partner, Amazon EKS Service Delivery Partner, AWS Microsoft Workload Partners, Amazon EC2 Service Delivery Partner, Amazon ECS Service Delivery Partner, AWS Glue Service Delivery Partner, Amazon Redshift Service Delivery Partner, AWS Control Tower Service Delivery Partner, AWS WAF Service Delivery Partner, Amazon CloudFront Service Delivery Partner, Amazon OpenSearch Service Delivery Partner, AWS DMS Service Delivery Partner, AWS Systems Manager Service Delivery Partner, Amazon RDS Service Delivery Partner, AWS CloudFormation Service Delivery Partner, AWS Config, Amazon EMR and many more.
FAQs
1. What is the difference between an Ansible role and a playbook?
ANS: – 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.
2. Why are there roles in Ansible?
ANS: – Roles improve the readability and management of code. And We can use community-created roles as well from Ansible galaxy.
3. Where are Ansible roles stored?
ANS: – /etc/ansible/roles are the default location of roles.

WRITTEN BY Vineet Negi
Vineet Negi is a Research Associate at CloudThat. He is part of the Kubernetes vertical and has worked on DevOps and many other Cloud Computing technologies. He is an enthusiastic individual who is passionate about exploring all the latest technologies from a learning perspective.
Comments