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

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.

主站蜘蛛池模板: 东辽县| 南投市| 灯塔市| 景谷| 中江县| 穆棱市| 汶川县| 榆树市| 罗定市| 姚安县| 正镶白旗| 资源县| 裕民县| 新民市| 高青县| 盐源县| 朝阳区| 辽阳市| 承德市| 河曲县| 陵川县| 永丰县| 张家港市| 宁津县| 胶州市| 巨野县| 沁水县| 澜沧| 湖南省| 防城港市| 德格县| 玉林市| 将乐县| 高雄市| 宝鸡市| 卢龙县| 保德县| 丽江市| 浙江省| 丹凤县| 齐河县|