Master Ansible YAML Inventory with Real-World Examples

Ansible Inventory Guide

Managing your infrastructure at scale? Ansible inventories define which machines you’ll manage and how. Whether you’re working with a handful of servers or orchestrating thousands, organizing your Ansible inventory efficiently is crucial.

In this guide, you’ll learn how to manage hosts and groups using YAML-based and INI-style inventories, define host variables, and even use dynamic inventories with cloud providers like AWS.

What is the Ansible YAML Inventory?

An Ansible inventory is a  YAML file that contains a list of the hosts and groups of hosts that Ansible interacts with. It can be static (manually defined) or dynamic (auto-generated from cloud providers or other sources).

  • Static Inventory: Predefined hosts and groups.
  • Dynamic Inventory: Hosts are fetched dynamically (e.g., from AWS or Azure).

📌 Default location: /etc/ansible/hosts
📌 Custom location: Use with -i <path> flag

INI vs YAML Inventory Format

INI Format Example

[webservers]
web1 ansible_host=192.168.1.10 ansible_user=admin
web2 ansible_host=192.168.1.11 ansible_user=admin

YAML Format Example

all:
  children:
    webservers:
      hosts:
        web1:
          ansible_host: 192.168.1.10
          ansible_user: admin
        web2:
          ansible_host: 192.168.1.11
          ansible_user: admin

Basic Structure of Ansible Inventory

Ansible inventory files are written in a simple YAML format where hosts are grouped together. The basic structure defines groups and hosts under them.

[webservers]
web1.example.com
web2.example.com

[databases]
db1.example.com

In this example:

  • [webservers] and [databases] are group names.
  • web1.example.com and web2.example.com are part of the webservers group.
  • db1.example.com belongs to the databases group.

Managing Hosts in Ansible Inventory

To manage hosts effectively, each host should be defined with an IP address or domain name. Additionally, the ansible_host variable allows you to map hosts by IP address, which is useful when hostnames differ from IP addresses.

Adding Hosts with SSH User and IP Address

The ansible_host variable can be used to define the host’s actual IP address, while the ansible_user variable specifies the user who should log in.

[webservers]
web1 ansible_host=192.168.1.10 ansible_user=admin
web2 ansible_host=192.168.1.11 ansible_user=admin

In this example inventory:

  • web1 and web2 are the hostnames.
  • ansible_host=192.168.1.10 defines the actual IP address of web1.
  • ansible_user=admin specifies that Ansible will log in using the admin user.

Host Patterns

Ansible allows you to select hosts dynamically with patterns:

PatternDescription
*All hosts
web*Hosts starting with “web”
web[1:2]web1 and web2 specifically

Managing Groups in Ansible Inventory

Groups let you categorize hosts for easier management. You can organize hosts by role (e.g., webservers, databases) or by environment (e.g., production, staging).

Defining Groups

You define groups in the inventory by listing hosts under the group names.

[webservers]
web1 ansible_host=192.168.1.10 ansible_user=admin
web2 ansible_host=192.168.1.11 ansible_user=admin

[databases]
db1 ansible_host=192.168.1.20 ansible_user=dbadmin
db2 ansible_host=192.168.1.21 ansible_user=dbadmin

In this example inventory file:

  • The webservers group contains web1 and web2 with their corresponding IP addresses.
  • The databases group contains db1 and db2.

Nested Groups

Ansible allows nesting groups inside other groups.

[frontend]
web1 ansible_host=192.168.1.10
web2 ansible_host=192.168.1.11

[backend]
db1 ansible_host=192.168.1.20
db2 ansible_host=192.168.1.21

[all:children]
frontend
backend

In this case:

The frontend and backend groups are nested inside the all group, allowing you to target all servers easily.

Using Host and Group Variables

Ansible lets you define variables that apply either to individual hosts or to groups. These variables control aspects like the SSH user, connection port, or even application-specific variables.

Host Variables with ansible_host and ansible_user

You can define host-specific variables directly within the inventory file.

[databases]
db1 ansible_host=192.168.1.20 ansible_user=dbadmin ansible_port=2222
db2 ansible_host=192.168.1.21 ansible_user=dbadmin

In this example:

  • ansible_host defines the IP address of the host.
  • ansible_user sets the SSH user.
  • ansible_port=2222 specifies a custom SSH port for db1.

Group Variables

Group variables apply to all hosts in a group.

[webservers]
web1
web2

[webservers:vars]
ansible_user=webadmin
ansible_port=2222

In this example:

  • All hosts in the webservers group use webadmin as the SSH user.
  • They also use 2222 as the SSH port.

Working with Dynamic Inventory

Dynamic inventory allows you to fetch hosts dynamically from sources like AWS, Azure, or Google Cloud. It’s useful when your infrastructure changes frequently.

Dynamic Inventory Example (AWS EC2 Plugin)

If you’re managing AWS EC2 instances, you can use the AWS EC2 dynamic inventory plugin.

plugin: aws_ec2
regions:
  - us-west-1
keyed_groups:
  - prefix: "tag"
    key: "tags"
hostnames:
  - tag:Name

This configuration will automatically fetch all EC2 instances from the us-west-1 region and group them by tags.

You can list the hosts by running the following command:

 # ansible-inventory --list

Output:

list hosts from inventory

Common Commands to Manage Inventory

Ansible offers several commands to help manage your inventory:

To view all the hosts and groups in your inventory:

 # ansible-inventory --list

For a graphical representation of the inventory:

 # ansible-inventory --graph

To get detailed information about a particular host:

 # ansible-inventory --host web1

Conclusion

Managing hosts and groups using Ansible inventory is essential for effective automation. Whether you’re working with static or dynamic inventory, it’s important to organize your hosts effectively and use variables to customize host behavior.

About Hitesh Jethva

Experienced Technical writer, DevOps professional with a demonstrated history of working in the information technology and services industry. Skilled in Game server hosting, AWS, Jenkins, Ansible, Docker, Kubernetes, Web server, Security, Proxy, Iptables, Linux System Administration, Domain Name System (DNS), and Technical Writing.

View all posts by Hitesh Jethva