Testing the inventory

Once the inventory is written, it is possible to test whether all of the hosts mentioned are accessible from Ansible. To do this, we can execute the following command:

ansible -i inventory all -u demobook -m ping

The -i argument is the path of the inventory file, the -u argument corresponds to the remote user name used to connect to the remote machine, and -m is the command to execute—here, we execute the ping command on all machines in the inventory.

The following screenshot shows the execution of this command:

We can also test connectivity only on hosts of a particular group by calling this command with the group name instead of all. For example, in our case, we will execute this command:

ansible -i inventory webserver -u demobook -m ping

The following screenshot shows the execution of this command:

In this section, we learned that Ansible needs an inventory file to configure hosts. Then, we created and tested our first inventory file and finally, we saw how to go further in the configuration of this file.

In the next section, we will see how to set up and write the configuration action code in the Ansible playbooks.