Using Ansible to automate your Laptop and Desktop configs!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
ansible has been my configuration management utility of choice for quite some time now like a lot of people i use it to manage all of my linux servers and make sure they are all up to date with the latest configuration changes that i want deployed to every server but i don't just use it on servers i also use it on desktops and laptops as well and people that have seen my laptop desktop configuration with ansible they've come away very impressed and i get asked a lot can you please do a video on how you use ansible to manage your laptops and desktops and finally that's exactly what i'm going to do in today's video in the first section i'm going to give you guys an overview of my ansible configuration and how it works and then i'm going to show you how to build a skeleton that you can use to start your own so before we get into that though i want to take a moment to mention the sponsor for today's video lynode lenode has been doing cloud computing since 2003 which is actually before amazon web services was even a thing on the node's platform you can get your server up and running in minutes and they include all of the popular distributions such as centos debian ubuntu fedora and get this also arch linux and let's be honest what could be better than a linux cloud server provider that allows you to tell all of your friends i run arch the node has multiple server plans available to make any app scalable and flexible you can use it to host a blog set up a vpn server a minecraft server or you could do what i did and set up a website for your youtube channel because the official website for learn linux tv runs on lenode and lenode offers 24x7 365 support regardless of plan size so you can get live help from a real person when you need it new users can get started right now with 100 in credit towards a new account and i highly recommend you check them out because lenote is awesome i've been using their services for quite some time now and i couldn't be happier so definitely check them out i highly recommend it with all of that out of the way let's go ahead and dive into ansible and see how we can use it to configure our laptops and desktops so in this section of the video i'm going to give you guys a look at how i configure my desktops laptops and servers and like the title of this video suggests i do all of that with ansible now here on the screen you are actually seeing a virtual machine now i actually don't use virtual machines on my channel unless i let you guys know because i prefer to use real hardware when i record desktop footage but all of my laptops and desktops are already configured so i don't really have anything to configure right now so i created this virtual machine so i could show you guys what it looks like now for reference in the description below i've included a link to a copy of my ansible git repository a copy of the ansible code that i actually use to configure my laptop's desktops and servers so that you guys can check that out and get some inspiration now i don't recommend that you run that on your computers reason being my ansible configuration is the result of five years or so of work plus i created it for myself so you may not like the result but if you do insist on running it on your hardware i do recommend that you run it in a virtual machine not on anything that's important because again i am including a copy of my ansible code for reference purposes only later on in this very video i am going to show you how to create your very own ansible configuration for your laptops and desktops so we'll take care of that later but what i'm going to do right now is just assume that this virtual machine that you see on the screen right now is a brand new computer a laptop or a desktop and i want to get it set up for the very first time for my use now obviously off camera i've installed the linux distribution and this is pop os so i didn't actually show that process so we'll assume that i've already set up linux on the device and it's time to get it configured now obviously i could just go through the settings and configure everything manually but that's not what i want to do because i've automated everything so when i get a brand new computer and i want to set it up what i do is i just open up the terminal and the screen is going to lag quite a bit because this is a virtual machine so we're going to ignore that what i'm going to do is just make this bigger make sure all of you guys are able to see this and we can go ahead and get started so when i purchase a new computer what exactly do i do actually there's only one command that i have to run that's it and the command looks just like this and the command that you see on the screen right now is literally the only command that i have to run to set up a new computer it's literally that simple so what exactly is this command doing so i'm going to break it down for you so first of all i'm going to use the curl command to fetch something from a url and deploy is actually the name of a web server on my local area network that's actually the host name for that device and all it's doing via apache is serving one file and this bootstrap file is actually just a simple bash script well it's not exactly all that simple but it is just a bash script so essentially what i'm doing is i'm fetching this file that's located on this server and this server has apache setup and is just serving this one file so curl is going to fetch that file and then i'm going to pipe it into sudo bash so let's go ahead and run it so first of all the script is going to install all updates which i've already done off camera and now it's installing ansible it's just using apt install ansible and it's installing some other packages as well nothing too out of the ordinary here so as it shows right here it's actually running the first provision and then we actually see the command right here that is actually running but i had to actually censor some of it because it's actually showing a password in clear text and i actually have yet to fix that so we're going to pretend like that's not the case i'll see if it actually looks better if i full screen this terminal here and it's actually going through all of my configuration on ansible so one of the things that you should know about my setup is that it uses ansible pull very heavily i have an entire series an ansible pull that i highly recommend you check out but you don't have to watch that video in order to understand what we're doing here by watching my ansible tutorial series card right here we'll teach you everything that you need to know about ansible the entire series is already up on my channel and it'll actually round out your knowledge and teach you quite a bit and then i also have like i mentioned a whole video on ansible pull card right here which will tell you everything you need to know about ansible pull and how it differs from normal ansible but for the purposes of this video i'm going to give you a very simple explanation now normally when you run ansible you have an ansible server and the ansible server uses ssh to connect to all of your computers and configure them ansible pull is kind of like the inverse of that with ansible pull it's not actually run on a server ansible pull is run on the same computer that you're configuring and it runs all of the changes locally ansible pull will download the changes from a server which is actually going to be a git server it's going to download a git repository it's going to save it locally and then it's going to run everything in that repository against itself and here's what that command generally looks like so first of all we have ansible pull an ansible pull is installed automatically when you install ansible even though it is a separate binary when you install ansible you also get ansible pull so if you've installed ansible already then you already have this and then with ansible pull we'll do dash capital u and then it expects a get repository so that could look something like this of course this is just a hypothetical repository and you wouldn't want to actually type this in but this is just an example of what a typical ansible poll command actually looks like it's ansible pull dash capital u and the u represents url and then you give it a url to a git repository and then it's going to like i mentioned download that and run it against itself the git repository has to be structured a very specific way and we will actually get into how to create the repository in this video and also how to populate it with actual config that'll configure your computers so what i'm going to do is actually use ssh to connect to a server that i have locally and that server is just called dev and it's actually a raspberry pi that i have as my central dev server for all of my coding it's basically where i download all of my git repositories to as i'm working on them so regardless of what computer i'm using i have all of my code in a central location that i can get to so i don't have to worry about what i've saved on which computer because everything that's related to writing code is centrally available on this raspberry pi and on this raspberry pi i have a folder called git this is in my home directory and inside there i have the actual ansible repository downloaded that i use for all of my laptops servers and computers basically everything inside that git repository i'll just list the storage here we have this file right here which is local.yml now i showed you the ansible poll command earlier and you'll notice that i didn't actually tell it what file to run and the reason for that is because if the repository includes a local.yml file at the root of the repository it's just going to run that file so basically a file name of local.yml is assumed i'm not going to go too into detail here because again my configuration enanciable is kinda huge so i'm going to give you the basics of how the layout works so if i bring up that file in an editor we can see that it's running quite a few things so first of all i'm going to target all hosts i'm going to run some pre-tasks against those hosts and pre-tasks are basically just tasks you want to run before anything else this one in particular is going to run the pacman module pacman is the package manager for arch linux if you didn't already know and it's going to use that module to update the cache but it's only going to do that if the distribution is being run on actually is arch linux and then we have a similar play right here which is using apt and it's using the apt equivalent of update cache basically update cache is going to update the repository index it's the same thing as if you ran sudo apt update for example but it's only going to do that if the distribution is debian or ubuntu so my ansible config is actually going to check the distribution to make sure that no tasks that are meant for a different distribution are being run on this one now here we have roles i cover roles in more detail in my ansible series so i'm not going to do that again here but essentially a role allows you to categorize tasks and run those tasks only against machines that are actually part of that role now here i'm actually targeting all hosts yet again and all hosts are going to get the base role applied to them so basically the base role is for everybody every laptop desktop and server the base role is for things that i want everything to have packages i want on every system my user config i want that on every system you get the idea the base role is the starting point for everything now if a machine is made a member of the workstation role then it's going to get workstation tasks run on it and then down here i have a similar role for servers so every computer every server is going to get the base role first no matter what and then they'll either get the workstation role or the server role and how does ansible know which role to apply and that actually has to do with this host file right here which is an inventory file now normally with ansible pool you don't have an inventory file but you can still use one which is what i do and as you can see here i have a workstation roll and all of the machines that are listed underneath it are all a part of that roll so that's how it knows now i'm going to quit out of there now i'm going to give you some hints as far as some of the things that i configure so inside my roles directory here i have the base server and workstation roles that i've mentioned earlier so if i go into the workstation role for example and only this one right here is actually required in an ansible role this is the folder where you put the task that you want run when that role is being applied so obviously you need that because if you don't have any tasks then well there's no reason to have the role inside that directory we have a main.yml which is the only file that's required in a role and basically what you see here is the fact that this main.yml file is basically just including other yaml files as you see here we have one for mate specifically the file manager and then key bindings and so on what's really cool though is i actually have all of these tasks right here that install specific applications so as you can see my ansible config does quite a bit as an example of some of the things that it does if i go into the desktop environments folder you can see that i have configuration for both gnome and mate and here we can see some of the things that i configure in gnome for example the appearance which means i could do things like set the wallpaper which is actually one of the things that i'm going to do later in the video there's also key bindings which is where i actually set all of my keyboard shortcuts i have a lot of code here and again i can't go over all of it because well this is about five years worth of work or something like that it would take probably an hour or two hours for me to go through everything that i'm going through here so basically you get an idea of the level of detail that i actually have in my git repository for configuring my laptops and desktops via ansible the entire reason why i've done this is because well i was recording youtube videos and i was getting tired of basically reapplying all of my settings and reinstalling my distribution sometimes after recording a video because at the time i only had one computer now i actually have a dedicated computer that's actually for the purposes of recording video but i still kept the ansible config going and it actually comes in handy anytime i test a new laptop or set up a new computer i don't have to do any work it does everything that's my wallpaper like i've mentioned key bindings installs all of my applications you get the idea so let's go ahead and disconnect from this terminal here so we can see that this actually finished ansible actually ran on the virtual machine and we could already see some of the benefit because we see the bash prompt right here and if i actually open a new window so you can see that when i first started configuring this installation of pop os it had the standard prompt now it has the actual prop that i use which is pretty cool now you can also see that the background is now blue and that's because i actually don't have this host name as one of the host names in my inventory file so ansible can't actually do all of the configuration but that doesn't really matter because i'm going to show you how to actually set this up in the next section i'm going to show you how to set up a git repository and then after that i'm going to show you how to create a skeleton that you can use to actually set up your very own configuration and i'm going to give you some fun examples as well so let's go ahead and get into the next section so now that you've seen my configuration it's time for me to show you how to create your own for this example i am going to use github but you don't have to use github it really doesn't matter where you host your code so long as you have a git repository that you can access from a terminal on your end if you are not using github then i'm going to leave it up to you to create a repository because essentially that's all you need is a git repository regardless of where you host it and as you can see right here i am logged into my github account so let's get started so what i'm going to do is create a brand new repository so for me what i'm going to do is call mine ansible desktop give it a description now i'm going to leave this repository as public now be careful what you save in this repository if there's something that you don't want to be publicly available then definitely do not include it when you upload files to this repository you can create a private repository that does change how you interact with it though it's beyond the scope of this video but for now i'm going to leave it as public just be careful what you commit to this repository then what i will do is add a readme file and then i'll create it so here we have the repository there's nothing here but the readme file so what i want to do is pull this down but before i can do that i need to create a key and then install git as well so in a terminal which i have open already right here there's a few things that we need to do now it's a great idea to create an open ssh key pair it's not required but it makes this process a lot easier it won't be necessary when it comes to applying your ansible code to your laptop or desktop but it does make it a lot easier to interact with the repository itself such as making commits and things like that so i think it's a great idea to create a key so i'm going to show you how to do that right now so to do that we will type ssh hyphen keygen and the type will be ed25519 and then dash capital c and then in quotes we can add a comment if you omit this it'll just default to have the comment as your host name for your computer but what i am going to do is just name it j which is easy enough and then i'll press enter now before we continue it's important to note that if you already have a key at this path the path that's displayed right here it will be overwritten so if you already have an ssh key you don't need to do this you can press ctrl c to break out of this process and abort creating the key so if you continue it's going to create a brand new key at the path that's highlighted and if that's okay with you we could press enter to continue now for the pass phrase i highly recommend that you add a passphrase this is optional it just adds some strength and more security to your key i'm going to go ahead and add one i just typed it in i'll press enter i'll type it in again then we can see that the key has been saved in this path right here just like we thought and if we list the storage of the dot ssh directory we should see at least two files in my case we have this file right here then we have the same file again but with the dot pub extension if we take a look at the public key we could see the contents of the public key right here and this is actually what we need to give github to add this key to our account now the private key which is going to be this one right here should never be shown in the clear and should be protected at all cost if that file was ever to leak then this entire key and security is invalidated and the key can no longer be trusted so what i'm going to do is do something that i really shouldn't do which is to show you the contents of the private key again you should never do this but i want to show you guys what a private key generally looks like and you can see that it's labeled as the private key and we have the key right in here now in my case it's okay for me to show you guys this key because i'm going to remove this key from my account before this video even gets uploaded i just like to show you guys what things look like so you can get a first-hand experience of what you should be expecting now obviously the contents of your key both the public and the private keys will be completely different i wanted to show you guys what they look like in general but anyway it's the public key the one that ends in pub which is the shorter output that we see right here that's actually what we need i'm going to copy that to the clipboard for now and then we'll come back to that but we need to install a package now i'm using popos and the commands that i'm going to show you will also work on debian as well as ubuntu and even linux mint there's quite a few distributions that use ubuntu or debian as the base if you're using a different distribution then i'm going to leave it up to you to alter the commands that i go over in this video to fit your distribution of choice anyway we will need git so what i'm going to do is sudo apt update to update the package repository index make sure everything is all fresh and ready to go and now it is and then next i will run sudo apt install git and it's already installed in my case it doesn't come default i was already playing around with this which is why i have it installed already and honestly i installed git on all of my machines but if you run sudo apt-install git it should install the package for you if you don't already have it installed on your system before we start using git there's a few things we have to do to identify ourselves first basically when we make a commit in get that commit is going to contain our name and our email address so we will need to set that first before we can actually get started for that we'll use the git config command and the first of two commands that we will use to identify ourselves is going to be git config dash dash global user.email and then you could type your email address in quotes and you should probably use whatever email address you normally use with github i'll just use a sample one for mine and then for the second command for git config i'll just recall the previous one i will change this to user.name and then in quotes basically you just put your name and then enter and that should be all there is to it now if you're at all curious where that config is stored it's in your home directory under dot get config and you could see the configuration is right there and now that we have that all set and ready to go we can go ahead and proceed to the next step so back up here in the repository what i am going to do is add the key that we've just created to my account so i'll click on my profile icon up here in the top right corner and i'll go down to settings then we have a section right here that says ssh and gpg keys so i will click on that and then i will add a new ssh key i'm going to call it ansible desktop tutorial but just give it any name you want and then what i'm going to do is paste the public key specifically the public key in this box right here and there it is i'll click add ssh key and now it's added to my account so i will click the back button several times and then here drop that down and we want to basically clone this repository and pull it down so we will do so via ssh so make sure that that's selected we could also do https but we don't want to do that we want to use ssh so i'll click on this little clipboard icon here then go back down to the terminal and we should be ready to go so one thing that i like to do is create a master folder to hold all of my get repositories because i will often be working with quite a few of them so what i'll do is create a directory called dev you don't have to do this if you don't want to completely optional and then we can cd into that directory right now it's empty so what i'm going to do is type get clone and then i'll paste in the url that i copied from the github repository page which we have right there i'll press enter i'll say yes and there we go and if i list the storage we have a folder that has the same name as our repository i'll go inside there and as you can see we have the readme file if you chose not to create a readme file then you can basically just do this to create an empty readme file if you need to but what i'm going to do right now is i'm going to edit this file and make a change to it just to show you the process of committing a change back up to github what i'm going to do is open the readme file in an editor and here is the default readme file so what i've done is just added a simple line of text here it doesn't matter what you add to this file just make some change if you are not feeling very creative you could just simply add a space to the end of the first line it doesn't matter what kind of change you make so long as you have some kind of change so in the case of nano i will hold ctrl and press o to bring up the save dialog i'll press enter to actually save the file then ctrl x to exit out so now if we run git status we can see that it's detecting that we've made a change is telling us that the readme.md file has been altered if we do get diff and then the file it's going to show us exactly what has changed since the last commit and in green right here with a plus sign which means we've added this line to the file we already knew that but what we could do now is do git add and then the file to add the readme.md and its current change to our commit then we can actually commit the changes so to do that we will type git commit then we can type a message with dash m and then i will type updated the readme file now it's telling us that one file has changed and then there has been one insertion which we already know then we do get status it's telling us that we have one commit that is ahead of the origin main branch which means that we have local changes that have been committed locally and those changes have not been pushed up to github yet so if i was to go up here to github and refresh the page here you can see nothing has changed so basically what the commit is doing it's allowing us to package up the changes to be able to ship them to github or whatever your repository server happens to be but it doesn't actually send it up to the repository we have to do that ourselves everything's ready to go so to send the changes to github we can run git push origin and then main and then press enter and we should be good to go now if i refresh the page it added the line as we can see right here so now our repository is set up it's configured we could pull it down we could push changes up to github or whatever our server happens to be so now we have everything that we need in order to move on to the next step in the next section what i'm going to do is show you how to install ansible and then we will actually write some ansible code to configure our computer in the previous section we set up git and then we also created the repository that we will be using for the remainder of the video and in this section it's time to install ansible and start using it so if you haven't already done so we need to update our repository index which can be done with that command right there and we will ignore the fact that i have updates for now and then what we can do is install ansible which is as simple as sudo apt install ansible just like that and in my case it's already installed because well ansible is one of the first things i always install but on your end this should install the ansible package and then you should be good to go now normally when people run ansible playbooks and things like that they use the ansible playbook command which is installed automatically when you install ansible we also have another binary here called ansible pull that is also installed when we install ansible itself now i've done an entire video on ansible poll which will take you deeper into ansible pull than what we're going to do in this video but the difference is that ansible pull allows you to run a playbook that is saved in a repository such as a git repository such as the one that we've set up in the previous step so what we need to do is actually create our first playbook so what i'm going to do is just use nano it doesn't matter which text editor you use and i'm going to name it local.yml so for the first example i'm going to keep it simple so i'm going to begin the file with three dashes which marks the beginning of a yaml file and if you didn't already know ansible playbooks are in the yaml format hence the yml extension as you see here now i'm not going to go over yaml in depth because that's not necessary for this video and it's not necessary for you to understand ansible you will automatically learn yaml as you learn ansible so don't worry about that now for the first line we will type a hyphen and then hosts then colon localhost so basically when you run an ansible playbook you are targeting specific hosts but since we are using ansible poll what we're essentially doing is pulling the repository down and running it against localhost when it comes to desktop and laptop configs this works a lot better than having an ansible server because for example if it's a laptop that you're configuring you could have the lid closed it could be in your bag which means it's suspended and not available on the network so an ansible server wouldn't be able to reach it and would error out but with ansible poll the machine will just pull down the configuration whenever it's online so i'll go to the next line then i'll type two spaces and i'll add connection and then local ansible uses open ssh by default but since we are running this locally and pulling the repository down we don't really need ssh for this i had you create the ssh key earlier because it makes it a lot easier to commit changes up to the repository but that wasn't required so for the connection we don't want it to default to ssh we want it to use the local connection which is why i have that next we will type become colon and then true now essentially become is enabling sudo because we will be making changes to our system and as i'm sure you probably already know we can use the sudo command to run a command as a different user that user defaults to root so essentially we need root privileges for the things that we are going to be doing since we will be doing things like installing packages so we add become true to facilitate that i'm going to press enter twice add a couple more spaces and the reason why that's important is because in a yaml file everything has to be lined up if things aren't lined up properly then you will get errors so just keep that in mind you don't want to tab sometimes in space other times you want to stay consistent so we'll type tasks and then colon so for the first task we will type colon and then name and then i will call it install h top then four spaces we want everything to be lined up we need to type the module the ansible module that we want to run there's all kinds of modules that we can run with ansible this one in particular is packaged because we want to install a package on our system i'll indent another two spaces here and then name h-top now it doesn't matter what package you install as long as it isn't already installed this is just an example h-top is a pretty cool utility to have on your machine anyway so i figured that this is a good enough example so that's all we're going to do for the first example so let's save the file ctrl o and then enter and then ctrl x to exit out so if we run git status it's detecting that we have this file right here it's colored in red which means that it doesn't know what it's supposed to do with that file it's not included in version control when you type git add and then the file name that's basically telling git that hey git i want you to keep track of this file and this essentially adds it to version control so that git can keep track of the file and its contents going forward so now if we run git status it's telling us that we have a brand new file local.yml so what i'm going to do now is commit that change type the message and it really doesn't matter what you put for the message just something where if you were going through the history you would know exactly what you were up to when you committed that change and as we already know that change exists locally we did package it up we prepared a commit but we need to send that commit up to github so to do that we will run git push origin main there you go so if i go back up here to the repository and then refresh it we now have the local.yml file and it actually added some color syntax highlighting here because it detected that this is a yaml file github is smart about things like that and then we have our actual code so now what can we do with this well what we can do is actually run this playbook locally on our machine now if i type which and then h-top note that i received no output at all which means that h-top is not installed on this computer so now what i can do is run the ansible configuration that we've just created with the ansible pull command so what i'm going to do is type sudo because it will be making changes ansible pull dash capital u and then what we need to do is grab the url for the repository so i will go back up here to the repository we want to be at the root of the repository we will drop this down now we used ssh previously to pull the repository down locally so we can work with it but to actually use this repository with ansible pull we should use the https version right here so we'll copy that and then i will paste it right here now if everything is working properly i should be able to press enter here and then it should just run so i'll try that it went by pretty quickly here but we can see that it did make a change it says changed equals one now the text is a bit large so i'll make it smaller so you can see the output better so at the very end it just gives us the results we already know that one thing was changed and then we have the task right here it says install h-top and if you remember when we added the h-top package in that section of the playbook we named the task install htop which is exactly what's here so whatever you name your task that's going to be shown in the output that's why it's important to use names for your tasks that make sense and give an accurate description as far as what you are trying to achieve with that particular task but how did this work though if you recall i ran this command right here sudo ansible pull dash u which is an argument where we provide the url to the git repository and then i included the url the https url for the repository but i didn't actually tell it what to run now we called our playbook local.yml now normally when you run ansible pull you need to give it a name of a playbook to run basically like that so what we could have done is typed local.yml because that's what we've named the playbook now actually we don't need to do that because by default ansible pull assumes that it's going to find a playbook named local.yml in the repository so as long as you've named it exactly that you don't have to type the name of the playbook itself so anyway here's the playbook that we have so far and it installed a single package it installed h-top now what we're going to do now is start expanding on this and make it better and better and better for the second example i'm going to show you how to install multiple packages so we're going to change this just a little bit here so what i'm going to do is move h top to a new line and then i will space over twice hyphen space to put it basically on its own line what i can now do is go down here i will add another package then again yet another package so tmux is an awesome utility i have actually done an entire video that teaches you how to use tmux and i highly recommend you learn because it's awesome but what i'm doing here is i am installing tmux as well as htop and i could have just created a brand new task and had each of these on their own task that isn't very efficient so what i decided to do was add all of the packages here one after another ansible allows you to do that all in one play that's what ansible calls tasks it refers to them as plays i also installed the vim knox package which is my favorite version of them it has additional scripting support i've also done some videos on vim if you want to check those out and learn how to use vim vim is awesome but that's not the point i wanted to show you guys how to install multiple packages it really doesn't matter which packages you go ahead and install these are just examples so what i'm also going to do is change the verbiage here to install packages because it's not just h-top anymore i'm installing more than just h-top and that's all i'm going to change for now so just like always we could run git status it sees the file has been modified let's go ahead and add it and then let's commit the change so i've added additional packages and now let's push the change up and it looks like everything is successful so far if i refresh this you can see here that indeed the changes are now in the github repository so now i can recall the same command that i've used earlier to pull the changes down and you can see here that there are no changes and that's because i already had the two packages vim knox and tmux installed so ansible isn't going to do anything if the requirements in the playbook have already been met now also you can and should ignore all these warnings right here ansible is used to using host names for configuration since we are using the local version it doesn't really use host hostname so these warnings can be ignored i really wish the ansible developers would get rid of these errors when you're using ansible pull i think it's redundant it causes confusion and makes some people think that they need to fix something there's nothing wrong it's completely normal but just keep in mind these warnings will come up and we can ignore those now let's bring up that file yet again [Music] and then we can work on the third example now this is where the desktop configuration idea starts to really take shape where we have the packages here that we're installing i mean technically there's nothing different about that than running this on a server there's nothing desktop specific about these packages now what we're going to do is actually get into things that are desktop specific and what i'm going to do is start with configuring the wallpaper yes i'm going to change the wallpaper on my laptop with ansible this is where it really starts to get awesome so first i will type name because we are setting up a new play i will name it copy wallpaper file in order to set the wallpaper we need to have the actual wallpaper that we want to set it to so then we will type copy we will set the source to wallpaper.png which will be found in the files directory we haven't created either of those yet don't worry about that yet we will set the destination then we will set the owner to root and the group will be rude as well now we will come back to this but for now let's go ahead and add the next play two spaces over we'll give the play a name then i'll just type set wallpaper because this is the play that will actually set the wallpaper and then for the module we will use deconf and for the key i'm going to actually paste this in because it's actually kind of long place that here you need to give it a value as well and then we will set up yet another play here i will name it set wallpaper position again we will use decomp add a key and a value as well so let me explain a little bit more what we're doing so in the beginning when we first started here we were using the package module to install a package ansible has a ton of modules that are available and when you want to do pretty much anything you'll be using a particular module to do that thing so here we are using the package module to install these three packages right here now here we are using the copy module what that does is it takes a file that exists wherever you have the source set right here and then it actually copies it over to the destination at the path that you type here so we are going to grab a wallpaper file that's in the files directory or at least will be shortly and we are going to copy that into user share backgrounds which is a directory that you should already have on your system if you're using a desktop variant and then we're going to copy this wallpaper.png to this name here ansiblehyphenwallpaper.png just to give it some distinction from all the other wallpapers that are in that directory it doesn't matter what you call it you could call it potato.p if you wanted to it really doesn't matter now this directory is owned by root so i have it set to root as the owner and the group now down here we are using the decomp module twice first we are using it to set the wallpaper the next we are using it to set the wallpaper position now gnome uses dcomf or technically g settings for all of the settings anytime you change a setting it's actually changing a key value pair in the background and deconf allows us to work with that the mate desktop also uses this same style so the key is here and then the value is here we have single quotes in here because this is a string and this here is also a string so what i'm going to do is save this file and i'm going to explain a little bit more about deconf i'm not going to go into too much detail but i think it's important that you know how to get the settings because it's hard to know what the key and values are supposed to be if you don't have something to look at or something to reference so what i'm going to do is go back to my home directory for now and then you will need the deconf cli package installed for this example which i already have installed actually and what that allows you to do is actually dump the settings to a file so you can actually see what all is available so if i run deconf dump i'll type a slash then i'll redirect that into settings.txt so here we have a dump of all of the settings i like the fact that the output is colorized here which isn't always the case but anyway we have the keys in green so for example right here we have org gnome control center and this is the key and last panel is background which basically when you go to the gnome settings the most recent thing that you visited or section within settings is known as last panel which is how it knows to send you back to that same section the next time you go to gnome settings so there's no value in configuring this one but if we scroll down further see there's quite a bit of things here now up here we have org gnome desktop background and this is one of the keys that we're messing with so org gnome desktop background is the path then we have the individual keys such as color shading picture options and so on which can give us an idea of what we can actually configure so now if i go back into the directory where our repository is bring up the playbook again here now this starts to make sense because now we have the key or gnome desktop background picture uri and then the value is set right here and when we run this it's going to set the value to that key accordingly as we have written in this file so i will save it so i'll just exit out and we can move on to the next step now if you remember we are referencing a wallpaper file in the files directory and we don't have a files directory and we don't have a wallpaper file either so we need to create both so i will type make dir and then files now we have the files directory there i run git status it shows that we have modified the local.yml which is true but it doesn't reference the fact that we've created a folder inside this repository and the reason why is because it doesn't even care if you create a folder unless there's something inside it now if i go into the files directory again we have nothing here yet we just need to add a wallpaper file it doesn't really matter what wallpaper file you use you need some kind of picture so i went ahead and downloaded a picture to my home directory off camera and we actually have that right here so what i'm going to do is just move it into this directory and there we have the wallpaper.png file if i go back getstatus actually shows that it cares about this directory now because there's something new inside that directory so i will type git add and then files i will commit that change but we still have another change that we need to commit as well because we have modified the local.yml file and we could type git diff local.yml for a reminder of the changes but since we've only made changes to one file we don't even need to give it the file name here so we just type git diff and of course it's showing all the new changes that we've made to that file so we could do git add and then the file name should be good enough and get push origin main it's going to take a little bit longer because the wallpaper file that i decided to use is a little bit on the bigger side here everything has been uploaded it took a while for some reason i'm not really sure why but now we can see that we have the files directory and we have the wallpaper file as well now before we run it there's one more thing that i need to show you and one more thing that we need to add to this file i can't believe i forgot to do this but you know we all make mistakes and that's okay because we will fix our mistakes the problem is that the decof module that we're using doesn't know which user to change these keys and values for so what we will type is become underscore user and then whatever our username is we will do the same thing here for the second one as well just like that save the file exit out and then let's go ahead and push the changes up and what we can do is run git commit dash am which means commit all files all files that have been changed we only have one anyway so we're basically doing a get add and a git commit in one shot simple enough push the changes and we are almost ready to run this but there's one more file that we will need to have installed so sudo apt install python 3 hyphen ps util i already have this on my system so i don't actually need to install it but you will definitely need it on your end in order for this to work otherwise the deconf module will not function now to cheat the system a little bit what you can actually do is add python3-psutil to the list of packages in the first play so that you don't even need to do this manually but i'll leave it up to you to do that anyway i will unmaximize my screen which will expose the desktop wallpaper that i have here keep your eye on the desktop wallpaper because i am going to go ahead and run the latest version of our playbook and you should see the wallpaper change before your eyes and here's the ansible pull command that we've been using so press enter keep your eyes on the wallpaper and let's see it happen now look at that how awesome was that not only do you get to configure your desktop with ansible you actually see the changes happen visually before your very eyes as it runs as you just saw the wallpaper changed when i ran the playbook so it looks like everything was a complete success now for the next example i'm going to add just one more thing to the ansible playbook that we have been modifying and building on and it's going to be a very simple change but i think it's a very important one because this is something that a lot of people want to do with automating configuration so at the very end of the file i'm going to add a brand new play and i'm going to copy a customized bashrc file so i'll use the copy module for this i'll set the source to files rc the destination will be my home directory dot rc the owner will be my user and the group will also be my user now i know that you might be thinking that this is a very simple example for this far into the video but honestly copying dot files as they're referred to the configuration files for your shell your editor and things like that is a very very common use case for configuration management on laptops and desktops so if anything i am giving you this example to inspire you so let's go ahead and add the bashrc that we are going to be using for this example inside the files directory i only have the wallpaper file currently so what i'm going to do is copy my current.bashrc file and i'm going to copy it to bashrc without the period because i do want to be able to see it let's go ahead and edit that file and if you don't already have a bash rc that you want to use you could just use the one that you have in your home directory it really doesn't matter but we want to do something different we want to know that this is the ansible version and not the one that came with the distribution so i'll just add a comment here i think that's good enough so now we have that file then in our playbook we've added a play to copy that bashrc to the appropriate place so the source is going to be the bashrc file in our files directory and again i took the period off because it just makes it easier to see inversion control it doesn't have to be hidden inside the repository itself and then the destination this is where bash expects to find a configuration file or at least if it does find one with this name it'll honor it and apply it it's going to copy it into the home j directory in my case on your end you just change j to whatever your username happens to be and then we're copying it in that home directory to dot bashrc let's go ahead and add everything so what we can do normally is we can run git commit dash am to commit all files and add a message but that won't add the new file that we've just added it won't add the bashrc because the file has to be in version control already as you can see here the bashrc file is listed as an untracked file so a simple shortcut we can do is git add and then a period and if we do a status it's basically added the brand new file as well as the file that we've modified to the commit regardless of whether or not that file was in version control before running git add and then period just make sure you don't have any files in there that you don't want to be a part of version control so let's commit the change then enter let's push the change should be good to go so what i will do is type head then i will direct that to the bashrc file in my home directory and you can see right here that that ansible message that i've added is not present of course not we didn't even run the ansible playbook yet but let's go ahead and do that now so there's the command i'll press enter so it's telling me that there has been one change and we can see that change right here so let's go ahead and check out the bashrc that i have local and now we have the line added via ansible so although that's a very simple example just think about what all you can do with this you can add all of your configuration files for example if you have a config file for tmux maybe your vim editor bash rc or whatever config files you want present in your home directory you can add them all as plays in the ansible config and have ansible manage that for you and then you can run the ansible pull command against any of your computers anytime you want to make sure that all of the configuration is exactly how you like it so let's recall our local.yml file yet again and go all the way down to the end and add some additional plays so now what i'm going to do is paste in all of the additional plays for our playbook in one shot then i'll go over each and explain exactly what the heck i'm doing now the most recent thing that we've done is we've added the config as you see right here to copy the bashrc file let's go ahead and get this centered here so what we're going to do now is add a new user to the system we're going to call that user velociraptor and i like that username because you know velociraptors can open doors and this is going to be a system account so what we'll use this for is to set up an automation in the background to run ansible automatically anytime we commit changes to the repository and we will use the velociraptor user as that background user for that particular purpose and since velociraptors can open doors i guess that makes sense now the next thing we're going to do in this section right here we're going to use the copy module which we already know takes a source file and then we give it a destination where we want that file to be so the source file is going to be this right here which i will create shortly and it's going to add that file here now the etsy sudoers.d directory if you create a configuration file for sudo for a particular user in that directory we want the velociraptor user to have sudo writes to make changes to the system in the background so by adding this file here we are actually configuring sudo for that purpose we want that file to be owned by root for security purposes we want the group to be root as well and these are the permissions that we need for that particular file next in this section right here we are going to add a cron job to the system and if you didn't already know a cron job is basically a scheduled task and you can schedule a task to run at a particular time so what we're going to do is we're going to set up a cron job we are going to give the cron job a name of ansible auto provision we want that crown job to run as the user velociraptor we want it to run every 10 minutes and the actual command that it will run every 10 minutes is this one right here it's going to run ansible pull we have dash u and then the repository url nothing new there but what is new is this option dash o what is that so what the o argument does is it sets up ansible pulled only run when there's actually been changes now if this was to run every 10 minutes and was going to run your entire ansible playbook you'll probably notice slowdown in the background for example if you're playing games on your laptop or desktop you might actually lose frames or your system might slow down while it runs in the background so we really don't want this to run unless there's actually been changes now the beauty of this is that when we run ansible pull manually on a new computer for the first time it's going to add the cron job for itself now think about that for a minute we want ansible to automate and run automatically we can set up a cron job to do that we don't want to set up the crown job manually we want that to be automatic so we make that a play so the first time this runs it adds a cron job that sets up all future runs of ansible which means from that point forward every time you commit a change to your repository every laptop or desktop that has already had ansible run on it at least one time will already have this cron job so you push your changes to the repository just one time and then all your laptops and desktops will all get those changes the next time that they check in and since we don't have a server that is handling this we don't have a central point that is trying to reach your laptops and desktops all your laptops and desktops are running this locally so they will only run this when they're online so you don't have to worry about you know maybe shut your desktop down or you have your laptop in your bag then basically this will not run unless they're on so you don't have to worry about wasted cpu cycles or wasted battery or anything like that and even if it does run again it's only going to run there's actually been changes so let's go ahead and save this file and exit out now the problem is we don't actually have that sudoers file that we are referencing in the playbook so if we were to commit this change right now and then run it it's going to fail so what we need to do is add that file right here so in an editor we will open up that file which of course doesn't exist just yet so first we type the username and all equals all in parentheses there no pass wd and then all also in caps as you see here so take a moment and make sure that you have this down exactly it's probably better to copy and paste it from the repository or the wiki article for this video than it is to type it manually if you make a mistake it's not going to work properly and since we're dealing with user permissions it's very important that we have this correct so i will save this file [Music] so let's go ahead and prepare a commit i think that's good enough for a message then we can push the changes up to github so if i check out my etsy password file we don't actually have a velociraptor user just yet so let's go ahead and run this and then we can see what exactly happens and there's our ansible pull command i'll press enter as you can see it has made some changes to our system so now [Music] we have the velociraptor user right here that's pretty cool then we can do sudo cron tab dash u velociraptor dash l to list the cron jobs for that user and we can actually see the koran job right here and then we actually have the name of our task right here it automatically put a comment right here to show us that ansible is managing this crown job and inside ansible the crown job has this name then of course we have this line right here and this is how we can actually make changes to this line in the future should we need to do that because as long as the name is the same ansible knows that it's this line the line of config that basically is right after this name that it actually needs to edit so we can make changes to this crown job as long as we keep the same name it's not going to duplicate anything so now at this point not only have we created our basic ansible configuration for our laptops and desktops we have also set up automation so that all future runs will happen automatically from this point forward so now what you can do on all of your laptops and desktops you can simply install ansible and then use ansible pull to run your configuration just one time then from that point on all future configurations that you push to the git repository will be then deployed to all of your machines in one shot and i'll leave it up to you to see what you can do with this ansible configuration and i hope that was helpful hopefully this video was helpful in getting you started on ansible and using it to manage your laptops and desktops ansible is a very powerful solution so if you are interested in ansible then i highly recommend that you check out my other ansible videos if you haven't already done so just check out the playlist now make sure that you subscribe to this channel because i have some awesome content coming very soon and let me know in the comments down below what kind of content you want to see so thanks for watching i really appreciate it and i'll see you in the next video you
Info
Channel: LearnLinuxTV
Views: 52,283
Rating: undefined out of 5
Keywords: Linux, Tutorial, Review, Howto, Guide, Distribution, os, open source, LearnLinuxTV, LearnLinux.tv, linode, ansible, ansible desktop, git, github, gitlab, ansible-pull, ansible pull, ansible playbook, ansible tutorial, configuration management, devops tutorial, what is ansible, ansible tutorial for beginners, ansible for beginners, learn ansible, ansible training, ansible tutorial for beginners linux, ansible tutorial advanced, ansible tutorial for devops, ansible playbook tutorial advanced
Id: gIDywsGBqf4
Channel Id: undefined
Length: 68min 27sec (4107 seconds)
Published: Fri Dec 11 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.