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

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.

主站蜘蛛池模板: 陕西省| 恩施市| 全州县| 绥化市| 泰兴市| 繁峙县| 北辰区| 阳东县| 密云县| 邵东县| 延川县| 三江| 开江县| 娱乐| 陵川县| 剑川县| 和平区| 嘉黎县| 万源市| 稻城县| 宁强县| 长白| 镇平县| 南江县| 秦皇岛市| 兴隆县| 墨脱县| 泽州县| 花垣县| 紫云| 望江县| 礼泉县| 恩平市| 滨州市| 美姑县| 正定县| 萨迦县| 正定县| 房山区| 根河市| 札达县|