One of the very useful feature of Ansible is using roles.
Now, what our roles roles simplifies a long playbooks by grouping tasks into smaller playbooks.
Or you could say the role or the way of breaking a playbook or a big playbook into multiple smaller
playbook files.
This simplifies the writing complex playbooks, and it makes them easier to reuse now.
Moreover, writing ansible code to manage the same servers for multiple environment can create more
complexity, and it becomes difficult to manage everything in one single Ansible playbook.
Right?
Also sharing a code among other teams can be difficult.
If you wanted to take the the playbook and share it to someone, then you have to share the whole entire
playbook.
You don't want to you do that.
That is where Ansible Roll helps solve these problems.
So roles are like templates that are most of the time static and can be handled by playbooks.
Roles allow the entire configuration to be grouped in tasks, modules, variables and handlers.
We already learn tasks and modules.
We'll learn variable handlers later on in order to better understand roles.
Let's take an example.
Here is the list of tasks that we wanted to perform on East Web servers.
The first task you wanted to do is to install a HTTP package, Apache package, start the service,
then allow the HTTP servers on the firewall and restart the firewall.
DX To reread its configuration, these are the list of four tasks.
Now I have another list of tasks for our west of web servers and for our West Web servers.
I just wanted to install the package and start a service.
All right.
So this is what I wanted to do and I wanted to do with one play playbook.
So if you want to combine all these tasks into one playbook, then you will see it has a list of tasks
one, two, three, four, five and six.
So let's say if you have another group of remote clients, let's call it North Web servers and you wanted
to do for them only start the service, then you have to create that as a seventh task.
Or you just wanted to do install HTTP package, then you have to do as a separate seven task.
So this makes your playbook a lot longer and of course it's hard to manage as well.
That's when we will use the roles to make our playbooks look a lot neater, cleaner, organized and
easy to manage.
All right.
By the way, when we combine these tasks, we're going to name this playbook as a by role YAML file.
All right.
So how are we going to transform them into different roles?
So here is our playbook, but the first for first task for east of Web servers and the last two tasks
for vast Web servers.
So what we could do here is, as we could see, the first four tasks are unique to East Web servers.
So what we will do, we will make a playbook out of this.
Sorry, we will make a role playbook out of this.
So the playbook will be the same as as we would write any playbook, but we will put them in the roles
directory.
So it will have set up a HTTP server as a name and the task will be installed.
HTTP package, start the service, enable the HTTP port and restart the configuration and we will name
that within the roles.
Then the second part of this playbook is to install the HTTP package and start the service only for
West Web servers.
Then we could do is we could create another role and that role we could just call it basic install because
that basic install only does the HTTP packet install and start service.
This includes installing, starting, enabling port and restarting the firewall.
So we will call this role as full install.
All right.
Once we have these roles created, then we could write our final playbook.
And this is a very simplified version of our playbook.
What we will do is we'll write the playbook, of course, define dash, dash, dash with the name and
the name will call it full.
Install the host where we will run the full install will be East Web servers, right?
This is the list of all the four tasks we wanted to run is web servers and then we will define.
There are roles right here now which your role you wanted to run for web server.
We wanted to run full install role.
And here is the list of all the tasks that we are going to put in inside of the roles called full install.
Same thing.
The next one is for basic install.
That's the name.
Where do you want to run it against?
We want to run it against West Web servers.
Okay.
And which role do you want to apply on West Web server?
We wanted to apply basic installer role.
Okay.
What is included in basic install?
Basic install includes only these two tasks right here.
These two tasks.
Okay.
That's it's just that simple.
You see how neat and clean our playbook looks now.
That's when the roles come in handy.
So I hope at this time you understand why we need roles, especially when we have a huge and big playbook
to deal with.
All right.
Moving on now, please note roles can be grouped by types of server type of applications or type of
organization requirement.
So to create roles, go to control node CD into expert roles.
And by the way, by default, when we had installed Ansible, we already had the roles directory created
for us.
All right, once we are there, we're going to make the directory for each role.
Meaning inside of these roles, we're going to create a new directory and we're going to call the role
name.
And for us, the role name is going to be basic install.
And full install.
So if you are doing the grouping of rolls by the type of servers, then you could have done make directory
web servers and with MEG Directory DB servers or make directory app servers.
So you could also have created directory by these as West Web servers or East Web servers if you are
picking certain tasks only specifically for these servers.
All right.
So what is the next step?
Next step is to create subdirectory called tasks within each directory.
So once we create these two directory, we will create a task directory on the both of these directory
and we'll name the same.
The same thing we will do is create yaml files within these subdirectories.
So the yaml file is going to be main yaml main yaml.
So from this point on the name, the naming convention for the directories and the name of the YAML
files remain the same, which is task task main main yaml.
Then let's go to our control node.
Right here is our control node.
Let's go ahead and log in.
To our control node.
All right.
Who am I?
I'm logged in as rue.
To which directory am I in?
I'm in root directory.
So let's go to Etsy and Sybil.
Directory and when you do ls minus l, you're going to notice the rules directory already created.
Now let's go to roles directory do ls minus l and you will notice I have already created basic install
and full install directories.
I have created these directories, the task directory and yaml files already so we could save some time.
Now as I'm going through this video, you you should follow everything with me.
You could pause the video right now and create that directory.
Once you created the directory, come back.
Then what we will do is we'll go into basic install directory and we'll create a directory called tasks.
So you should have that directory by now.
Same thing will go back to full install now and we'll create the same directory called tasks.
Then within each of these tasks directory.
Let's go to our first one as the basic one.
So let's go one step back.
Let's CD into basic directory, CD into task directory.
And now we have main YAML file inside of the main yaml file.
We will write our YAML file formatted role.
All right.
Same thing for let's verify we have the main YAML file for our full install as well.
So right now I'm in etsi ansible rules basic install task.
Now I will go to CD Etsy and Sybil roles full install and tasks and do ls minus l to make sure it has
made yaml.
Yes, it does.
All right, now let's go into our YAML file using the Vim editor and we will define our YAML parameters
or the task a list of tasks that we wanted to perform through the role.
All right, let's go in.
Let's clear the screen so we'll have a less confusing screen.
So let's do them.
By the way, which directory are main?
I'm in full install.
So full install should have all of these four tasks them main dynamo.
And as I said, I have already wrote down this YAML file.
Pay close attention.
The first one is to install the package.
It's there, use the module.
Yum, the name of the package to be installed and what you want to do.
Second task to start the service PD use the module service, the name of the service and state of the
service to be started.
Next one open port for HTTP.
The module, the service.
The permanent.
Do you want it to be enabled when system reboots?
US and the state right now.
What do you want to do or want it to be enabled?
Meaning allow this port on http.
Sorry on a firewall.
Then restart the firewall.
Meaning restart the firewall.
Say reread the configuration check.
Everything is good.
The especially the indentation should be good.
And if everything looks good, very nice.
Let's go ahead and save the file.
So we our full install tasks main yaml.
Roll is done.
Next one is you'll do the same thing for a basic install main die yaml file and here is the list of
tasks.
Let's go back in to our control node and we're going to see ansible roles, basic install tasks and
here we have our main all we'll do Vim editor to main the YAML file and we have a very basic install
is just to install a ADB package.
Yum.
Is the module the name of the package?
The state?
Same thing with this service.
If you notice one thing in this YAML file, we are not defining the task because that task, the directory
that this main I.M. file is in, that directory is called tasks.
So Ansible is smart enough to figure out that this role is the task.
All right, so let's save it.
I have created that already for you to save some time.
So our basic install main yaml file is there.
All right.
What's the next one?
Now go and create at see ansible playbooks by rolled out yaml file.
All right.
So we have this playbook that we have to write as our final playbook.
So we'll go into our Ansible control node and we'll go into Etsy Ansible and Playbooks.
This is the directory inside of Playbooks Directory we have a playbook called by role the YAML file.
Let's do ls minus tr inside of this directory and you will see by role the yaml file the playbook that
I have already created.
Now when I do them editor by role and you will see the very first line we have defined the YAML file,
the second line, the name of the task that we are running which is full install host where we are running
it against, we are running it against on all hosts because we do not have a West Web server, we do
not have East-West servers.
So we are going with what we have and that is all and all is only one remote client that we have that
we're going to run our full install on.
All right.
And the role, what do you want to do with this?
Host?
We wanted to run the role and the role that we have defined is called full install.
So please pay close attention on the invitation and the formatting of this YAML file.
So we'll define the roles right aligned vertically under name and host.
Then the second task would be basic installed.
And where do you want to do the basic install on?
We'll do it on our local host, which is our Ansible control node right here and the roles that you
want to use to run the task.
We will use basic install.
Everything looks the same.
Perfect.
Now let's go ahead and save the file.
All right, so our file is saved.
We created by G.M. playbook.
Now it's time to run it.
Before we run it, we know the full install is going to install a HTTP package on a remote client.
So let's go to our remote client first.
Let's log in as route.
As you can see, I'm logging in as root.
Hostname is our linux client one.
And before we install DPD package, let's make sure it does not have a HTTP package already installed.
If it does, yes, it does.
We'll go ahead and use yum!
Remove HTTP package.
All right.
So that is removed.
It should already stop the service as well.
All right, it's done.
We'll check to see the firewall.
DX firewall where we want to run.
List all.
We wanted to enable see it has already enabled which we will do is will remove that.
So to remove that will use firewall cmd command dash dash.
Permanent dash dash remove dash service equal http.
And it should remove our HTTP service from here.
And now let's check a list all and we'll see that http.
It's not removed because we have to reload.
Load the firewall.
The.
Configuration.
Okay.
I think it's Da'esh, Daesh.
All right, it's reloaded.
And now let's go ahead and check to see if the list HTTP is gone.
Yes, http gone.
All right.
So the HTTP package has been removed.
The HTTP service has been removed from firewall.
It's and the next thing we'll do on the local host to make sure the local host does not have the HTTP
package.
All right.
It does have it.
So we'll do yum!
Remove HTTP feed and it should remove it asking for confirmation.
All right, so it is a move.
Let's check one more time.
RPM Q graph for http.
Nope, nothing is there?
All right, very good.
Now let's clear the screen and go to our playbooks.
We are in our playbooks and let's do less minus l tr and we'll running by role dynamic file.
Right?
So let's clear again, Ansible, that playbook by roll dynamo.
Let's hit it.
And moment of truth.
Let's cross your finger and let's hope that it will run successfully.
All right.
Full install right now is running and gathering all which is our remote client, which you only got
one remote client full install.
So it says task is full install, which is install HTTP package.
So it's going to full install and it's going to install the package, start the service, enable the
port, restart the firewall.
Good.
Then it's coming to right here.
The play is basic installed.
That's the name gathering facts, which is for the local host.
Now the task is to install basic install HTTP package right here, install and restart the service only.
So see right here, the first task is change four, which is the four changes it made.
The second task is to do only install package and start the services changed.
Number two.
All right, we didn't get any error message.
Congratulations.
So it worked.
Now verified if it worked on our remote client and local host, which is our Ansible control node.
So let's go to our client first and let's do our PM dash queue and grab for http d.
Nice it is there.
So now let's check the http service started or not.
Sys ctl status hpdi.
Yes, it has started.
Now it's time to do the firewall.
To check a Fed enabled HTTP port on firewall dash, dash list dash all.
And then then yes, it did.
Beautiful.
So it did everything that it's supposed to do on our remote client.
Now let's go to our local host or control node and let's see if it actually instal the package http
feed.
It did.
Okay, now let's see if it started the service status, http PD and it did.
Okay.
You see, our tiny, small playbook, which is only by role, is just a small playbook.
And it's doing the big things because we are separating the tasks within smaller playbooks and that's
what the functionality or the feature of roles.