Getting started with Ansible 04 - Running ad-hoc Commands

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right so we are moving right along in our ansible series and in this video we're going to actually install ansible and use it to run some commands against our servers this is where it all begins this is where we actually get some hands-on with ansible itself enough with all the foundational concepts let's actually dive into installing ansible and running commands [Music] all right so here we are on my laptop i'm going to show you some ansible commands we're not actually going to be writing playbooks in this video we'll get to that but we do need to get ansible installed and we do need to understand how it interacts with our servers now there are multiple ways of installing ansible you can actually install a repository from ansible itself to get the latest and greatest you can even use pip with python to install it but for now we're going to go ahead and just use the version of ansible that comes with the ubuntu repositories so to install ansible it's pretty easy we do sudo apt update to make sure that our index is up to date i'll put in my super secret password here and that's done we'll ignore the fact that i haven't done my updates yet so now that the index is up to date we can run sudo apt install ansible press enter and it's going to install quite a few dependencies here but that's okay i'll just press enter to accept that and it's not that big of a package so it should install fairly quickly so now we have ansible installed and to use it more effectively we need an inventory file so let's go ahead and create that now if i list my storage i still have the ansible tutorial get repository here and you should have one too whatever you decided to call yours you should have a directory for that so we'll go into that directory and right now we only have the readme file so what i'm going to do is create the inventory file which is going to contain the ip addresses for each of the hosts that we want to manage with ansible so i'll just do nano and then inventory this file won't exist because we only have the readme file now we have this empty file so what can we do to actually create an inventory well actually all we need to do is type the ip addresses of the servers that we want to manage here so i'll go ahead and type those in and those are mine now if you have dns names for your server so if you've registered a domain name or you have a dns server where it's able to recognize the host names you can actually type the host names of the servers as well instead of the ip addresses now dhcp ip addresses as you probably know they can change so you want to be careful with this but for the purposes of this introduction to ansible i think ip addresses are fine again if you have fully qualified domain names or any kind of dns names that is recognized on your network you can go ahead and use those instead but anyway i'll do ctrl o and enter to save the file and then control x to exit out so now we have that inventory file as you can see right here but if i do get status we can see that on track files we have inventory so this file inventory right here the one we just created is not in version control it's not understood by git this is a foreign object that's essentially what that means so what we want to do is go ahead and get add and then that file we want to add that file to version control press enter and now we'll see how git status changes so it says changes to be committed it shows inventory as a new file so now this file is going to be added to version control and it's going to be added to the next commit as i mentioned in the previous video a commit is basically like bundling all of your changes into a bucket to send up to the server in our case you know github is the get server we're using and now that we've added it we need to commit it so we could do this git commit dash m first version of the inventory file added three hosts and it really doesn't matter what your message says we just need to summarize the changes that we've made here so far i'll press enter and then we can go ahead and push those changes up to github and that's the command that will do that get push origin master and if i go back to the browser we have the ansible tutorial repository right here i'll go ahead and refresh it and now we have the inventory file that we just pushed up to github i click on that we have the three ip addresses here of the servers which will of course be different on your end but these are the ip addresses for my servers that i've created in virtualbox but you get the idea we were able to create an inventory file and inside there we just added the ip addresses of those hosts couldn't get any simpler than that so now let's go ahead and actually run an ansible command to make sure that everything is working so we're going to run this command right here i'll go ahead and type it out ansible all dash dash key dash file and then i'm typing the path to the ssh directory if you didn't already know that tilde right here is short for the home directory or the home directory of the user i'm logged in as and we named the key ansible dash i and we need to give it the file name of an inventory file which i simply named inventory which is in the current working directory again i'm inside the actual directory for the repository that we cloned locally dash m for module and the module i want to run is ping now you might think that this is simply going to ping all of the hosts inside the inventory not exactly it's actually going to make a connection to each of the hosts inside the inventory so it's a little bit more than just a ping but what this is going to do is allow us to make sure that everything is working i'll press enter and we have three successes so what this means is that ansible was able to connect to each of the servers and establish a connection that's exactly what it did so according to this everything is going very well because we were able to make a connection to each of the three servers so to explain this command a little bit more we're going to run ansible against all we're going to give it the key file that we want ansible to use dash i we need to give it an inventory file i named the inventory file well inventory dash m allows you to run a module there's all kinds of modules out there and the one i chose to do first is ping which again is not a simple ping test if you're familiar with like the icmp ping this is not that it's actually connecting via ssh it's actually making a real connection and it was able to do that so we are able to communicate to those three servers via ansible this video is sponsored by kernel care and cloud linux i'll get back to the video in just a moment but before i do i wanted to let you guys know about a really awesome service they are making available to users of centos 6. if you didn't already know centos 6 will reach end of life this november of 2020 and when that happens you will no longer receive security updates and that's a big problem if you continue to use centos 6 beyond that date your servers will be increasingly more vulnerable to newly discovered security vulnerabilities so it's definitely recommended that you update to a newer and supported version of centos if you can it's definitely challenging for an organization to migrate to a newer version of centos but at the same time they can't live without ongoing support either now you can make this problem disappear centos 6 extended lifecycle support from cloud linux allows you to continue to use centos 6 beyond the end of life date and actually remain secure and patched because you will continue to receive patches and security updates as you would normally but this time from cloud linux repositories because they are going to keep this going for you with their new service for a limited time you can get a specially discounted bundle kernel care live patching and extended life cycle support for centos 6 together with a 30 discount on centos 6 extended life cycle support and a 15 discount on kernel care and as for kernel care it's a live patching system that automatically applies security patches to linux kernels and shared libraries with no reboots it's used on over 500 000 servers and has been used to patch servers running for over six years for such companies as dell zoom liquid web and other enterprises it works with all the major linux distributions and interoperates flawlessly with common vulnerability scanners configuration management tools and patch management solutions as well check out the url that is on the screen right now or give the link a click that's in the description below this video to learn more and get your quote now let's get back to the video but you know i don't know about you but i'm a little bit lazy i guess because well i guess that's why we're automating things in the first place right because we're lazy admins and we don't want to do a hundred different steps we want to basically automate everything and actually that's not being lazy that's being effective especially if you have 500 servers we want to basically automate as much as we can but i don't want to type so many things when i go ahead and run an ansible command let's go ahead and shorten that command down a little bit so we can make this a little bit easier to use so what we're going to do is create an ansible config file so we'll do nano and again make sure you are inside the repository directory and we're going to create ansible.cfg just like that i'll press enter and we have another empty file here now this ansible config file is read by ansible whenever you run it so what we want to do is set some defaults so the stanza i have here in square brackets is defaults i'll press enter to go to the next line and we'll do inventory space equals space and then the name of the inventory file we want to use which is simply inventory on the next line we're going to add private key file space equal space then basically the tilde again that's shorthand for home slash dot ssh and then ansible just like that so we're basically setting some defaults here so i'm going to save the file ctrl o and then enter and then control x and we have that file here now if we look at the etsy directory we also have an ansible directory inside there as well and we also have an ansible.cfg file we're going to ignore that we created one local and that's going to override the one that's in etsy that's in our local working directory here ansible.cfg same name but this one is going to take priority now let's see if we can shorten that previous command a bit again it was this one right here that's kind of on the long side so let's see if we can actually shorten this down i'm going to remove actually most of this and i'm going to shorten it basically down to that ansible all dash m ping i'm not even telling it which key to use or which inventory file to use because we put that in the config file so it actually should look in the config file to find the key and the inventory file path and there we go we did the same thing and we were able to shorten that down quite a bit again down here at the bottom we see the original command and at the top the new version ansible all dash m ping the reason that works is because we declared the path to the key file here and also the file name of the inventory file as well so we don't actually have to type those things out anymore because the ansible config file contains those items so we should be all set so i'm going to go ahead and give you guys another command so ansible all then dash dash list dash hosts and then enter and it's basically going to show you a list of your hosts now it's not that useful but it could be useful just something to keep in your toolbox here it's going to give you a list of all your hosts and it shows that have three it shows the three ip addresses so that could be useful but perhaps more useful than that is this one so we're doing ansible all we want to run it on everything the module that we want to run is gather underscore fax and then enter now there's going to be quite a bit of output here isn't there look at all this it just keeps going and going and going and going when ansible connects to a server it's basically pulling a list of information about that server including the processor that it has the operating system that it's running all of your environment variables and all kinds of things here you can see that there's quite a bit of information and it did that for each of the three servers and another thing that we can do since that was you know very long is we can limit it down to a single host and this is good to know the dash dash limit that's useful if you want to run ansible but target just one of your hosts you can use dash dash limit for that then i'll give it an ip address and then enter it's going to do basically the same thing as last time but only against the particular server that i identified with that command with the limit and again there's a lot of information here so you might be wondering why the heck would i ever want to do this well first of all you could want to grab some information specific to the host and you can see that pretty much everything you want to know about that server ansible is pulling that information down but you might also use it for troubleshooting as well because if something is failing like for example if you are targeting a specific processor or a specific distribution you can basically compare how you have variables set up in your playbook against how the variable is shown on the server that's getting way ahead of ourselves but i think that it is something that could be useful if you want to pull information about your servers so go ahead and give it a try so now you know how to run commands against your servers with ansible and we want to make sure that we are fully synchronized with github here so let's run git status and see what has changed since we started working or since the last commit and as you can probably guess the ansible.cfg file that's a new file so i'm going to go ahead and add that in let's go ahead and run status get status there it is it's going to add ansible.cfg to version control then we can run git commit first version of ansible.cfg or whatever you want to say for your commit message it doesn't really matter i won't judge you enter and get push origin master just like that so now everything should be synchronized with github let's go ahead and check that here we are again with our repository i'll click here to go back to the root of the repository and sure enough we have the ansible config file and it even has color syntax right here which is pretty cool so now you can get a feel for basically using ansible with version control and now you are even able to use ansible to execute commands against your actual servers so so we're actually quite a bit productive already and this is only the fourth video so there you go at this point we are able to run ad hoc commands against our servers we can run one command and have it executed on each that is pretty cool now to be fair we are not automating anything yet and we haven't used ansible to actually make changes to our servers we're going to go ahead and take a look at that in the next video and then in the video after that we're going to take a look at creating our own playbooks which will actually be our beginning step into the world of automation which is where the magic really happens so i'm hoping you're loving ansible so far and i hope that you are loving this series as well and if you do please click that like button because that lets youtube know that you want to see more content just like this and go ahead and check out the next video in this series because that is already up on my channel and if you haven't already done so definitely subscribe to be the first to see alerts for new content as soon as it comes out on my channel thanks so much for checking this out and i'll see you in the next video you
Info
Channel: Learn Linux TV
Views: 126,536
Rating: undefined out of 5
Keywords: LearnLinux, Linux, Tutorial, Review, Howto, os, open source, ansible, commands, ansible commands, modules, ansible modules, ansible guide, ansible course, ansible tutorial, ansible full course, devops, cloud, infrastructure as code, vps, digital ocean, linode, automation, scripting, ansible for beginners, automate, devops tools, gather facts, ansible overview, beginners guide ansible, Learn Ansible, ansible training videos, ansible training, ansible tutorial for beginners, ansible ad hoc
Id: 4REljLsOnXk
Channel Id: undefined
Length: 18min 29sec (1109 seconds)
Published: Fri Jul 31 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.