Transcript
  • Servers and Tools

Ad-Hoc Commands

From the class:  Ansible

The simplest thing we can do with Ansible is to run commands on the remote machines by using the Ansible command line tool and specifying which hosts we want to run the command on. So in this case, let's run a command on app1.local, just to start off simply. And I need to provide a module option. This is going to be the Ansible module that is going to provide the functionality that we need.

So an example module is the ping module, which is pretty straightforward. It's just going to ping the remote host. When I press Enter, this is going to give us back the result of that. And so what it did is it went to app1.local, to that machine, and pinged it.

Now what makes Ansible really useful is the ability to do this on more than one machine at a time. So, for instance, instead of running this module on app1.local, let's run it on all of the machines under the app roll. So I can just type app here and now there's two machines that Ansible will deal with, app1 and app2.local.

There's a special keyword that Ansible provides to us called all. This will run the command on all the machines under all the roles. This time around, the ping command or the ping module runs on both the app servers and the database server and the load balancer server. This was a lot faster than having to ssh into each one of these machines independently to run this command.

There's hundreds of different modules that we can use. Let's try a different one. I'm going to run this on all the hosts and the module we'll use is the shell module. That let's us run arbitrary shell commands. We could provide another option to the Ansible command line tool called a, which I believe stands for arguments. And we can provide arguments to the module.

In this case, I'm going to provide the command that I want to run on the shell. We might just run the who am I command, for instance. And what will come back is that we're running as the vagrant user on all of these remote machines. Or we could do something a little bit more complex, like echo out the result of running some kind of command, like who am I.

One of the features of Ansible is that it can detect the state of the system and can just do the bare minimum number of things required to get us to a designated state. For example, I've got here a module-- a new module that we're using called apt. So this is going to be the apt. Package manager. And we're going to run that on the app role and the arguments are going to be the name of the package that we want to install and the state.

So this state here is present, currently, but it could be absent if we wanted to remove the package. So what this module is going to do is to make sure that the state of this system is such that this package is installed. And if it's already installed, we don't need to install it again.

I need to add one more option to this module because we need to run this with pseudo privileges so I can provide the become option. That allows Ansible to become a different user and by default, if we don't provide one, it just runs the command with pseudo privileges. When I press Enter, this is going to go to the different app machines or the app hosts and install the node.js package.

That process took a couple seconds to complete because it had to install node.js from scratch. But let's run the command one more time. This time, it completes almost immediately and that's because Ansible knows that the package state is already present. So it doesn't need to run the whole thing again.

Over in the Ansible documentation, you could learn more about ad-hoc commands by clicking the Introduction section and looking for the Introduction To Ad-Hoc Commands and read through that. You could also see a list of all the modules that are available. There's really a ton of them available for any different possible thing you can imagine. And I often find it a little bit difficult to find the module that I'm looking for here. So instead, you can just use Google search and type Ansible and then the module and normally it'll come up as the first thing in Google results.

Let's just take a look at the shell command that we used. That's under the Commands Module and then we click on shell. And for each one of these, Ansible will provide documentation that gives you the various options that are available for that particular module. Here's some examples. And then down below, it'll give you a couple of examples of how to use that, how to use the module.