The Ansible Inventory File and Project Directory

Ansible Inventory

Ansible can manage only the servers it explicitly knows about. For this, we use an inventory file. The inventory file in Ansible is used to specify the hosts on which you want to run your Ansible ad-hoc commands (single task) or playbooks (a set of tasks). It basically allows you to define managed hosts, which are defined by their FQDN or IP that Ansible will use to identify them.

An inventory file can be static, where hosts are defined manually, or dynamic, where you integrate Ansible to any other inventory source in your environment. These sources can be Cloud providers like AWS, Azure, Google Cloud Platform or any other supported platform. In this post, we will have an overview of static inventory files.

It’s worth noting that the inventory file is not the hosts file that is used by the OS resolver to match hostnames to IP addresses. It is a file which is specific to Ansible to define the managed hosts.

After installation, Ansible provides an inventory file sample in /etc/ansible/hosts. This file can be used or a new one can be created in a custom path as per the need.

This file has an INI file syntax and looks like this, where you define the hosts group name between square brackets then you specify the actual hosts by their names or IP addresses.

By default, while running an Ansible ad-hoc command or playbook, Ansible will use the default inventory file located in /etc/ansible/hosts. If you didn’t specify any group there, you will have the warning below, where Ansible is trying to tell you that the group all does not exist in this file.

You will either to create the groups you want to use in the default file, or create your own custom file, and the best practice is to put it in the same project directory as the Ansible playbooks you want to run.

To indicate your custom inventory file instead of the default one while running an Asible ad-hoc command, you use the -i option. In the two examples below, the inventory file used is called myInventoryFile.

Organizing Deployments with Project Directories

For every single set of tasks that you would run in Ansible, it is common to create a project directory for that. This will make it easy to distinguish between different tasks.

If for example, we have to deal with package installation, we would create a directory called install and put our inventory file there.

We will add our hosts in the inventory file then save it. We have a group named all containing two servers named server1 and server2. Because one server can be part of multiple group, server1 will also be a member of webservers group, and server2 will be member of appservers group.

The inventory file I am using look like this

The –list-hosts argument will list the hosts in the inventory file that belongs to all group

To test the connectivity of the appservers group with the ping module.

Another test to check the uptime of the servers in the all group. The Ansible inventory file file this time is called hosts.

It is also possible to run a command against a single host in the inventory. You just need to specify the server’s hostname as defined in the inventory file.

We have seen in this post what is the use of the Ansible inventory file, its initial default location after Ansible is installed and how to create a custom inventory file for our own deployments. We’ve also had a review on how to stay organized with the Ansible project directories. In the next post, we will go through the use of the Ansible configuration file and how this file is used to simplify hosts management and Ansible administrative tasks.

Thanks for reading. Stay tuned!

Find this post interesting. Share it!

1 Comment

  1. Pingback: Ansible for the Impatient Beginners – vAdmin-Land

Leave a Comment

Your email address will not be published. Required fields are marked *