The inventory file

We do not need Ansible if we have no remote target to manage, correct? Everything starts from the fact that we need to manage some task on a remote host. The way we specify the potential remote target is with a host file. We can either have this file specifying the remote host either in /etc/ansible/hosts or use the -i option to specify the location of the file. Personally, I prefer to have this file in the same directory where my playbook is.

Technically, this file can be named anything as long as it is in a valid format. However, you can potentially save yourself and your colleagues some headaches in the future by following this convention.

The inventory file is a simple, plain text INI-style file that states your target. By default, this can either be a DNS FQDN or an IP address:

$ cat hosts
192.168.199.170

We can now use the command-line option to test Ansible and the host file:

$ ansible -i hosts 192.168.199.170 -m ping
192.168.199.170 | SUCCESS => {
"changed": false,
"ping": "pong"
}
By default, Ansible assumes that the same user executing the playbook exists on the remote host. For example, I am executing the playbook as echou locally; the same user also exist on my remote host. If you want to execute as a different user, you can use the -u option when executing, that is -u REMOTE_USER.

The previous line reads that we will use the host file as the inventory file, and execute the ping module on the host called 192.168.199.170. Ping (http://docs.ansible.com/ansible/ping_module.html) is a trivial test module that connects to the remote host, verifies a usable Python installation, and return the output pong upon success.

You may take a look at all the ever expanding module list (http://docs.ansible.com/ansible/list_of_all_modules.html) if you have any questions about the usage of existing modules that were shipped with Ansible.

If you get a host key error, it is typically because the host key is not in the known_hosts file, and is typically under ~/.ssh/known_hosts. You can either ssh to the host and answer yes at adding the host, or you can disable this checking on /etc/ansible/ansible.cfg or ~/.ansible.cfg with the following code:

[defaults]
host_key_checking = False

Now that we have a valid inventory file, we can make our first playbook.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.126.211