The inventory file

We do not need Ansible if we have no remote target to manage, right? Everything starts with the fact that we need to perform some task on a remote host. In Ansible, the way we specify the potential remote target is with an inventory file. We can have this inventory file as the /etc/ansible/hosts file or use the -i option to specify the file during playbook runtime. Personally, I prefer to have this file in the same directory where my playbook is and use the -i option.

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

The inventory file is a simple, plaintext INI-style (https://en.wikipedia.org/wiki/INI_file) file that states your target. By default, the target 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 hosts 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 exists 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 in the example reads in the host file as the inventory file and executes 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 returns the output pong upon success.

You may take a look at the ever-expanding module list (http://docs.ansible.com/ansible/list_of_all_modules.html) if you have any questions about the use 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 when adding the host, or you can disable this by checking on /etc/ansible/ansible.cfg or ~/.ansible.cfg with the following code:

[defaults]
host_key_checking = False

Now that we have validated the inventory file and Ansible package, 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
18.116.45.207