top of page

Ansible Roles



Ansible Roles concepts provides skeleton for an independent collection of different variables..

Roles are grouping of tasks which can be easily moved from one playbook to another.

Lets go through with below example to understand this concept in more easiest way....


If suppose you have 10 systems and you want to perform some action using 5 different task.

So practically you would not write 5 different task in single playbook as it may create a confusion. So to avoid such kind of scenarios Ansible Roles comes into a picture where you can write 5 different Roles and call in your Playbook as when it requires.


Ansible Roles Directory Structure (POSTFIX)


Ansbile Roles expect some standard to be followed while placing files into Roles. Ansible has its own utility called "ansible-galaxy" which can help us to setup Roles directive structure.


How to Create Roles using ansible-galaxy ?


[root@tecgeek ~]# cd /etc/ansible/roles


[root@tecgeek roles]# ansible-galaxy init postfix


[root@tecgeek roles]# tree

`-- postfix

|-- defaults

| `-- main.yml

|-- Files

|-- handlers

| `-- main.yml

|-- meta

| `-- main.yml

|-- README.md

|-- tasks

| `-- main.yml

|-- tests

| |-- inventory

| `-- test.yml

`-- vars

`-- main.yml


7 directories, 8 files

[root@tecgeek roles]#


Here :

ansible-galaxy init postfix : ansible-galaxy executed with init (initialization) action of postfix role


Now Lets understand use cases of each folder and file.


defaults/main.yml :


This directory contains defaults for variables used in roles.


files/ :


This directory holds files that can be used by this role to copy on remote system.


handlers/ :


This directory contains handlers which may be used by role.


meta/ :


Meta folder has only two variables :

galaxy_info: meta information for galaxy about your role.

dependencies: what roles this role depends on.


tasks/ :


Contains the main list of tasks that are to be executed by the role.


templates/ :


This is the folder where you can store all your template files of roles. This can be emtpy.


vars/ :


This directory contents of other variables that are going to be used by the role.


In next topic we'll try understand Postfix role in more detail.


bottom of page