Inventory

Learn about an Ansible inventory file and its different types.

The inventory in Ansible refers to the collection of hosts or nodes that we can target with our automation. It can be organized using groups or patterns, allowing us to select specific hosts or groups for executing Ansible tasks. An inventory plays a crucial role in the automation of tasks.

The all keyword

The particular keyword all includes all the hosts of the inventory used. The only exception is localhost, which we need to specify. The all keyword is important and will be required in our Ansible usage.

The ansible-inventory tool

The ansible-inventory command-line tool is included in every Ansible installation, and its purpose is to show the current Ansible inventory information. It’s advantageous to verify the current status of our Ansible inventory. It accepts Ansible vault and Ansible inventory in INI, YAML, and JSON formats. We can get the complete list of parameters using the --help flag. A handy feature is to display the list of the hosts using the --list parameter or in a tree view using the --graph parameter.

Ansible list view

The list view of the ansible-inventory command is useful for displaying a JSON of the current inventory used by Ansible. The more complex our inventory becomes, the longer the list is.

Let’s suppose we have a simple INI inventory file similar to the following:

localhost ansible_connection=local

We can have the list view using the following command:

ansible-inventory -i inventory --list

Where:

  • The ansible-inventory command works with Ansible inventory.
  • The -i parameter specifies the source inventory file in the current directory.
  • The --list parameter requests the output in list view format.

The result of the execution is ...