Learning Ansible 2.7(Third Edition)
上QQ阅读APP看书,第一时间看更新

Hello Ansible

As we have seen in the previous chapter, it is possible to use Ansible to automate simple tasks that you probably already perform daily.

Let's start by checking whether or not a remote machine is reachable; in other words, let's start by pinging a machine. The simplest way to do this is to run the following:

$ ansible all -i HOST, -m ping  

Here, HOST is an IP address, the Fully Qualified Domain Name (FQDN), or an alias of a machine where you have SSH access (you can use a Vagrant host, as we have seen in the previous chapter).

After  HOST, the comma is mandatory, because otherwise, it would not be seen as a list, but as a string.

In this case, we have performed it against a virtual machine on our system:

$ ansible all -i test01.fale.io, -m ping  

You should receive something like this as a result:

test01.fale.io | SUCCESS => {
"changed": false,
"ping": "pong"
}

 

Now, let's see what we did and why. Let's start from the Ansible help. To query it, we can use the following command:

$ ansible --help  

To make it easier to read, we have removed all the output related to options that we have not used:

Usage: ansible <host-pattern> [options]

Options:
-i INVENTORY, --inventory=INVENTORY, --inventory-file=INVENTORY
specify inventory host path or comma separated host
list. --inventory-file is deprecated
-m MODULE_NAME, --module-name=MODULE_NAME
module name to execute (default=command)

So, what we did was as follows:

  1. We invoked Ansible.
  2. We instructed Ansible to run on all hosts.
  3. We specified our inventory (also known as the list of the hosts).
  4. We specified the module we wanted to run (ping).

Now that we can ping the server, let's try echo hello ansible!, as shown in the following command:

$ ansible all -i test01.fale.io, -m shell -a '/bin/echo hello ansible!'  

You should receive something like this as a result:

test01.fale.io | CHANGED | rc=0 >>
hello ansible!

In this example, we used an additional option. Let's check the Ansible help to see what it does:

Usage: ansible <host-pattern> [options]
Options:
  -a MODULE_ARGS, --args=MODULE_ARGS
                        module arguments

As you may have guessed from the context and the name, the args options allow you to pass additional arguments to the module. Some modules (such as ping) do not support any arguments, while others (such as shell) will require arguments.