Getting started with Ansible 12 - Managing Services

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello again and welcome back to learn linux tv and welcome back to my ansible series we are up to video number 12 and in this video we are going to take a look at managing services so let's go ahead and get started [Music] all right so let's go ahead and change into our ansible repository directory and check for changes even though there won't be any but it's a good practice to get into and there we go we are good to get started so what i'm going to do right now is bring up the playbook that we've been working with for quite a few videos now it's this one right here so in a previous video when we were working with centos we had an issue where i had to start apache manually and in that video i mentioned that we would get into managing services well here we are and we could go ahead and fix that problem with centos to make sure that we don't have to do anything manual to get that going so let's go ahead and find that section in our playbook it was in the web server section and here that is and this is the play right here that installs apache on centos servers and one thing about centos that's different than ubuntu is that when you install a package on centos that includes a service it most often won't be started automatically they leave that up to you so what we're going to want to do is make sure that apache is running after we install it so i'm going to add a new play and i'm going to add it right about here for the name i will say start httpd and then in parentheses this is not required but i'm just adding sent to us here just so when we look through the logs when this runs then we will know that this is a centos specific play i'm going to go ahead and add some tags so that way we can target this specific play or at least have this play run with all the other apache related tasks and i will add apache centos and httpd as well and the module we will want to use is called simply service this allows us to manage the services on our linux servers and the name of the service that we want to interact with or manipulate in some way is httpd it's the same name for the service as it is for the package on sent to us they keep it simple and we will need to add a state and we will make the state started and finally we'll add when ansible distribution is equal to you guessed it centos just go ahead and save it then over here on the centos server let's check the status of the httpd service which should be running because i didn't change anything and we can see that it is running so what i'm going to do is go ahead and stop that actually and just to test this i have the website from the centos server open right here i'll go ahead and refresh it it's currently working then once i stop it okay it should be stopped i'll refresh the page and it fails why well because it stopped so now it's not running and if i check the status again we can see that it is not running and we can also see that the service is disabled which means that it will not start automatically when this centos server is started but before we get to that we did actually make our changes to this playbook so if i bring that back up here we have this new section of code this new play right here that uses the service module to interact with the httpd service right here and the state will be started as soon as this runs so let's go ahead and run it so we can see that something did actually change so if i scroll up here just a bit we can see that the change was right here start httpd and that was changed on the centos server just like it should be so if we check that server now we can see that it is now running it was started 48 seconds ago and if i go down here to my browser and refresh the page it should work and there it is that's pretty awesome so we were able to start the service with ansible that's pretty cool but the problem now is that it is still disabled so if this server gets rebooted then this will not start apache will not be running so let's go ahead and fix that right now so i'll pull up the playbook we've been working with let's go ahead and fix it and actually it's extremely easy to fix this problem because we only need to add one new line i will add enabled yes just like that that's all we need to do we don't even need to create a new play for this we are able to start the service and also enable it right in the same place so let's save this playbook here and run it and there we go well it says something has changed and it should be the same play let's double check that sure enough it was the same play that changed check the status again and we can see that it is now enabled so we were able to solve that problem as well as the fact that it wasn't running by default using nothing but ansible and automation to make that happen so now what i want to do is show you guys an example of restarting a service for example let's just say that you are going to change the configuration file or something in a configuration file and it's a kind of change that requires a service to be restarted after you make the change and how would you do that in ansible well in order for us to do that we will need a change of some sort to use as a test case so i was able to come up with a simple test case that i think works but it does require me to teach you something else in addition to restarting services and that is the line in file module so i'll go ahead and demonstrate that right now we'll bring this back up so what i'm going to do is scroll down quite a bit here it doesn't really matter where we put this but i try to keep it as consistent as i can and before we copy the default html file i'll add the new play right here i'll give it a name like i always do so a simple name we're going to change an email address that's inside a config file we'll add some tags to it and we are going to use the line in file module like i mentioned that basically allows you to well change a line in a file so i guess the name actually is very descriptive isn't it in the change that i'm actually going to make i'm going to bring up here on the centos server and i'm going to bring up the config file for apache and you don't have to follow along i'm just going to show you the change that i'm about to make and here it is so i will search for i guess that's close enough and this is the line right here that i want to make a change to so i'm going to do set and then number which is a trick in vim to add line numbers so the line number that i want to edit is 89 i want to change this server admin email address right here now this change is completely random so i just wanted some kind of change to make just to show you well a change with line and file but more importantly i wanted to have some sort of change that we would want to hypothetically restart apache for and whether or not we needed to actually restart apache for a change is beyond the point but with the line and file module i am able to make a change to a config file and when you do make a configuration change it is often the case that you need to restart the service as well so i'm going to exit out of this and we'll go back to our workstation here and let's go ahead and get this play written out so the path to the file that we want to edit in some way is that file that i basically had open just a moment ago use a regular expression and the line that we want to end up with is this one so let me go ahead and explain what we're doing here so as i mentioned the line in file module this module right here allows you to change a line in a file and this is very useful but you have to be very careful with this because there is the possibility that you could duplicate changes every time you run this i'll explain that in a moment and basically the path is where you type out the path and the file name of the file that you want to edit in some way and while regular expressions are beyond the scope of this series regular expressions do allow you to manipulate text in many different ways it's extremely powerful it's not specific to ansible but the ansible line and file module does support regular expressions and in this case the caret symbol is where we are telling it that we want to edit a line that server admin is the beginning of that line so if server admin was you know several spaces to the right this will not match it actually has to be the very beginning and that line if i bring up that file again i believe it was line 89 if i'm not mistaken i guess my memory does work anyway it's this line right here and it's basically server admin and then there's an email address a default email address there but notice i didn't even type the default email address that's there because with regular expressions i'm just looking for a line that begins with server admin i don't need to type the entire thing because it already matched that line at this point and i want that entire line to become this that's where we type line we have this option line right here and i want that line to become this right here and i only want to do that if the distribution is sent to us because the apache config file is different on ubuntu and in fact it's in a different location as well if i remember correctly so there's all kinds of differences here we are targeting centos right now just as a test but there is also another thing that we want to do here and i haven't shown you guys this yet i'm going to type register httpd just like that now what register does is it allows ansible to capture the state in a variable so it's going to make more sense in just a moment when i type the next play and i'll call this one restart httpd i'll just identify that it's for centos only of course some tags just for safe measure so again we will use the service module the name of the service is again httpd let me go ahead and center this better here state is restarted originally we were making sure that the state was started now we are making sure the state is restarted and then when and i bet you think i'm about to type ansible distribution and if you thought that you are actually incorrect i'm going to do something completely different this time i'm going to type httpd.changed just like that so basically what's going to happen here is when this play runs it's going to take a look at this file this configuration file right here it's going to look for a line that begins with server admin it's going to change that entire line to be this right here it's only going to do that if it's a centos server and then it's going to register the state in a variable name i just called it httpd i could have called this you know pizza for example it doesn't really matter what you call it you just have to call it something because we basically want to refer to this later if this has changed then we want to basically do something so here we have another play this one is to restart httpd we're going to use a service module again the name of the service is the same of course the state is going to be restarted but we only want to do that if this httpd variable has actually changed so we have this built in right here dot changed so if this variable here httpd includes a state of changed which we're referencing right here it's going to go ahead and restart the service now notice that i am not saying here when ansible distribution is equal to centos and the reason for that is because if i were to include that it would be a bit redundant because this play right here only runs if it's sent to s so this variable has no possibility of ever being changed unless it's sent to s so i don't need to refer to that again here because there's no other scenario where this would be changed it has to be sent to us to get to this point in the first place now i'm going to go ahead and run this and there's some very important things that i want to make you guys aware of let's go ahead and save the file then over here on the centos server i'm going to close out of this let's make this a little bit simpler here i'm going to cat the file and i'm going to grep for server admin just like that and we can see that the line we want to edit is right there i'll go ahead and run the playbook and we can see that i actually have a little error here that's a little embarrassing but you know what it happens so i'm going to bring that back up in the editor here and you probably saw what the error is but just in case you haven't i was missing the d right here you know what it happens to all of us so i'll save the file let's go ahead and run it so we can see that two things were changed so here change email address for admin that was changed and restart httpd that was also changed and then here on the centos server let's just run this command again and we can see that the server admin line was changed exactly the way that we wanted it to be and what i recommend when you are working with configuration files and changing lines is to run the playbook again right away and make sure that there are no changes this time because i haven't made any changes to the playbook so nothing should change here so let's just go ahead and make sure that and there were no changes why is that important well when you are working with configuration files and if you have any typos or errors then what you'll see when you grep for the line that you're changing or when you open up that file is that it will add that line again and again and again and again it's a classic symptom and even if you google this if you google line and file and duplicates you will find all kinds of people having this problem it's very very common so if you have any problem or any typo it will duplicate the line so just make sure that any time you use the line and file module that there's no changes unless there needs to be it's very important just run it again it's as simple as that now there's another potential problem you could run into that i want to point you to so bring this back up go back down here now let's say for example you have multiple changes that you want to make to this file so maybe after this play we are changing a different line right after this if you use the same variable as you did with the original play to register a change you might run into problems and i'll just basically duplicate this entire line here and let's just pretend for example this is not the same change we're making i'm not actually going to save the file but let's just say for example when this play runs right here it registers a change and it's going to store that change in this variable just like it should but then when this play runs it does not register a change because maybe the requirements of this play is already met then this variable is going to be replaced with a variable that does not include a change which means that this restart play right here will never run because this variable here at this point does not include a change because it's going to use the most recent version of that variable so if this one right here is a change and this one was not then this is going to say basically no changes here and this will not run i just wanted to make you guys aware of that because when i first started with ansible it took me longer than i want to admit to figure that out anyway i'm going to go ahead and undo my changes here so anyway i will close out of this file we have no more changes to make for this particular video but you know what you now know how to start services how to restart services so you should be all good to go for now just make sure you practice the concepts in this video and that you master these before you move on to the next video so in the next video we are going to take a look at adding users to our system via ansible and it's just going to get more fun from there so go ahead and check out the next video if it's uploaded if it's not uploaded yet just go ahead and subscribe to my channel if you haven't already done so to get notification as soon as that video is out and when it is i will see you there do you
Info
Channel: LearnLinuxTV
Views: 8,985
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: soeBHGAMkoQ
Channel Id: undefined
Length: 21min 13sec (1273 seconds)
Published: Mon Aug 24 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.