Getting started with Ansible 11 - Managing Files

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so far in our ansible journey we've automated some things such as installing and updating packages and while that is certainly a great thing to automate there's certainly a lot more to linux administration than installing packages in this video we are going to take a break from installing packages and we are going to look at the concept of copying a file to our server we're going to create a file in our git repository and then we are going to use ansible to grab that file and put it in a very particular place so we could do things like customize a default website or maybe install a default configuration file there's all kinds of use cases for this so let's go ahead and check it out [Music] so let's go ahead and do a quick check we'll just do a get status make sure we are fully synchronized and we are and then of course a get pulled to make sure no one else on our team has basically committed any changes since we last worked with the repository and we should be good to go so what we want to do right now is create a new directory so i'll just do mk dir i'll call it files clever i know and now we have that directory among all the others here now notice if we do get status that it's not even showing that we've made any changes at all and that's because well we don't have anything in the files directory it's an empty directory by default git is not going to copy an empty directory or give you the opportunity to add it to version control long story but just so you know but what we can do is use nano or whatever your text editor of choice is to create a file in that directory and i'm going to call it default underscore site dot html you don't actually need to have an underscore instead of spaces it's just a linux admin thing it's habit but it makes things a lot easier when you're working with a command line because you don't have to take spaces into account but anyway i'll press enter and let's go ahead and add some content i'm going to create a very dumb html file it's not going to be impressive at all but we want to basically use this as an example of copying a file to our servers so i'm going to give it a title so basically the title of this website is website test then in the body we'll add a small little paragraph here let's go ahead and close the body tag and then the same let's close html and we have a basic html file this is not a course on html or web design or anything like that but this is a very very basic example we just simply have a title that's what should show in the title bar of the browser basically we have the body section where we can include the body of the web page and all i'm doing is just including a paragraph that says ansible is awesome which it is then i close everything out at the end so i will save the file and exit out now in our playbook we need to go ahead and add a play to copy this file to the server so let's open up the site file right here and then let's go to the end of the web server section which is about right here we'll create a new play and i'll give it some tags to keep in line with what we went over in the previous video so the module that we are going to use is copy and what copy allows us to do as you can probably guess is copy a file to the server we need to give it a source which is abbreviated and the source is default underscore site.html you know basically the file that we created in the previous step we also need to give it a destination which is also abbreviated and in our case that will be slash var www html index.html just like that we will give it an owner i'll just set that to root then group i will also set to root and then we'll give it a mode of 0 6 4 4 which will set the permissions of that file and linux permissions are beyond the scope of this video but i do have videos on my channel that goes over that if you want to learn more but anyway what we're doing here again is we are using the copy module we have source and the source is the file name that we want to copy over to the target server now if you remember i created a directory called files and inside that directory i placed this file i created that new because that didn't exist before now i'm not actually calling out files here the directory name of files is assumed so we only need to include the file name and then we have the destination the destination on the target server is going to be var www.html index.html enter this here and the file name here on this line does not need to be the same file name as the source which can be helpful if somebody is wondering what the purpose is of this file we called it default site because well it's the default site but the destination is looking for an index.html file in this directory that's the default web root of apache so that's the file name that we want it to become on the target having a different source file name and destination file name is also useful if the destination file is a hidden file we may not want that file to be a hidden file in our repository and if you didn't already know files that begin with a period are hidden in unix and linux that just makes it easier for us because we don't have those files hidden in our version controlled repository but maybe the destination file is actually hidden so we need to include the period there so there's all kinds of useful things that we can do with this in the naming but essentially we need a source which is going to be in our files directory and then we need a destination where that will be copied to the owner and the group i'm setting to root now when it comes to web servers it is very common to set the owner and the group of that file to something like www users or something like that whatever the apache user happens to be but that doesn't matter in this case because it's just a static html file and setting the owner and the group to root is good for security purposes as well because that means that nobody will be able to change this file unless they have access to the root account and again we have the permissions here what we want the permissions to be of the file once it does get to that server so let's go ahead and save this file and we'll exit out and to run this we will use this command right here just like we always have i left out tags because i want everything to run so you can see the difference i'll press enter and let's see what happens now here we can see that two servers actually did get changes we can see that right down here so if we scroll up now we can see that this play right here resulted in two changes these are the web servers so that means that this file should exist on both of those so let's go over here to server number three and we'll see if that file exists and there it is we can see that this index.html file is exactly as we wrote it in the repository and then here on sent to us we have the exact same thing so we could see that that was indeed a success and if i go down here to the browser we can see the default website for centos and i'm going to go ahead and refresh this and we get the text ansible is awesome now if i enter the ip address for the other server the ubuntu server we get the exact same thing because ansible copied that index file to both of these servers now i want to go ahead and show you another way that we could do some file management on our target servers but before we do that let's go ahead and take a moment and make sure that we are fully synchronized with github so git status just to see what we've changed since we last made a commit and right here we have the site.yml file as we would expect and then when we check the diff on this file we can see that we have the new section here that we've added so i will do git add then that file and then git commit and then the message will be added a play to copy the default site file for web servers just like that and now we still have one more change now notice before the files directory did not show as a change when we ran git status but now that we actually have a file inside that directory it does show that but notice it's not actually showing the default site file right here now that this folder here actually does contain a file the folder in general is new so it's actually calling out the fact that files is new so we're going to go ahead and add that so git add files and then when we check the status we have the one commit that we just set up and now we have this file right here that is going to be added to the next commit let's go ahead and take care of that and there we go now at this point we have two commits that are staged locally so let's go ahead and move on so what i'm going to do is open up the playbook yet again and i'm going to add another section here and i'm also going to show you a new module at the same time so i'm going to add hosts workstations which is brand new i'll get to that in a moment become true tasks and then the first of the new tasks we will give it a name of install unzip i'll use the package module because that is going to be the same name regardless name is unzip that's the name of the package we want to install and then i will add another play right here and i will call this install terraform and the module i'm going to use is on archive which actually requires the unzip package which is why we are installing that in a previous step and the source i will give it a url and don't worry i will fully explain everything i'm doing as soon as i finish typing it so that was quite a long url there the destination will be this right here user local bin so let me go ahead and explain what i'm actually doing here so first of all i am adding a new section and this is going to be targeted towards workstations i haven't actually added a workstation to the inventory list just yet but right now i'm just setting up the plays that need to be executed towards that group again become true we'll use sudo that's essentially what that does then we want to execute some tasks and what we're going to run first is this play right here to install unzip and the unzip package basically allows you to unzip zip files from the command line so we're essentially just going to install that package the next play is a lot more interesting though because we already know how to install packages so that was nothing new but this one is definitely new and i'll go ahead and center this a bit here so the name of this play i set to install terraform terraform is just arbitrary it's just a fun example i'm not actually going to go over terraform in this video i have videos about terraform already it's just a very cool utility if you are at all getting into cloud management or cloud hosting any of those types of things or want to look at automation outside of ansible terraform is awesome now basically the source is set to this url right here how did i know what url to use well basically what i did off camera is i just googled terraform download and here i just want to make sure that terraform dot io is the domain so i know that i'm going to the right site here but anyway i'll click on this and then scroll down a bit and almost all linux distributions are 64-bit nowadays and when i hover over this link right here you can kind of see on the bottom left corner the url the text might be a bit small and i just basically right clicked this and copied the link address that's how i know what it actually is and then up here you could basically paste that into the source so what's going to happen here is the on archive module is going to essentially allow us to unzip a zip file or a compressed file in this case terraform is downloaded from hashicorp.com we want to grab it from there and we want to go ahead and grab this zip file right here from that site the destination we set to user local bin user local bin is a very common directory on a linux system where binaries are stored so we are essentially making the terraform utility available from that directory we didn't actually give it a file name because we don't actually have to give it a file name we could we could call this binary potato if we wanted to but that would be kind of silly the terraform zip file actually only includes one file which is well terraform so that means terraform will be available inside the user local bin directory so if i minimize this real quick i'll just do control z to go back to the command line and then let's take a look at that directory there's nothing in there right now so that's what's going to happen it's going to download terraform from this url and then store it in this directory now this option here remote source we are letting ansible know don't even bother looking in the files directory for this you won't find it there this is a remote source now personally i would have thought that https being the beginning of the source would have been enough to tell it that and maybe they'll fix this in a future version but we do have to actually call out the fact that this is something that is downloaded from the internet which is what remote source does mode we will set the permissions of the file that is extracted from the zip file and then we simply set the owner as root in the group as root as well so i'll save the file so now that we have all the required plays in the site.yml file in our playbook we haven't actually added the workstation to our inventory list yet so let's go ahead and take care of that right now so we'll edit the inventory and then we could go ahead and create a section for workstations and then we can go ahead and add the ip address of our well workstation and in my case that is one seven two one six 135 so now that we have that i will go ahead and save this file now next we will need to add the ansible key to that workstation so we will use the ssh copy id command and we will use that against this ip address i'll say yes to accept the connection type the password and we should be good to go now you might be wondering why i'm including a workstation section here so real quick before we run it let's take another look at the file and the takeaway here is that not only can we copy files to servers we can use ansible to provision our local workstation as well and that could be very useful because in this case we are making sure that we have terraform installed on our workstation or at least that's what we will be doing now it's very common for people that work in linux administration to want their local computer to be set up in a very specific way so it might make sense to have a section for workstations and have it automate the provisioning of your own computer in this case terraform is just one example of a utility that we might want on our local workstation but generally speaking maybe you have your bash config file your vim config file or maybe you just want to provision your own user account there's so many different things that you can actually do and again this is just one example of that so let's go ahead and run it and see what happens now we can see right here that install updates is taking a very long time and the reason why is because well the workstation the machine that i'm actually connected to right now you can see the title down there in the team looks bar that has never had this run against it yet so it does have a little bit of catching up to do before it's up to date with the others so we can see that the updates have been installed and it says changed for installing terraform so so far so good and then here we can see that the workstation the machine with this ip address right here has had two things changed just like we expected so we could do which and then terraform and see that it actually does recognize terraform as a command and if i run it we get this default help page right here which shows us that terraform is actually working and we have ansible to thank for copying this over that was awesome so in this video i showed you two ways that we can basically do some file management on our servers i showed you how we can copy a file to our servers and i also showed you a way that we can download a zip file from the internet and extract it locally and there's more file management tricks that we can do in ansible than just this but i figured that these would be two fun examples to get you started so i will have the next video on my channel uploaded very soon so definitely stay tuned and as soon as i have that edited and uploaded i will see you there you
Info
Channel: LearnLinuxTV
Views: 9,944
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: teEhLgHpGgo
Channel Id: undefined
Length: 20min 49sec (1249 seconds)
Published: Tue Aug 18 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.