Projects
Writings
Notes
Log
Glossary
Contact

Understanding Ansible Inventory Files

April 13, 2026

Hosts, groups, variables, and dynamic inventory explained

Understanding Ansible Inventory Files

Ansible inventory files are crucial components of Ansible, serving as the primary means to define the hosts and groups that Ansible will manage. Here's a comprehensive overview covering all aspects of Ansible inventory files:

What is an Ansible Inventory File?

An Ansible inventory file is a plain text file that contains information about the hosts and groups of hosts that Ansible will manage. It provides Ansible with the necessary information to connect to remote hosts and execute tasks.

Format of Inventory File

Inventory files typically follow an INI-style format, although YAML format is also supported. The file may contain hostnames, IP addresses, and groupings of hosts.

Example of INI-style Inventory File

ini
[web_servers]
server1.example.com
server2.example.com
 
[db_servers]
server3.example.com

Example of YAML Inventory File

yaml
all:
hosts:
server1.example.com:
server2.example.com:
children:
web_servers:
hosts:
server1.example.com:
server2.example.com:
db_servers:
hosts:
server3.example.com:

Sections in Inventory File

  • Hosts: Individual hosts are listed directly under the [all] section or under specific group sections.
  • Groups: Hosts can be grouped together under section headers, facilitating the targeting of specific sets of hosts.
  • Variables: You can define variables for hosts or groups within the inventory file.

Hosts and Groups

  • Hosts: Represent individual machines or servers that Ansible will manage.
  • Groups: Logical collections of hosts that allow tasks to be executed against multiple hosts simultaneously.

Host Attributes

  • IP Address/Hostname: Defines the network location of the host.
  • SSH Port: Specifies the port to use for SSH connections.
  • Connection Type: Specifies the connection type, such as ssh or winrm.
  • Ansible User: Specifies the username Ansible should use when connecting to the host.
  • Group Memberships: Indicates which groups the host belongs to.

Dynamic Inventory

Dynamic inventory allows Ansible to use external sources like cloud providers, database queries, or custom scripts to generate the inventory dynamically. This enables dynamic scaling and flexible infrastructure management.

Patterns

Patterns are used to select hosts or groups to execute tasks against. They allow for targeted execution of tasks based on various criteria, such as groups, patterns, or regular expressions.

Examples of Patterns

  • all: Targets all hosts in the inventory.
  • group_name: Targets all hosts in a specific group.
  • host_name: Targets a specific host.
  • *: Targets all hosts in the inventory.
  • group_name[0]: Targets the first host in a group.

Variable Assignment

Variables can be assigned to individual hosts or groups within the inventory file. These variables can then be referenced in playbooks to customize tasks based on specific host or group attributes.

Example of Variable Assignment

ini
[web_servers]
server1.example.com ansible_user=ubuntu ansible_ssh_private_key_file=/path/to/private_key.pem

Alias and Meta

  • Alias: Allows defining an alternative name for a host.
  • Meta: Provides metadata about hosts or groups, such as documentation or inventory plugin options.

Example of Alias and Meta:

ini
[web_servers]
server1.example.com ansible_host=192.168.1.10
 
[web_servers:vars]
http_port=80
 
[web_servers:children]
load_balancers

Excluding Hosts and Groups

You can exclude hosts or groups from execution using the --limit or --exclude options in Ansible commands, providing greater control over task execution.

Using Patterns with Ansible Commands

Patterns can be used with various Ansible commands like ansible, ansible-playbook, and ansible-galaxy to target specific hosts or groups during execution.

Integration with Dynamic Inventory Plugins

Ansible supports various dynamic inventory plugins that allow fetching inventory information from external sources like cloud providers (AWS, Azure), configuration management databases (CMDBs), or custom scripts.

Conclusion

Understanding Ansible inventory files is fundamental for effectively managing infrastructure with Ansible. By mastering the concepts covered above, you'll be equipped to create and manage Ansible inventory files efficiently, enabling streamlined automation and configuration management workflows.

Dart
Java
Python
Pytest
LaTeX
JavaScript
Jest
TypeScript
React
Gatsby
HTML
Markdown
CSS
Sass
Bootstrap
Tailwind CSS
Django
Flask
Flutter
SQLite

© 2025–2026 manjana/blue-hexagon

Connect with me on GitHub or LinkedIn

PostgreSQL
Nginx
Postman
Terraform
Vim
Git
Github
Heroku
Docker
GitHub Actions
Linux
DigitalOcean
JetBrains
PyCharm
Bash
VMWare
Cisco
Ansible
Paloalto