- In this video, I will show you how to create
your own roles.
Back to roles that is for a simple reason that roles,
well, they're part of the collections.
So I want to show you what you can do
to set up your own role.
And in order to do so, let me go to the roles directory
and let's investigate motd as well as motd2.
What is the thing?
If you use Ansible galaxy role in it,
my role, for instance,
then you create the directory circulates required
to create your own role.
So if I use tree, my role, then what do I get?
I get these different directories
that you should always have in role.
Now you can just fill it out
and make sure that the appropriate information
is where it needs to be.
I am going to show you in the role sub directory
the motd role.
And in the motd role, we find a very simple example.
First, I cleaned up the directory searcher,
everything that's not required is deleted.
So we only have defaults meta tasks and templates.
If you go there in order of importancy,
first in meta, what do we have in meta?
Meta is for metadata
and metadata is information that will typically end up
in Ansible Galaxy.
So this is where you are going to make sure that all the
important information is there.
Well, this role is going to be so important.
We need Ansible 2.8 and later.
Of course change anything that needs to be changed.
Next, let's have a look at tasks.
In tasks there's main.yml.
And in this main.yml, this task is very simple.
This task is only copying an motd file.
The sources templates, motd destination is motd.
And you know what?
Let's make that new motd so that we don't override anything
and mess up things.
So this task is working with the template.
The template is what we have right here, the motd.j2.
And in this template, we can see there's a couple of Ansible
effects and there is a contact message,
contact system manager if anything is wrong.
So the system manager is a variable that you need to provide
and it is provided through defaults main.yml.
The thing in the defaults
is that defaults contains some standard variables
and it's okay to override them. You don't need them.
You wanna work with own. That's okay.
We just need a default value for the variables to ensure
that the role is not going to fill
because you are missing default values.
And the rest of it is going to happen from the Playbook.
Now notice that in the directory that you are using,
the roles are supposed to be in the roles directory.
Oh, oh, my role, wrong directory my friends.
Might as well rm minus rf it
because we don't need it anymore anyways.
Let's have a look at motdrole.yml,
where we can see
that this role is executed on Ansible2.
The other information, not really important.
The important part is right here.
So roles, we call role motd and how we override system,
system manager.
Now what's going to be the results?
Well, Ansible playbook motdrole.yml
is going to do the role.
And if you check Ansible on Ansible2
- a
cat etc
newmotd,
then what do we see?
We see, yay, welcome to Ansible2.
Everything is looking okay.
And the interesting fact, it is overwritten at the variable.
Now there's a couple of more things that I want to show you
about roles, and that is these pre and posts.
If you look at pre tasks, what do we see in pre tasks?
This is an example of how you can use pre tasks.
Why would you want to update your cash before rolling motd?
I don't know
but I just wanted to show you the pre-task
and that is how it works.
And so pre-task is important.
Pre-task is to define the order of tasks
and how they are going to be started.
And that is about it. Enough about roles.
Let's go to the end of lesson lab.