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

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.

主站蜘蛛池模板: 西安市| 温州市| 汽车| 德清县| 崇明县| 新民市| 崇信县| 桃源县| 巨野县| 竹北市| 屏山县| 吴江市| 云龙县| 乐平市| 全州县| 丰县| 商丘市| 拉孜县| 盖州市| 新宁县| 青海省| 哈尔滨市| 邛崃市| 海原县| 盱眙县| 连江县| 株洲县| 佛冈县| 腾冲县| 延边| 海盐县| 鹤岗市| 全椒县| 清丰县| 洪江市| 上饶市| 义马市| 白朗县| 桂东县| 获嘉县| 托克托县|