Ansible Roles and Variables

Understanding Roles,

Roles Reusability, Variables, Templates

Learning Outcome

5

Recognize how these components simplify automation management

 

4

Identify how Templates help generate dynamic configuration files

3

Understand the purpose of Variables in Ansible automation

2

Explain how Roles improve organization and reusability

1

Understand what Roles are in Ansible

Previously, we learned about

 Playbooks, Inventory, and Modules in Ansible

These components help automate tasks across multiple systems

Understanding the concept

Imagine a project team where each member has a specific responsibility 

such as development, testing, or deployment

Similarly, in Ansible, Roles divide

Automation tasks into organized and reusable components

To manage complex automation tasks efficiently,

Ansible uses Roles, Variables, and Templates to structure and reuse configurations

What are Roles?

Roles are a way to organize Ansible playbooks into structured directories

They help break large playbooks into smaller,

manageable components

Roles make automation easier to maintain and reuse

Role Directory Structure

A typical role structure looks like this

Explanation

tasks/ → Contains main automation tasks
 

handlers/ → Defines triggered actions (like restarting services)
 

files/ → Static files copied to servers
 

templates/ → Dynamic configuration templates
 

vars/ → Variables used by the role
 

defaults/ → Default variable values

Role Reusability

One of the biggest advantages of roles is reusability

Instead of rewriting automation tasks

multiple times, a role can be reused in different playbooks

Example

A web server role can include:

 

Installing Nginx
 

Configuring the service
 

Starting the server

This role can be used across multiple environments such as:

This ensures consistent configuration across systems

What are Variables?

Variables allow users to store values that can change depending on the environment

They make playbooks more flexible and reusable

Example Variable

web_port: 80

This variable can be used inside tasks or templates

Variables can be defined in

 

Playbooks
 

Inventory files
 

Role directories
 

Variable files

- name: Install nginx

 apt:

   name: nginx

   state: present

What are Templates?

Templates are dynamic configuration files generated using variables

Ansible uses the Jinja2 templating engine to create these files

Templates allow configuration files to adapt based on variable value

Example Template

Example configuration file template

Here, the {{ web_port }} value will be replaced with the variable value when the template is deployed

server {

   listen {{ web_port }};

}

Why Templates are Important

Templates help in

Generating dynamic configuration files
 

Supporting multiple environments
 

Reducing manual configuration changes

They are commonly used for

Environment-specific configurations

Application settings

Web server configuration files

Important components used in roles

Roles organize automation into structured directories
 

Variables allow dynamic configuration values
 

Templates generate configuration files using variables
 

Role reusability allows the same automation logic to be used across environments

Summary

5

Templates generate dynamic configuration files using variables

 

4

Variables store values that make configurations flexible

3

Role reusability allows the same automation to be used across multiple environments

2

Roles help improve code organization and maintainability

1

Roles organize Ansible automation into structured components

Quiz

What is the main purpose of Ansible Roles?

 

A. Manage network traffic

B. Organize and reuse automation tasks

C. Store system logs

D. Install operating systems

 

Answer

What is the main purpose of Ansible Roles?

 

A. Manage network traffic

B. Organize and reuse automation tasks

C. Store system logs

D. Install operating systems

 

DevOps-Understanding Roles, Roles Reusability, Variables, Templates

By Content ITV

DevOps-Understanding Roles, Roles Reusability, Variables, Templates

  • 79