What it is
Ansible inventory is essentially a list or collection of hosts (servers, workstations, network devices, etc.) that Ansible can manage. It tells Ansible what machines exist, how to connect to them, and how to group them for easier management. Inventories can be static (simple files listing hosts and their properties) or dynamic (generated by scripts or cloud providers). Hosts can be organized into groups, and variables can be assigned at the host or group level to customize behavior. Inventories provide the foundation for playbooks, as playbooks act on the hosts defined here.
Example
# This is a static inventory file in INI format
# Define a group of all Ubuntu workstations
[workstations]
# Each host entry can include ansible connection variables
Lab-1 ansible_host=192.168.1.10 ansible_user=admin ansible_become=true
Lab-2 ansible_host=192.168.1.11 ansible_user=admin ansible_become=true
# Define a group of all Linux servers
[servers]
# These hosts might be production servers
Server-1 ansible_host=192.168.1.20 ansible_user=root ansible_ssh_private_key_file=/home/admin/.ssh/id_rsa
Server-2 ansible_host=192.168.1.21 ansible_user=root ansible_ssh_private_key_file=/home/admin/.ssh/id_rsa
# Define a group of database servers
[databases]
# This could be a subgroup of servers for targeted tasks
DB-Primary ansible_host=192.168.1.30
DB-Replica ansible_host=192.168.1.31
# Example of group variables
# Variables can be defined for all hosts in a group
[workstations:vars]
# Default password for becoming root (use vault in production)
ansible_become_pass='my_secure_password'
# Specify package manager type if needed
pkg_mgr=apt
# Children groups (nested grouping)
[production:children]
servers
databasesKeep in mind that any variable defined per-host in a group can be added under
[<group>:vars]to apply to all hosts in that group.For example, adding the following under
[workstations:vars]or the YAML equivalent ingroup_vars/workstations.ymlwill set bothansible_user=adminandansible_become=truefor all hosts in the workstations group.ansible_user=admin ansible_become=true
Inventories consist of hosts, groups, variables, and nested groupings, which allow playbooks to operate on multiple hosts efficiently without repeating connection details.
Dynamic Inventories
For cloud environments, you can use dynamic inventory scripts or plugins that automatically discover hosts:
# Example: Using AWS EC2 plugin
plugin: amazon.aws.aws_ec2
regions:
- us-east-1
keyed_groups:
- key: tags.Environment
prefix: env
- key: instance_type
prefix: typeGroup variables(group_vars/)
The group_vars/ directory is used to define variables for groups of hosts. Groups are defined in your inventory, and any variables in group_vars/<groupname>.yml apply to all hosts in that group. This allows you to avoid repeating variables for every host and maintain consistency across a set of servers with similar roles, such as webservers, databases, or loadbalancers. It’s conceptually like a “team profile” – every member inherits the group’s shared settings, reducing duplication and simplifying maintenance.
Similar to host_vars, the variables are automatically loaded by Ansible when it detects the group in the inventory. Group variables also participate in the Ansible variable precedence hierarchy: host_vars override group_vars if a variable exists in both places. You can also have nested groups, allowing for shared variables at multiple levels (e.g., all → webservers → webserver1). This makes group_vars essential for scaling Ansible deployments efficiently while keeping configuration DRY (Don’t Repeat Yourself).
Example
group_vars/workstations.yml:
# Variables common to all hosts in the workstation group
ansible_user: admin # Default SSH user for webservers
nginx_max_clients: 1024 # Common Nginx setting
firewall_enabled: true # Apply firewall rules to all webservers
monitoring_enabled: true # Enable monitoring for webservers
Host variables(host_vars/)
In Ansible, the host_vars/ directory is used to store variables specific to individual hosts. Each file in this directory is named after a host defined in your inventory. When Ansible runs a playbook, it automatically loads these variables and applies them only to the corresponding host. This is extremely useful for managing host-specific configurations, like unique IP addresses, credentials, or system-specific settings, without polluting your global variable space. Conceptually, you can think of host_vars as a place where each server “has its own profile,” allowing for precise and isolated customization.
The variables in host_vars are typically YAML files containing key-value pairs. These files are automatically included by Ansible, so you do not need to reference them explicitly in your playbooks. The hierarchy ensures that host_vars override group-level or default variables when conflicts occur, giving you granular control over your environment. This mechanism is particularly important in heterogeneous environments where each host might need slightly different settings but still share common roles or tasks.
Example
host_vars/Lab-1.yml:
# Host-specific variables for 'Lab-1'
ansible_user: admin # SSH user for this host
ansible_port: 2222 # Non-default SSH port
nginx_worker_processes: 4 # Custom Nginx worker processes for this host
db_host: "dbserver.internal" # Points to a specific database server