Getting started with Ansible 07 - The 'when' Conditional

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello again everyone welcome back to my channel and welcome back to my ansible series i love ansible and i'm really excited to be doing these videos i hope this series has been as awesome for you guys so far as it has been for me to create these videos now in today's video we are going to look at a way that we can differentiate a playbook by host in terms of which distribution the host is running it's very common in most linux shops to run multiple distributions maybe you are an ubuntu shop or a debian shop and you have a need for a different distribution for a one-off case or maybe you are split 50-50 but regardless of what the situation is perhaps you want to run plays only on certain distributions and that's what we are going to explore in today's video so let's go ahead and dive right in [Music] so here we are on my laptop and before we get started i do want to show you guys a very important thing to do when you are working with a git repository now this is not actually specific to ansible it's just a best practice when you are working in a collaborative environment where you have multiple people working on the same repository now for us it's just you and i so you're not actually working on a collaborative get repository in this series but we're going to pretend as though we are so i'm going to cd into the directory for our repository and these are the files that we have in there right now now the best practice i want to show you guys is running git pull what get pull will do is it'll go ahead and pull down any changes that have been made since you last well pulled changes so far we've only been committing and pushing changes but if someone else on the team has added something to ansible we might want to pull their changes down now if version control is being handled properly there's going to be a code review before people merge their changes in but that's beyond the scope of this video at a minimum it's just a really good idea to do git pull every now and then just in case someone else on the team has pushed any changes since you last worked on it so i'll just press enter and entering the correct passphrase is also a best practice but anyway we already knew what was going to happen which is essentially nothing it says we're already up to date again you're the only one working on this repository so that's to be expected but get pull what that will do is pull down any changes like i mentioned that may have happened since the last time you worked on this repository anyway we're all up to date and we're ready to go now what i'm going to do is add a fourth server now you don't actually have to follow along with this part right here because i haven't even told you to create a new server i just want to show you an example that's going to set the stage for this video and this is going to fail anyway so just go ahead and watch this you don't have to actually go ahead and do what i'm doing but anyway what i'm going to do is edit the inventory file and i'm going to go here to the bottom and i'm going to go ahead and add another server the server that i created off camera and that's the ip address my dhcp server gave that server so i'll save the file and then exit out so now what i'm going to do is go ahead and run the same playbook from the previous video the one that installs apache so again that's ansible hyphen playbook and we want it to ask the sudo password or the become password as ansible refers to it as and the playbook we want to run is the install apache playbook again we created this in a previous video anyway i'll press enter and let's see this fail and we can see that it actually failed it says failed equals one and it's actually referring to our new server right here and i'll just mention real quick that off camera not only did i create that server but i also went ahead and copied my ssh key over to that as well the ansible key in particular to make sure that we are actually able to ssh in and run ansible commands but it still failed so let's scroll up and see if we can figure out why and honestly you've probably already guessed why but as we can see right here we are running the apt module to update the repository index but it's telling us that the apt-get update command there's really no such command no such file or directory apt-get so we can already determine right here that this module the apt module isn't compatible with this particular server because apparently it's not running a debian or ubuntu-based distribution now if you've worked with other linux distributions then you already know that some distributions use different package managers so for example dnf yum pacman there's a number of them but if we're not running a distribution that has apt installed or uses apt at all to begin with then obviously we are not going to be able to run the apt module against it so i'll go ahead and clear the screen here and over here on the last tab i have the new server we can see that it is actually running centos we can see that right here because that is the name of the host if the host name was not sent to us we could simply do cat slash etsy os hyphen release just to see what that file contains and there we go we can see that we are running centos and it's version 8 and centos doesn't have the apt command at all so if i do which apt there's no such thing and if it's old school it might be apt-get but of course that's going to fail as well because well this is centos it doesn't actually use that so how do we actually work around this problem so let's go ahead and bring up our previous playbook and see if we can fix this so i'll just go ahead and edit that with nano again or you could use whatever text editor you want to use and i'll open that up here and right away we can see that it's running the app module i'm sure you remember that from the previous video but what we want to do is not run the apt module against a distribution that is not supported so i'm going to show you a way to accomplish that right now so i'm going to add a new line here at the end i'll make sure that i'm lined up with the a for apt and we are going to type win and then colon and for win you can actually use this against any kind of variable if you remember in a previous video we were able to use gather facts to basically just pull a bunch of information about the server we can actually use any of those variables here to complete our win statement but i'm going to give you a built-in variable right now and that is ansible underscore distribution and we want to run this only when that is equal to ubuntu now if you are running debian for example you can just replace ubuntu with debian and it'll still work but we want to go ahead and add this to each of these plays here let's go ahead and save this file and let's see if our result is any better now now look at this everything was successful we have no errors at all failed is zero all the way down here now what we also see is that with this host this new one right here it skipped three plays and well that's because i made it a requirement for the distribution to be ubuntu for those plays to be executed and since that's false with centos it just basically skipped everything in the playbook that did allow the playbook to finish but you know that's not very useful for our centos machine we don't actually have apache or any of the changes there that we have made in our ubuntu system so we can go ahead and fix that and we will later but i want to go ahead and show you a few more things first so let's go ahead and bring that file back up in our editor here now one thing i want to show you is that if you want to basically run this against a few different distributions then the syntax changes a bit we will use in instead of the double equal sign and we will put the ubuntu in double quotes and square brackets here but then we can also do this so for example if you had a mix of debian and ubuntu servers then you can write your win statement like this and it'll work because both debian and ubuntu have apt so you can use this if you have a mix of debian and ubuntu that's really easy to solve now i'm not actually going to save my changes here because another thing i want to show you guys is just a reminder of gathering facts like i mentioned and i'll go ahead and type out the command that we've used in a previous video to go ahead and do that it was ansible all and the module was gather facts and we could do dash dash limit and we could limit the results to the new server that we just added so i'll just type the ip address for that server here let's press enter and we get a bunch of information here so i'm not going to go over every possible use case for the when statement we can see that there's a bunch of variables here we even have ansible product name that shows that it's virtualbox so if you wanted to run something for example only against virtualbox virtual machines you could target your win statement against ansible product name as you see here you could do equals equals virtual box what we can also do here is check which operating system or distribution the host is running so we know how to write our win statement so for that we'll just run the same command again but we will grep our ansible distribution i'll press enter and we actually get several variables here the first one is the one that we've been using or the one that i showed you earlier ansible distribution and for this host it's centos we also have ansible distribution version so if you want to run your plays against a specific version of the distribution you can actually do that here so for example if i go back to the playbook here we could basically change it to something like this to match that variable that we saw in the gather fax now obviously this is not going to work because this is not version 8.2 of debian or ubuntu but if this was sent to us you could do something like this to target a specific version that would work just fine so for example you could also do ansible distribution then the double equal sign and ansible distribution version 8.2 so now we're targeting centos but only versions 8.2 again this will fail because we're still using apt but i wanted to write out the statement what it would look like if we wanted to target a very specific version of the distribution so i'm going to go ahead and undo this change here and now we're back to the way the file was before so let's go ahead and expand this playbook to target centos as well as ubuntu now what i'm going to do is i'm going to copy this entire section here so basically i'm going to copy everything and then i'm going to paste it right here and i basically duplicated everything so as you can see we have the first three plays the update repository index the installation of apache 2 and the installation of php all for ubuntu and they all depend on ubuntu but starting here we're going to change this around a bit so first of all we want to change these to centos because these last three plays are going to be targeting centos now we have centos targeted for those but we actually have other problems because we don't have apt on centos but we do have dnf so i'm going to change all the occurrences of apt to dnf for these so that's a bit better but we also have another problem and that is that the package names are also different on centos so apache 2 that's the name of the package for apache on ubuntu and debian but on centos it's httpd just like that and actually the php package is simply php so that's a bit simpler now i left the update cache right here because that actually works on centos as well so i'm going to leave that alone and we should be good to go ahead and run this playbook now so what i'm going to do is save it and then run it and fingers crossed let's see if this will actually work so what i'm going to do is run this playbook just like we did before but now we have tasks specific to ubuntu and tasks that are specific to sent to us as well let's see what happens we can already see that the install apache 2 package name here did run on center west i forgot to change the verbiage of the name but you get the idea now we do have an actual problem with centos though if you recall in a previous video when we installed apache on ubuntu or debian the default apache website was immediately accessible but that's not the case with centos though so if i go down here to my browser we have the default web page for ubuntu for apache as i mentioned but if i change the ip address to the centos system this is going to fail and the reason why is because there are two problems actually so if i go over here to the centos system it can basically do system ctl status httpd which is the name of the service for apache on centos the same as the package name so it's not actually running so what i can do is do sudo system ctl start httpd i'll press enter type in the password here i'll go down here back to my browser i'll refresh the page and it's still not going to work why well first of all i want to mention that we want to refrain from running a manual command like this because we are using ansible to automate and we want to automate everything but i haven't actually covered starting services just yet so we're going to cover that in a future video but there's actually another command that we need to run to allow communication to port 80 which is what apache runs on by default so for that we will run sudo firewall hyphen cmd dash dash add dash port set that equal to 80 slash tcp and then enter and it says success so if i go down here back to the browser again now it works just fine now we have the apache default website for centos and you know we ran another manual command didn't we so basically we want to think like automation masters we want to automate everything so every time we run a command manually it's okay every now and then but if we are going to run that command multiple times in the future we basically want to write it down and then plan on automating that later so now we have two things that we want to automate in the future starting services we want to start the httpd service and we also want to add a firewall rule to allow traffic to reach the centos system via port 80. that's beyond the scope of this video but i just wanted to point that out to you guys so you know what the current flaws are in the playbook that we have written so far but anyway we have made some changes to our ansible git repository and we always want to make sure that we commit those changes back up to master so if i run git status we can see that we have two files that are modified so if we run git diff on the inventory file then we can see that the change that was made is i added that centos system we can see that right here so i'm going to add that and then commit with a message added centos server and it's just habit for me to type get commit messages in all lowercase so you can probably hopefully ignore that so we have that staged right there so get status we still have one more file that we have updated since we started working on this repository today so what we'll do is get diff and basically we already know what the changes are but we just want to make sure that everything looks exactly as we think it should i could press enter to scroll down here and we could see that we have added additional plays and we have added when statements to target specific distributions now again this is a very unorganized mess so i do understand that we will get to refactoring and reorganizing this in a future video but what we want to do is go ahead and commit the changes for now so i will add this current version of this playbook to version control and we will see now that we have that file staged for commit and we also have a commit that's ready to go so let's go ahead and commit the last file here or whatever you want to say it really doesn't matter we now have two commits that are staged and ready to go so let's go ahead and push those up to github and there we go we should be fully synchronized and that concludes this particular video so there you go in the next video i am going to show you guys some ways that we can actually do the reorganization and consolidation that i've been talking about to clean up the playbook and actually make it look better so go ahead and check out that video when it's uploaded if it hasn't already been uploaded and i will see you there do you
Info
Channel: Learn Linux TV
Views: 67,577
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: BF7vIk9no14
Channel Id: undefined
Length: 20min 57sec (1257 seconds)
Published: Wed Aug 05 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.