官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 中江县| 丰台区| 望江县| 兴山县| 孝义市| 丰都县| 海城市| 中江县| 高州市| 水富县| 兰溪市| 黑水县| 陈巴尔虎旗| 吉林市| 策勒县| 都昌县| 玉林市| 绵竹市| 宜都市| 宜章县| 原阳县| 湘潭市| 连平县| 冀州市| 崇文区| 聂拉木县| 潼关县| 金川县| 南澳县| 公主岭市| 开化县| 顺义区| 永州市| 延庆县| 江永县| 白沙| 措勤县| 乐昌市| 新田县| 武强县| 佛山市|