Skip to main content

Defining the Inventory

Ansible Overview.png



Defining the Inventory

The inventory is everything when it comes to Ansible. Your inventory is your set of hosts to be managed by Ansible. It could be a handful of hosts to a few hundred hosts.

  • The default location for global Ansible configurations is in /etc/ansible/
    • The default location for Ansible inventory is /etc/ansible/hosts
  • You can have user-specific Ansible configurations in ~/.ansible.cfg
    • Based on the Ansible path preference, any settings in ~/.ansible.cfg will take precedence

    • You can set a custom default inventory by setting this line in ~/.ansible.cfg :

      inventory = ~/path/to/inventory-file



Ansible Declarative Syntax

Ansible predominantly uses YAML (Yet Another Markup Language) to design anything from playbooks to inventory. YAML is very easy to read and write and extremely versatile to handle a complex set of data types. To be proficient at Ansible, you must be proficient with YAML.

INI Format

You can use the INI format style when writing your inventory if preferred. Here is an example of an INI format inventory. You'll see from the top to the bottom, it defines:

  • Aliases (easy to assign short names to hosts)
  • Groups (putting hosts into groups)
  • Groups of groups (assigning groups into additional groups)
# Aliases
# Value on the left is the alias to refer to the value on the right
web1 webserver1.domain.tld
web2 webserver2.domain.tld
web3 10.100.100.10

# Groups
[windows]
web1

[linux]
web2
web3

[nova]
web1
web2

[berlin]
web3

# Groups of groups
# Group name: us-east-1
[us-east-1:children]
nova

# Group name: eu-central-1
[eu-central-1:children]
berlin

YAML

Here is the INI format inventory in YAML format. You'll notice the --- at the top of the YAML file. This is the proper way to indicate the start of a YAML document.

---
all:
  # Define the aliases here
  hosts:
    web1:
      ansible_host: webserver1.domain.tld
    web2:
      ansible_host: webserver2.domain.tld
    web3:
      ansible_host: 10.100.100.10

# Create a group called windows and add web1
windows:
  hosts:
    web1:

# Create a group called linux and add web2,web3
linux:
  hosts:
    web2:
    web3:

nova:
  hosts:
    web1:
    web2:

berlin:
  hosts:
    web3:

# Create a group called us_east_1 and add the child group nova
us_east_1:
  children:
    nova:

eu_central_1:
  children:
    berlin:



Testing Your Inventory File

# Test the Ansible inventory
# using the default inventory file 
# specified in your Ansible configuration file
ansible-inventory --list

# Test the Ansible inventory
# using a custom inventory file path
ansible-inventory --list -i /path/to/inventory-file

This command will list out your inventory in JSON format and let you know if it found any issues with your inventory file.