Part 9 – Ansible Roles

As of now we have written playbooks that include all the details like hosts, variables, tasks and plays. This is not an ideal way if we have many tasks and lots and lots of plays to be included, the best way to do it is to separate each item into its own directory and this is how its done in prodution especially if a organization use cloud. So let’s do some Role things.

Before knowing anything further about Roles, we need to understand about the ‘include’ which is core for the Roles to work. include can be used in a playbook to use another playbook instead of rewriting the entire playbook again. The placement of include determines the use of it. The include statement mentioned in a task referes to the tasks and include mentioned at the hosts refers to another playbook.2017-04-05 12_25_02-Playbook Roles and Include Statements — Ansible Documentation

©Ansible Docs

Roles – Directory Structure

Roles directory includes directoryies based on the functionality rather than by the name of the server for a better reusability and each directory has a directory within for each item and all the details are mentioned in the main.yml file. Below is the sample directory structure i have.

2017-04-05 12_42_45-vagrant@ansi_ ~_my_ansible


Let’s now see what’s included in each main.yml file and also finally to make use of all this roles, I created two yml files my_webrole.yml and my_dbrole.yml. To make use of the include and demonstrate it i created another playbook called final.yml. Each subdirectory under the dbrole/webrole is optional and you use them based on your environment. All we do here is convert our existing playbook to respective main.yml files under roles.


2017-04-05 19_50_04-vagrant@ansi_ ~_my_ansible2017-04-05 19_59_09-Untitled - Paint2017-04-05 19_52_38-vagrant@ansi_ ~_my_ansible


2017-04-05 19_50_17-vagrant@ansi_ ~_my_ansible2017-04-05 19_59_09-Untitled - Paint2017-04-05 19_53_08-vagrant@ansi_ ~_my_ansible


2017-04-05 19_50_40-vagrant@ansi_ ~_my_ansible2017-04-05 19_59_09-Untitled - Paint2017-04-05 19_53_29-vagrant@ansi_ ~_my_ansible

The main.yml files under the dbrole are also similar. Now let’s cat the my_webrole.yml and my_dbrole.yml files which basically makes use of the roles we created.

2017-04-05 20_06_21-vagrant@ansi_ ~_my_ansible

These yml playbooks can be executed separately and they just work fine. But to demonstrate the usage of include command, we used the final.yml and here is the cat output of final.yml.

2017-04-05 20_08_56-vagrant@ansi_ ~_my_ansible

Finally running the final.yml playbook just works fine as earlier. Note that ansible mentions the role it is executing and also the task in the role in the output, this is great for troubleshooting.

2017-04-05 20_11_10-vagrant@ansi_ ~_my_ansible

Hope this was informative. Thanks!

 Part 1 – Introduction to Ansible

Part 2 – Ansible Lab Setup

Part 3 – Understanding our First Ansible Command

Part 4 – More about Inventory File

Part 5 – Ansible Config

Part 6 – Ansible Modules

Part 7 – Ansible Tasks, Plays, PlayBooks

Part 8 – Ansible Play Recap, vars, Notify, Handlers

Part 9 – Ansible Roles


#include, #main-yml, #roles, #roles-structure