Table of contents
1.
Introduction
2.
Role Directory Structure
3.
defaults: 
4.
vars: 
5.
tasks: 
6.
files:
7.
templates: 
8.
meta:
9.
handlers: 
10.
How should variables be defined in a role?
11.
Different locations for Ansible roles
12.
Creating a New Role
13.
Creating a Simple Ansible Role
14.
Setting Up Our Ansible Role
15.
Creating Our Tasks
16.
Creating Our Playbook
17.
Running Our Playbook
18.
Frequently Asked Questions
18.1.
What are Ansible Server requirements?
18.2.
What is Ansible Galaxy?
18.3.
What’s an ad hoc command?
19.
Conclusion
Last Updated: Aug 13, 2025

Ansible - Roles

Author Nilesh Kumar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

 

Introduction

A helpful configuration management tool is Ansible. It makes it easier to configure numerous servers using a single controller instance. It facilitates the automation of complex processes like configuration management, application deployment, CI/CD pipeline creation, etc. Code redundancy is increased when ansible code is written to manage a single service across many environments or products. It becomes more challenging to handle everything in a single Ansible playbook file as functionality becomes more complicated. Coding amongst teams becomes challenging. Ansible's Role helps with these issues. Ansible roles are standalone components that enable the reuse of standard configuration processes. The playbook must contain the use of ansible roles.

 

ansible roles

 

Role Directory Structure

Directories such as defaults, vars, tasks, files, templates, meta, and handlers can be found in a role directory hierarchy. A main.yml file with pertinent information must be present in every directory. Let's examine each directory more closely.

defaults: 

Contains the role's default variables. Since default variables have the lowest priority, overriding them is simple.

vars: 

Contains the role's variables. Variables in the vars directory are given precedence over those in the defaults directory.

tasks: 

These consist of the primary set of actions that the position is expected to take.

files:

It consists of files we wish to copy to the remote host. The path to the resources kept in this directory does not need to be specified.

templates: 

Includes file templates that allow changes from the role. For building templates, we employ the Jinja2 templating language.

meta:

Provides information about dependencies, support platforms, and roles like an author.

handlers: 

It contains handlers connected to services that can be called by "notify" directives.

How should variables be defined in a role?

  • The layout of the ansible roles directory allows for the definition of variables at various levels.
  • The default variables used for default role functioning are contained in the main.yml file in the vars folder. It is not intended to overwrite them.
  • Default variables may be found in defaults/main.yml. Variables can replace these variables with the same name set in the playback and have higher precedence because they have lower priority.
  • Variables set in the playbook will always take precedence over those specified in the role. Secrets and encrypted data from the vault should always be controlled from the playbook because role variables are meant to be general.
  • According to the playbook's definition, role variables have the highest precedence and overwrite inventory variables and playbook variables when a role is called.

Different locations for Ansible roles

Roles may be readily used from playbooks by storing them in the default place.

  • ./roles The highest precedence is ./roles.
  • /.ansible/roles After that, /.ansible/roles are checked
  • /etc/ansible/roles Next, /etc/ansible/roles are examined.

Creating a New Role

Refactoring an Ansible playbook into a role is a typical strategy. To do that, we must take apart a playbook into its component components and then use the directories we just saw in the previous section to piece them back together as an Ansible role.

The installation and configuration of a primary Nginx web server from scratch will be demonstrated in this section using an example of adding a new role. You must have VirtualBox, Ansible, and Vagrant installed locally if you want to follow along.

In common pathways like the directory of the orchestrating playbook, the roles/ directory, or the set roles path configuration variable, Ansible looks for referenced roles. Additionally, a custom path can be provided when referring to a role:

- hosts: all

  roles:

    - role: "/custom_path/to/the/role

Creating a Simple Ansible Role

Our Ansible roles can be as straightforward or as sophisticated as required. Let's begin by establishing a specific position that will use the apt package manager to update all the packages on a remote system. Only a minimal role will be created, and we'll consider how to use it in our playbook.

Setting Up Our Ansible Role

We need to construct our role first. Although we can manually create each directory, it is easier to allow ansible-galaxy to handle this for us:

ansible-galaxy init apt_update

The following will then appear in our console output:
 

- Role apt_update was created successfully

Creating Our Tasks

The job of updating the packages on our remote system will then be created in the role.

# tasks file for apt_update
- apt: 
     name: "*"
     state: latest

 

The data above will be included in our tasks/main.yml file.

We won't add any variables, templates, or files just now because we want to create a simple role. We will next proceed to construct a playbook that includes this job.

Creating Our Playbook

We will need to construct a new playbook that includes this job now that we have established our role to update packages.

- hosts: something
  become: yes
  roles:
      - /full_path_to_role/galaxy/apt_update

Remember that this playbook doesn't have to be in the same directory as our role; it can be found anywhere on our system.

Running Our Playbook

We have now developed our playbook and included our contribution to it. All that is left for us to execute this script, at which point the packages on our remote system will be updated and displayed on our terminal.

ansible-playbook apt_update_playbook.yml

Frequently Asked Questions

What are Ansible Server requirements?

In order to install Linux in a virtual machine if you use Windows, you must be a Windows user. Python 2.6 or later is necessary. 

What is Ansible Galaxy?

Ansible includes this utility to build a base directory structure. On the website Galaxy, individuals can discover and distribute Ansible material. The following command can be used to download roles from the website:

$ ansible-galaxy install username.role_name

What’s an ad hoc command?

Without utilizing a playbook, users start ad hoc commands to take action on a host. Think of it as a single command.

Conclusion

In this article, we have extensively discussed his article explains the details of Ansible - Roles along with the details of Role Directory Structure, variable defining, location for Ansible-Roles, and Creating a New Role

We hope this blog has helped you enhance your knowledge regarding Ansible Roles. If you want to learn more, you can refer to our guided paths on the Coding Ninjas Studio platform to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. To practice and improve yourself in the interview, you can also check out Top 100 SQL problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews. Do upvote our blog to help other ninjas grow. Happy Coding!!

thankyou.png

 

 

Live masterclass