Getting started with Ansible 14 - Roles

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome back to my ansible series you know the playbook that we've been working with it's a bit messy there's all kinds of consolidation that we can do some refactoring that we can do we can definitely simplify that quite a bit and in fact we did go through a little exercise in reorganization earlier in the series but in today's video what i want to show you guys is the concept of roles which allows us to better split up our tasks in a way that makes much more sense so what we're going to do is check out the concept of roles we're going to implement that and we will use it as an excuse to go ahead and do some reorganization and make everything look much more cleaner than it does right now so let's get started and check out how to implement roles in ansible [Music] and before we get started let's do a quick sanity check i'll go into the ansible repository directory here and i just ran git status i wanted to see if there were any changes that needed to be pushed up to master and of course we will run a get pull to make sure there are no changes in the reverse direction and there's not so we're good to go so at this point what i'm going to do is just copy and paste the playbooks instead of type them out i try to type them whenever i can but things are getting much more complex and i think that it's just going to be easier that way so i'm going to go ahead and open up the playbook that we've been working with which is this one right here and i'm going to change it quite a bit actually what i would like to do is just make a copy of this so we have the original so i will run cp for copy and then that playbook and then i will rename this to before roles so at least we have a copy of the playbook before we make the changes that we are about to make so go ahead and pull up the playbook right now and in this one playbook file we are doing essentially everything we did split out the bootstrap tasks in its own playbook in the previous video but if we go through this we are doing quite a bit of stuff here and while everything works and everything is running just fine we definitely want to clean this up so what i'm going to do is rewrite this with the concept of roles and then i will be right back as soon as i have everything typed out and here we have the new version of the playbook at first it looks very similar doesn't it we're going to run some pre-tasks here we're going to go ahead and update the cache now if i scroll down you can begin to see where things are drastically different so for hosts all i'm just including a role named base for workstations i'm including a role called workstations for web servers we have the web servers rule for db servers down here we have the db servers role and then of course we have the same thing for file servers now we can't run this yet because it will fail we haven't actually created the roles yet so i'm going to save this file and if you didn't already know there will be a link in the description below this video so you can copy the code that i'm using from this video so you don't have to worry about typing everything out if you don't want to anyway i'll save it and close it and let's look at the directory structure here we have a files directory right here we have a bunch of playbooks we have the site before roles playbook which was the original one from the previous video and this is our new one right here now what we want to do is create the actual roles and to do that we will need to make a new directory called roles and inside there we will need to make a new directory for each of the roles that we plan on creating the first one i'm going to create a role called base another called db servers file servers web servers and finally one called workstations so to backtrack a little bit here we now have the roles directory in our base repository directory inside there we have a directory for each of the roles that we will be creating so i've cleared the screen and we have these directories right here that represent our roles and inside each of these we need to create yet another subdirectory this one is going to be called tasks so what i'm going to do is make the directory and then i'm just going to essentially repeat the same thing for each and i forgot to add the one for file server so i'll go ahead and do that so now inside each of these directories we will have a task directory and inside the task directory is where we are going to put our playbooks or as they're called task books inside each of these so what i'm going to do is just basically go into the first world directory which is base then inside the task directory it's currently empty we're going to go ahead and create a file and we're going to name it main.yml i'll press enter and what i'm going to do is go ahead and paste in the code for this particular file which is actually just this one right here that's it now you're going to notice a few differences here from a playbook again this is called a task book when you have a yaml file inside a role it's not really a complete playbook but it's a task book we don't have at the beginning here hosts and then all or hosts workstations or whatever it is we just have a task and you can have multiple tasks here this particular one only has this one play inside here so that's why it's called the taskbook it's like a playbook but it's a bit simpler than that so i'm going to save this file in case you're wondering all i did was grab the text from the playbook in the previous video where we are adding the key for the simone user and i just pasted it in here because that right now is the only task that we actually need inside this particular file so i'm going to back out of here and here and what should we work on next how about the db servers role need to be in the tasks directory and again nano main dot yml this file is going to be named the same inside each regardless of what the actual role is it does need to have this file and the structure so we have a folder called roles and then a folder that is named after the role inside there we have a task folder and then inside there we have main.yml that's always going to be the same here so now we have another empty file so what i'm going to do is paste in the code for this and what i did was i pasted the code from the original playbook that pertains to db servers because that's what this role is for and you could probably notice a theme here i'm basically segregating everything that we had in our main playbook or our site playbook from earlier and i'm just dividing those tasks into basically roles so i'm going to save this file there's nothing actually new here and then i'm going to back out again and again and let's go ahead and go into file servers and then tasks same thing yet again and for this we only have one place so i'll paste that in and again i just grabbed that from the earlier playbook nothing new here i'll go ahead and save it and also notice how everything is lined up to the left in the original playbook everything was tabbed over a couple spaces to the right but that was only because we had a host section in that file and everything needed to be nested under that but we don't have that host section anymore so we are all the way over to the left anyway that rule is done next up we have web servers and we're going to go ahead and paste in the plays that are specific to the web server group we have quite a bit more code here because we had quite a few more plays inside the section of the playbook that was handling setting up apache because i just kept using that as an example over and over again but again nothing new here all i'm doing is installing the packages just like we did before so i'm just going to go ahead and save this file and close out same thing here then we can go ahead and set up the workstations rule and there we go i just pasted in the tasks that are relative to the workstation group so essentially just installing the unzip package like we did before and now we are pulling terraform from the terraform or hashicorp website and storing that locally so nothing new yet again we went over that in a previous video so we have most of the roles set up but we don't have everything done quite yet if we were to run this it would still fail and that's because the code that we've added is requesting files because if you remember we were doing some file management as well and when it goes to retrieve those files the files are not going to be where they should be in a role so i'm going to back out and again let's go into the web servers role and right now we only have the tasks directory right there but we need one more and it's going to be files so we have those two the files directory is not required to be in the role directory unless you actually are going to copy files so what i'm going to do is i'm going to copy the file that we were working with before it should be right about here that's the default site html file and i'm going to copy that into the files directory so now we have it there so when we look at the actual taskbook i'll just go ahead and bring it up real quick just so you're aware of how this is going to work we have a section that's going to copy the default site file and that's the last one right here the source is defaultsite.html we see it on this line right here and if this is actually being requested inside of a role that's when it's actually going to expect to find a files directory and then inside the files directory it's going to expect to find the file that we are referencing so i'm going to go ahead and run this and then i'm going to explain how this actually works so that way you can fully understand how roles are implemented so is it going to work well let's go ahead and find out it's time for the moment of truth we will run ansible playbook yet again and we will run it against the new version of our entire playbook that now includes roles let's see if it works if all goes according to plan there should be no changes because i didn't add any new tasks whatsoever i only sorted the original ones and just made it cleaner and there we go everything was a success there was no errors there was also no changes there shouldn't have been so let's go ahead and walk through this to make sure that we have a good understanding of how roles actually work now first of all we are running the site.yml file anytime we do an ansible playbook run this is the file name that we've been given it so essentially what's going to happen is ansible is going to look at this file and the first play is targeted towards all and this section here simply updates the cache nothing new here this section is pre-tasks which means like i mentioned earlier in the series we want these tasks to run first before any other tasks and that's why we included the update cache here because you want to make sure that the repository index is not stale nothing new there but then when ansible goes down here it's going to see hosts all and that literally means every single server in the inventory file is being targeted here and it's going to apply the base role to every server so that's the first thing that it's going to do so i'll minimize this and what that means is ansible is going to look for a directory in the base folder called roles it's going to find it because it's there and then it's going to look for a directory that is named after the role we are applying in this case it's base so it's going to find that all set so far and inside there it's going to add a minimum look for a task folder that is a requirement there's other folders that can be here depending on which features you are using but that's beyond the scope of this video so it does find the tasks folder is going to go in there and then it's going to look for a file called main.yml it's going to find it because it's right there and then it's going to run every play inside that task book from top to bottom we only have this one play so that's literally all this role is going to do right now i'm going to go ahead and back out of here then what's going to happen is it's going to run this play right here it's going to look at all the servers that are a member of the workstations group and it's going to know that based on the inventory file because that's where we set it and right here we have workstations we only have one host that is a member of that group so for that server it's going to be applied the workstations role which means it's going to walk through the entire process again it's going to take a look at your roles directory it's going to look for a role directory named after that role and there it is and again a task directory it needs to find that and then the main.yml it's going to go ahead and look at that and again run every play from top to bottom then again it's going to go through each of these and the web servers one is of special note because we have another folder in there so i'll close out of here so you know like always it's going to look for a folder named web servers or a folder named after the role so it's going to go into the roles directory web server and then it's going to go into the task directory and it's going to run the main.yml file just like you did with the others top down so it'll go here and install the apache package it'll start and enable that service if it's ubuntu it's going to run this we are currently differentiating by ansible distribution and to be fair we could consolidate this quite a bit more but you know that's beyond the scope of this video anyway it'll go down here then what's interesting is here i'm telling it to grab a file by the name of default underscore site.html and put it at this location on the server and if we back up one directory as i mentioned before we have the files directory there and we have the default site file right there and then let's go ahead and run it to see the output one more time and there we go everything works just fine so if i scroll up a bit here we can see exactly what it's doing again it's going to first work on all it's going to gather facts from all the servers here it's going to update the repository index for both like i mentioned these are the pre-tasks that were run then for hosts all it's adding base notice that the base or the name of the role is here first in the output so it's role name colon whoops i went down there for a bit so it's the name of the role colon and then the name of the task and the ssh key was already on those servers so those are okay it went through that now it's targeting workstations gathering facts now it's running the workstations role it's installing the unzip package and also terraform which was already there so those are okay now it's going through web servers db servers and so on so definitely practice this one a bit more create some additional roles because this is a very important concept to understand and then in the next video i am going to introduce you to host variables which is going to allow us to consolidate our playbooks even more so once you're ready go ahead and go on to that video and i will meet you there thanks for watching you
Info
Channel: LearnLinuxTV
Views: 16,536
Rating: undefined out of 5
Keywords: Tutorial, Learn Linux, ansible, ansible tutorial, templates, ansible templates, devops, ansible tutorial for beginners, infrastructure as code, devops tools, ansible playbook, devops tutorial, getting started with ansible, ansible automation, ansible for beginners, ansible playbook tutorial, ansible 101, ansible training, learn ansible, ansible training for beginners, ansible training videos, redhat, linux, linux tutorial, ansible roles, ansible overview, devops training
Id: tq9sCeQNVYc
Channel Id: undefined
Length: 19min 37sec (1177 seconds)
Published: Tue Sep 01 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.