Getting Started with Ansible 08 - Improving your Playbook

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so during the course of our ansible journey so far i've mentioned several times that our playbook is messy we could do some cleanup and reorganization and in today's video we are going to take a first look at doing exactly that we want to make sure that our playbook is efficient and it's more than just making sure that the code is easy to read that does help it certainly helps people that are looking at our code after us but it also makes ansible run more efficiently if it has to execute fewer tasks to do the same thing so let's dive right back into ansible and look at how we can clean up our playbook [Music] all right and here we are let's go ahead and dive right in so i will open up our playbook in nano just like before so here we have the playbook that we've been working with so far for installing apache and we have three tasks for each distribution so we have three tasks for ubuntu and three tasks for centos now i'm going to show you guys an easy way we can consolidate some things here to start us off and then i'll do more consolidation as we go but first of all i don't like the fact that we have two plays for installing packages here we can actually consolidate that into one i'll show you how to do that so what i'm going to do is move the apache 2 package to a new line and we need to make sure that it's aligned properly and then what i'm going to do on yet another line here is type the second package just like that which also means i can get rid of this entire section and then i can change the verbage here so what i did was i consolidated both package installations into one play because you can actually use a list just like you see here to install multiple packages with one play now down here with centos i'm going to basically do the exact same thing just like that and then i will actually do yet another line here just like before and add php so that also means i can get rid of this entire section and then i can change the verbage here let's just take a quick little look through to make sure that everything looks okay and everything looks okay to me now we didn't actually make any changes that are really going to impact this a whole lot now that we have both package installations in one play it might run a little bit faster but we probably won't notice a difference but at the very least we did clean this up a bit so let's go ahead and save the file and then let's go ahead and run it you can see that there's zero changes straight down here nothing failed basically well it's exactly the same again we just consolidated this we didn't actually make any changes that are going to be totally surprising or anything like that but i think that the playbook is much easier to read now because we have fewer tasks we have two plays for installing packages one for each distribution and that's a bit better not extremely better but it's a step in the right direction so let's go ahead and consolidate this yet again and what if i told you i could actually do everything that is being done in this playbook in just two plays at all period just two so i'm going to go down here to where i'm installing packages for ubuntu and what i'm going to do is add a new line and then i'm going to type update underscore cache colon space and then yes which means that i could go ahead and get rid of this entire first play right here because update cache is actually an option for the apt module it doesn't actually have to be separate it could be in the same task as another apt play i just edit it here as an option to the play where we are installing those packages i can do the same thing here for centos as well so i will again add update cache yes which again allows me to get rid of this entire section and it's not always going to be the case but we can see that update cache is an option here for centos for dnf that's not always going to be the case you're going to have different options for different modules but whenever possible the ansible developers they try to keep things the same and consistent as much as they can so they're not going to have an option that updates the cache on one package manager be different with a different name than another one as much as they can help it so we have the same thing here the same feature which effectively does the same thing but now we are down to two plays for the same thing this playbook will not do anything differently at all it's just going to be consolidated and better organized so let's go ahead and run it and would you look at that we were able to consolidate our playbook down to two plays and of course it's not going to do anything differently because again we're not doing anything different we're just doing it better but what if i told you we can actually get this down to one play no i'm not kidding we actually can let's actually consolidate this down to one play right now and here's how we're going to go ahead and do that so i'm going to change the name here i'm going to take out the ubuntu and then i'm going to change the name of the package here to this and i'll also change the name of the php package as well i'm going to do the same thing basically so if you're looking at this for the very first time and you haven't seen these brackets in double quotes in ansible before what these actually are are variables so i'm basically referring to a variable called apache underscore package we can call it whatever we want and then another variable php underscore package the names are arbitrary but we have these curly braces here to basically differentiate this as not being a name of something but actually a variable and because we want to use a string the names of these packages are strings we want to have those in double quotes what we're going to do is take off the win ansible distribution here we're going to get rid of that and in fact let's just delete everything else too so now we have just one play that will do the same thing as our playbook has been doing it's not going to work just yet we do need to do one more thing here so let's save this file and actually because i am very particular i'm going to take the two off of apache because that's just the name of the package on ubuntu i want to be a little bit more general anyway i'll close out of the file and now what we need to do is edit the inventory file we have these hosts right here and what we need to do is declare the variables that we just added to our playbook so i'm going to go ahead and add it to this one right here okay so i have added the variable to each line there's actually a much better way to do this than i'm doing it here i'm just giving you an example of a method that we can declare the package names as variables and then refer to them later now here i have the first three hosts declaring the apache underscore package variable as apache 2 in the php package here as lib apache 2 mod php and i'm doing the same thing for the first three hosts because those three hosts are ubuntu hosts the last one is centos and i'm going to make the variables equal to something else for these because again the package names on centos are different i'm going to go ahead and save the file and again i'll bring up the playbook you can see that we are referring to the variables here and we're doing all of this in one play now there's one more change to this file that we will need to make before we're going to be able to run this and i left this for the end because i wanted to bring up the existence of a different module now here we are calling apt obviously that's going to fail on the centos system because it doesn't have apt so what i can do is i could change this to the word package package is a module in ansible that is a generic package manager it's just going to use whatever package manager the underlying host or the target server actually uses so dnf for centos it's automatically going to use that if you run this against an arch linux host then it's going to automatically run pacman so that's basically going to be the magic ingredient that's going to make this work so i'll go ahead and save the file and before we run it let's go ahead and take a look at the documentation for the package module here on my browser i still have the documentation open right here for apt now what i can do is just go ahead and search for package and here it is right here and it basically tells us that this is a generic os package manager it installs upgrades and removes packages using the underlying os package manager now if we scroll down here we can see that we have fewer options when we compare this to dnf or apt but you know it's going to allow us to consolidate our playbook even better so i think it's a win either way well is it a win is it going to work let's go ahead and check it out and try to run it let's see what happens and that's it it actually ran faster than it did in the past and again we were able to do the same thing with just one play that's pretty cool so to be fair it can be argued that this may or may not be the best way to handle a multi-distribution linux shop because we are hard coding the variables for the package names that can get a little tiring but you know at the end of the day it's up to you the administrator on how you want to handle situations just like this and i just wanted to show you how far down you can actually consolidate and reorganize a playbook and we were able to get the playbook that we've been working on basically down to just one play which is awesome so i hope that was helpful for you guys and again please subscribe to my channel if you haven't already done so and you will be the first to see the notification for the next video when it comes out and also click that like button if you like this video i would really appreciate that and also check out my patreon page if you would like to support this channel so thanks again for watching and i will see you in the next video do you
Info
Channel: Learn Linux TV
Views: 51,525
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: JJ-aoyydfVU
Channel Id: undefined
Length: 13min 12sec (792 seconds)
Published: Fri Aug 07 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.