Host Patterns and Ad Hoc Commands in Ansible

Gourav Shah
A free video tutorial from Gourav Shah
Premium Instructor| 35k+ students| Devops Trainer and Author
4.1 instructor rating • 14 courses • 41,220 students

Learn more from the full course

Ultimate Ansible Bootcamp by School of Devops®

Unofficial Guide to EX407 Redhat Certified Specialist in Ansible Automation

07:32:45 of on-demand video • Updated July 2020

  • What is ansible, why its needed and how it works
  • How to configure ansible and setup static/dynamic inventories
  • Ad hoc server management, running jobs in parallel
  • Managing systems entities using ansible Modules
  • Writing Playbooks to define the state of infrastructure as a code
  • Creating modular, reusable code with Roles
  • Creating dynmic code with vars and templates
  • Using Ansible Galaxy
  • Encrypting sensitive data with Vault
  • Service Discovery and managing Multiple Environments
  • Application Deployment Strategies, Rolling Updates
English [Auto] This listen we're going to start talking about the host Batten's. We've already used one in our previous lesson where we used ansible utility at all as a host back and that all is like a wild card. Host baktun which connects to all the host in the inventory. However there are many other factors that you could use. This is the wild card they could also use star all the same. You can also use x y style star. You can group together two options or two patterns using the Karlan that's how you grouped together. You can add subscript very you can find the ranges or you can defend a specific option from a particular group or a host can do an exclusion. Let's say you want to exclude one host or one back and you can use the colon and the pattern there. You can do that the intersection using and that's ampersand actually not a question mark. And you can also use a regular expression Very can define your own back and then begin to look at some of these examples by you know by actually using those right. You can also an other way to limit the execution to a certain specific so I'm forced to use a hyphen hyphen limit option. Now let's look at it one by one. We start with the white Garvie already used all as a baton. We can also use asterisk there or you can define let's say AppStore and then find that right. So we get to hear the same model but with different patents and Mostar you need to call it that's very important. You can either use a single court or a court order combination of both at the same time. I'm going to use all. Here are. Let's see. You know inside the double star that's same as using all Actually it's all same right. You can also specify let's say apse star which will connect to all the application so anything that touch with app and that follows with any Bacchantes is what we are using there. You can also use hyphen if in limit to let's say all but we're going to limit the load balancer hosts only so you can use half and half and limit and pro-white that limiting Bakan as well. Now next thing is we're going to group together the packet. So this is a grouping of patents rather and you can define let's sit back in one and Collen back and two. So let's see. App one an app too is going to pick. So there's many defined the back and it could be host or group awfullest as well. So this is going to kick in anything in app one have to and that's the host names actually and anything in the balance of group that's those other Pakhtun that can be sort of used here. Now if you want to use group let's say prod. Now this does not find anything for that group because we have not defined that group yet as part of our Inventar let's define that. And our production group or Prod. contains all the holes that we would define which are part of Albion NDB. So we create a group of groups by using Kallen children. And that's where we define the group. So app Albi NDB are all part of Prod.. So when I use a baton godless plot that's how we define all the host in a practical environment it calls all the host in that. So it's called everything except for local laws because local news is not part of that process. Now let's look at the you know look at look at there are few more patents. Let's say you want to exclude something so proud. Colin you know it has to be proft Carlin. And then exclusion D.B and it has to be in single court that's very very important. So when you use these exclusion marks sort of any use and it has to be in sync old Soviet excluding D-B from all the production hosts so anything that part of that is part of beby it's getting excluded here. And we're excluding Let's see another batton let's L.-B. So it's going to pick only the servers that's effective here. Do you subscript civic and use it for a group of folks from a specific at a rate let's say we have all observers and we just sort of find out specific who are not on the production server but we want to find out you know zero deedle and to zero call. Maybe so this will give me first three holes. That's what you see there. You can also pick a range of course let's say to own words six exclude the first two and that it's going to it's always going to give me anything which starts from the second note on straight and it can do it for any group to actually. So this shows me anything stuck. Starting with the second note on looks that's that's the subscript you go to the regular expression you can define a back so we can say that anything that starts with an already B and then follows with any back and let's say any Apple 1 2 at 3 so it should match to be just for Stockland. So the string call has that already beat and then anything that follows can also be matched. So this is going to match everything in the stocks with that money. So we have three notes which match match that local expression. And now we will all looked at hyphenation Lamech with high limit as well. You can define multiple buttons so you can have this grouping of patterns there you can you know define that expression when you use it as well. Right. And that's about the host Batten's that we've been looking at. We also looked at how to use the hyphenation limit. Now there's another there are many ways you can limit the the way you run things so if you were to run as a one on a whole one at a time what happens right now is it runs on all the host at the same time in parallel so if you want to run it on one who started name otherwise you can define the fork to be one. So if you use that option hyphen if one. I know it's simpler if used minus. That's the different that you see here. If it was my mind is if when you see one who is tricked I'm displaying the output to. So that's how you can control if you want to run on little one whole start to start again. That's how you sort of control that and that's about the host patterns. Now we're going to look at how do you run ad hoc commands using ansible sometimes you want to use ansible maybe just as a pallial executed but you want to connect them into servers and transfer market rate and that's what we want to achieve here. And if you want to do that we're going to use and to boost the backend and then we're going to use mine to say no more do nothing here despite what John said. Rockman And in this case we're ending a hostname come on on all the holes which match APA and B-B that's almost Baten. Any come on for that matter. Just like executing identically on a show it's just running it in on a bunch of servers. That's exactly what is happening here. So then you want to run or use ansible for Iraq so management runs America on its checksums Tartous on a bunch of servers. This can be a useful utilization of ansible it's flat rate and. And I mean obviously we're going to look at playbook and start creating infrastructure as a code but this can be used for many a times to perform some adhoc tasks are not great. So we learned about some of these commands and that you could get it here. And when you run that the start actually is actually. So it was connected to what is the output outcome and written code as well as the actual output of the command. That's important because when we start using playbooks and models that output will be different. Great let's say you want to install a package we can that actually do that using YEM install as well. So let's say you want to connect with certain servers. Let's say you want to connect with a pack and that is the same pattern that Amandi be servers and we want to install them. So we use yum install minus Y them that actually on those servers. Now here it does not install it because the permission was denied because you have to run it as. So we use that minus B minus 4 become option become option is equal and Basutos actually and that allows us to proceed and install the package that has them. Now if you don't need again since this command itself the command is important. It went through fine. But if you have another command which is not important let's say we're trying to use our user Let's see what happens the first time it creates the user. Second time it feels because most of the shell commands unlike him are not important. And if you want that are important and if you want an ability to run and simple over and over again we need to start using models. And that's the true way of using ansible and that's what we're going to look at in the next lesson. We start talking about modules.