Difficulty: Beginner-Intermediate
Estimated Time: 25 minutes

Welcome to the interactive course to learn to create and use the different functionalities of Ansible roles!

If you are here, it is very likely that you already know a little about Ansible and how to create a Playbook or a very simple Role ... If not, I recommend you to see our other interactive course that serves as an introduction to the fabulous world of Ansible! You have it [here] (https://katacoda.com/devopstf/scenarios/ansible-101).

What will I learn in this course?

Well, you will learn how the different directories of an Ansible Role work.

Being organized when creating a role is very important , since it will allow us in the future to export it to another project without losing its functionality and without having to go around the different files to edit one by one the fields that we need to adapt for the new environment.

It is not necessary to use all the directories for this , so this course will focus on teaching which are the most used, so that in a short time you know the priorities and how to use them correctly.

In this course we will learn to use the following directories :

  • Directory ' defaults '
  • Directory 'tasks *'
  • Directory ' templates '
  • Directory ' vars '

If you consider that you are ready to give everything of you ... Let's start the stage!

DevOps TF - Óscar Díaz Domínguez

Ansible 101 - Roles

Step 1 of 6

The directory 'defaults'

We know what a role is and how it works, what utilities it has for us and how to use them to our advantage ... but do we know how to use its most powerful functions? The roles, in addition to tasks and files, are capable of hosting own variables, as well as generating files based on variables that could change from one execution to another.

Our test role will be responsible for creating an identification card for a mascot in .txt format, which will change depending on how we execute our playbook (or what we execute). To simplify the example, we are going to make a separation between pets that are mammals (dogs and cats) and pets that are oviparous (nymphs, canaries, etc ...).

We will have to show their name, their gender, their type of reproduction and the type of food they need (I think in the case of mammals, seeds in the case of oviparous), as well as the year of their birth.

In the end we will generate the cards under a directory with the name of the pet, which will be under a directory that will define if it is a mammal or oviparous and we can create all those that we want.

With the clear example ... we will be seeing one by one how they work and why we can take advantage of the directories of the Ansible roles, in order of appearance.

What is its usefulness? This directory is responsible for hosting variables by default, just in case some of them do not define it, have a value that allows the Role to run without major problem.

Generally in it we are going to define a variable whose value is not extremely important, or that we know will change quite frequently.

In our case, these variables will be the name, the gender and the year of birth, which are data quite likely to change.

Creating our main.yml With this clear, we are going to create our variables by default.

First of all, let's locate ourselves in the Ansible directory: cd ansible {{execute}}

And now let's create the file main.yml inside 'defaults': touch roles / katacoder / defaults / main.yml {{execute}}

Inside, we will place the following lines of code:

Name: Toby gender: M Birth: 11/20/2017 type: undefined Food: indefinite With this ready, we do not need anything else from this folder.

Let's go to the next directory ...