Getting started with Ansible 15 - Host Variables and Handlers

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
it's a hot summer in michigan we have four seasons here cold colder coldest and hot and my air conditioning is barely working so i apologize if i look a little bit sweaty but you didn't come here to hear about my heating and cooling problems you came here to hear about ansible and what we're going to do in this video is talk about host groups and that's something that is going to allow us to customize even further how each individual host is handled when we run our playbook so let's go ahead and dive right in so before we get started let's go ahead and take a look at our repository and make sure that we've committed all changes up to this point and we have several changes here and these changes all involve the implementation of roles which is what we took care of in the previous video now the site before roles file right there that is just a backup of the site playbook before we implemented roles so i'm going to go ahead and add that one real quick i'll go ahead and commit the change that leaves two files right here we have the roles directory which we added in the previous video and then let's go ahead and add the site file as well so these are the changes that we are going to make so we're going to commit everything so far and now we are all set and ready to go now in order to take advantage of host variables we need to create a brand new directory that is going to store the files that are going to contain the variables and it's going to be this directory name right here and i am creating this right in the root of the repository now if we take a look at the contents here we have the roles directory which we set up in the previous video now this file isn't really necessary i'm just going to leave it here because i want to make sure that all the files are here because different viewers of this series are going to be coming in at different times and i want to make sure that all the content is there but technically if this was a real production repository we could probably get rid of this we have the bootstrap script right here which you already know what that means and you should know what the other files are as well but now we have this host vars directory where we can actually put the files that are going to contain the variables so i'm going to go into that directory and first of all let's go ahead and take a look at our inventory file because we want to basically create a host variables file for each of our hosts and we have a few here so we need to go ahead and create some host variable files for these so i'll create the first one and i'm going to name it after the ip address of the first server there at the top now the name you want to give this is going to be either the ip address or the host name or the dns name of your server so if you have dns setup you would use that instead basically whatever you are calling it in the inventory file whatever that happens to be that is going to be the name that you are going to give the file it needs to match and then you just add the dot yml extension to it so i'll press enter and i'm going to create a few variables here as you could probably tell if you've been following along these variables all have to do with the ubuntu hosts and we're setting the names of the packages as well as the name of the apache service we're creating variables to contain those things right here and i'm going to go ahead and save this file and then exit out now what i can do because i'm lazy is i'm just going to go ahead and copy this file and i'm going to copy it to the name of another server so i'll go ahead and copy that to server 133 and then i'm going to do the same thing for 134 but what i want to do also is make a copy of this for the centos server as well which is the server that ends in 248 and now i'm going to open up the centos server file right here in an editor because all these values are wrong it was copied from the other file which you know contains all the ubuntu values for this so we want to go ahead and get this corrected so i'm going to change the name of the apache package to httpd and the same for the name of the service and then the php package name on centos is simply php so now we have our host variable files right there as you can see and that's all well and good but what the heck can we even do with those and how does that even help us well something i've been mentioning again and again and again throughout the series is that there's a lot of cleanup that we can do now to be fair host variables actually don't really have anything to do with tidying up your playbooks and your task books but i'm going to use it as an example to do that because you're going to immediately see the benefit as soon as we start doing that but essentially we created these variables in these host variable files and we can go ahead and refer to these in the task books so that basically allows us to generalize our playbooks and also have a lot more control so what i'm going to do to show you guys the benefit is i am going to open up the taskbook for the web servers role which i have edited off camera and you'll immediately see the benefit quite a bit has actually changed in that file and i didn't want you guys to sit through me typing so i went ahead and took care of that and i'm going to show you the difference right now so let's go ahead and bring that up in the editor it's this file right here and we can already notice that it's quite a bit shorter and if you look at it you'll see that i am actually referring to these variables several times so in the first play right here i changed the name it says install apache and php packages i have some tags like i did before but the big change is right here the names of the packages i just used the variables instead of calling them out previously we actually had a separate play for centos in a separate play for ubuntu but i changed the package manager to the generic package package manager and then right here i just use those variables instead of the names so that should be pretty self-explanatory then here where i go ahead and basically start and enable the apache service i change the name here i am referring to the service by a variable instead of hard coding that because the name of the service is different in centos and ubuntu now to be fair when you install apache on ubuntu it's automatically going to start apache and enable it so this isn't really necessary for ubuntu but it is actually a best practice to keep everything consistent and right here we are not calling out a different action depending on which distribution we are running we are referring to the service by the variable apache service which we declared in the host variable files and that's what i put here and just like before we're going to make sure that it started and enabled down here we have this play where we are changing the email address you know basically the random change i decided to do earlier in the series to go ahead and show you guys how we can go ahead and just restart a service or restart a service after something changes and this one i left the win ansible distribution is equal to centos right here because this will fail on ubuntu the apache config file is in a different place and we don't even really need to do this i probably should just go ahead and delete this play altogether but you know what i'm just going to leave it here for now just as an example but we don't actually need it so nothing has really changed there but we go down here to restart httpd it depends on the variable apache dot changed or at least the state of the variable apache to be a state of change which we are registering right here previously that was httpd i just generalized the name to apache and i changed it here as well to make it match and then here again i am referring to the apache service by the variable instead of hard coding that in there and then of course we have the default html file that we already had in the file there so i'm going to go ahead and exit out and i'll show you the difference which is actually well basically no difference because it's going to run exactly the same as before it's just that we were able to clean it up a bit and use host variables to generalize our place which is pretty cool so again nothing has changed and implementing host variables that hasn't really helped us a whole lot when it comes to running the playbook but it certainly helped us quite a bit when it comes to actually managing our playbooks it's easier to read and easier to manage and it just looks cleaner now another concept that i want to show you guys is the concept of a handler now to illustrate that i'm going to open up the taskbook yet again and when we go down here we are basically telling apache to restart or at least we're telling ansible to restart the apache service if the apache variable that we register in this step right here has registered a change but this isn't necessarily the best way to handle it as i mentioned in a previous video if we have multiple changes to that config file then any one of those changes can be set to no change and then no restart will happen but a handler is something that is triggered and any one of the plays in the playbooks and taskbooks can trigger that change so let's go ahead and change this so what i'm going to do is i'm going to actually just get rid of all of this right here completely and i'm going to change register to notify and what i want to notify is a play called restart apache so i was able to remove an entire play out of here because we're basically going to do this somewhere else i'm going to save this file and exit out and let's go into the role directory for web servers and that's where we have our files and our tasks we're going to make a new directory though called handlers and inside handlers we're going to create a file called main dot yml and here i'm basically going to add the same play as before i probably should have copied and pasted it and i'm going to make the name of the play equal to the name of the notify that i've added earlier i notified restart underscore apache in the taskbook i need to do the exact same thing here and then what i'm going to do is add service then the name is going to be then the state is going to be restarted and that's basically all there is to it so i'm going to save this and if i were to go ahead and run this nothing would actually change because well that's not going to be triggered unless it actually does make a change so what i'm going to do instead is go back in here to task and let's edit that main.yml file our taskbook here let's go down where we change the email address and i'll just make a random change i'll just say comment.net it's a change i guess it'll work so we'll just go with that and we're going to go back here and then go ahead and run it now what should happen is that this is going to run and it's going to actually change that value in that file the email address basically and once it does that we should see that it is going to notify the handler and then at the end the handler will run and then restart apache there we have the change and we have the handler running as well so it says changed equals two so if i scroll up here we have the play to change the email address which i changed from.net.com and it made the change this change is specific to centos but it's a good example anyway then we can see right here is running handler restart apache and then it went ahead and changed it there so basically what happened is that this change actually triggered the handler to need to be run and then it was run afterwards and the value here is that you could have 10 different tasks or however many that all make individual changes to a file if any one of them makes a change to that file then this handler will be triggered and it will be run and that works a lot better than running the register that we ran earlier which doesn't handle multiple changes as efficiently as handlers do but there you go let's go ahead and look at this one more time so basically down here we have a notify and we added that in place of the register that we were using before so this is going to notify a task by the name of restart underscore apache just as i've written it here if this play registers any change and we could also notify this task from any other task as well again you can have a bunch of them and each one of those changes can notify the restart apache handler and that's all well and good because if any one of those registers a change and then notifies this play then it's going to go ahead and run that and as soon as it does it's going to look in the handlers folder which we have right here the file name is basically the same but it's going to look for a task that has that exact name in it which it's going to find right here and it's going to restart the apache service that's a variable which we gave it in the host variables file that's where we declare this variable and then state is equal to restarted which means apache will be restarted and again we have our host variables files in this host vars directory and we have one for each host that's basically all there is to it when it comes to using host variables and using host variables has given us additional functionality that we were able to use to make our playbooks and our taskbook even better so i hope that was helpful for you guys and in the next video which is actually going to be the last video in this series we are going to take a look at templates and i will see you there as soon as i have that uploaded thanks for watching you
Info
Channel: LearnLinuxTV
Views: 8,883
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: shBlQQZLU9M
Channel Id: undefined
Length: 16min 57sec (1017 seconds)
Published: Thu Sep 03 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.