Command Modules and Idempotence

Gourav Shah
A free video tutorial from Gourav Shah
Premium Instructor| 35k+ students| Devops Trainer and Author
4.1 instructor rating • 14 courses • 41,367 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 The previous lesson we discussed about models being put into place for almost all except for special type of Marty you know if you look at the models there are specialized models for let's say copying file installing packages managing let's have lands on Cisco's device or repeating something made on the cloud. So there are specific models for most of these jobs. If you have a specific model that is typically an item brought in and that the list of models that you see here except for some times that you may not find the exact model to do your job. For example if you want to install something from source or call an API that you have for your own application or some customized work in which case you can use some special type of called us come on murder then come on molders allow you to run some commands on us crib directly on a managed node and then there are different types of modules including rolled her eyes actually bypasses the Martu subsystem. And this is useful when you do not have bitumen installed on the resulting node you know because it will require Spuyten. The second type of model is. And the third is show these are very similar and these allow you to run some command that actually using the Batan model subsystem that well has and then another one which is quite useful if you want to copy the script and run it. You can do it in one go instead of using two different model that the script it copies and runs your and executes descript on the resulting. Or you can also use expect. So if you want to use it but you know sometimes you have to expect or expect that it is based on say say input and then runs it. Now come on shell which are very similar except for a few things which you know a command does not have a shell and does not or cannot access it like you are operators such as greater than less than symbols and in semi-colons which shell allows you to do so shell in walks or runs search and it can allow you to run anything that you know that you require from the environment or these kind of operators so you in a sense command is restrictive. It is better and secure. So you would use shell selectively and only when you need it. Lets go in exactly what some commands using these models and learn how this really works. So first come on that we are going to run it on you know on the host about an app servers and we are responsible with the host body and that is let's say Prod.. In this case and we're going to use up you know the model cardless come on and with option that's free that's you can basically invoke a command on that you know on the resulting Norcia right on the host Bardens. Now if you want to use bifold spice symbol you know one way to do that is you know you can bite it off better you get the output. Now you're doing the piping on the current host that's a control node right now if you want to get the same output. You may want to use let's say minus the ice. Because this is capital here and this is happening on the control not not inside the you know inside the Target actually. And if you want to use the pipe and you want to run it on domination towards the control nodes the command won't allow you to do that. So if you run it with come on it's going to add an hour. You need to use shell. So that's what we were talking about. You need environment variables to use if you want to use pipes or operators like greater than less than that operators and so on. You'll have to use the shell model instead of the command. So this is very real change to show and not on the same command which will work on the nodes as well. So this is actually doing the grouping on the Nords not on the control load here. Right so if you want to use those up here does you show otherwise just use Come on if you want to see the difference between Raun show. Let's have a look at it. We lose minus v v v v option and that's what those option or the extra option. And if you with withdraw you'll notice that it's not even doing any byte and novice Martu subsystem it just establishes that it always has such connections and Ransey Come on inside the Target node Benteke. There's other meat bite on the resulting node if you want to use any other thing. Let's say a shell or come on you go through or you go either the audio subsystem in which case you are going to need the you know byte and being installed right. And you see a lot more output when you show and it's you know invoking is establishing a such connection it's copying over some script and then it's going to start invoking it inside that target Noraid. And that's the reason why you see so many lines of output is that all of it should do. Probably a few lines and you know that sort of bypassing the byte and a modern subsystem that's flat rate and that's the difference between using a raw and using a shell. So it's something that is the only one your device does not or or not installed by and but has a search and you want to run some commands on that device is when you would use shell. Now let's talk about the importance radio because show the these combined models are not burdened by design. I'll show you what I mean by actually executing one come on here. So in this case we're going to create a directory so we're going to run Diyar and we are going to created it actually I am the one using the command module. And let's see what happens when you run it for the first time and all the consequent is right so be it using command module with the argument minus C and inside that we are going to run a command dramatically to create a directory that is M. KTAR and slash slash diad one. Let's see what happens when you execute that command here on the observer's first time. It would show you succeeded. It has created the directory second time though. Fields. That's because the file already exists so we are running in looking a command. That command is not important. And that's the reason why this failed so bad models don't know what you trying to do. So it is not an important by design but it has an option that it could use so that the error that file existed. And you know that's the reason I'm giddy. Come on. Now if you want to make an item board then that is one option that the command model provides. And this creates option which is very useful and creates a option not a bottom feeder to your code. And if you click on any of this come on modules you'll see that option there. That's the creative option. Right. And this is the only way to control that importance in case of ansible if you look at BERTOUCH have there are few more options that it provides using not only of analysts and so on. Right. In guess off but there's only one option creates. So in this case the Create will check whether the file is present. If it is President it will skip. If not it will create it. Right so let's add that option and see how this changes the output or the behavior of ansible. Date. So we're going to add that option that the directory that it is creating. So we're letting on people know that this command creates this file or directory it could be either off Doc and ansible is going to check now. And Skip that's the use of creates and that's how you control the item Portlands of you know that simple tools or the common use by using that particular option if you're you know if you're if you're not creating a file you can still create a flag file and check for it. And that's the way you control command and item portions of those command modules.