Skip to content

Ansible_inventory

This chapter covers Ansible inventory configuration, groups, variables, and dynamic inventory.

The inventory file defines the hosts and groups that Ansible will manage.

┌─────────────────────────────────────────────────────────────────────────────┐
│ Ansible Inventory Concept │
├─────────────────────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────────────────────────────────────────────────────────────┐ │
│ │ Inventory File │ │
│ │ │ │
│ │ [webservers] # Group │ │
│ │ web1.example.com # Host │ │
│ │ web2.example.com │ │
│ │ web3.example.com │ │
│ │ │ │
│ │ [databases] # Group │ │
│ │ db1.example.com │ │
│ │ db2.example.com │ │
│ │ │ │
│ │ [all:vars] # Group variables │ │
│ │ ansible_user=ubuntu │ │
│ │ │ │
│ └─────────────────────────────────────────────────────────────────────┘ │
│ │
│ Inventory Formats: │
│ ✓ INI (default) │
│ ✓ YAML │
│ ✓ Dynamic (script) │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
inventory.ini
# Individual hosts
web1.example.com
web2.example.com
db1.example.com
# Define groups with [groupname]
[webservers]
web1.example.com
web2.example.com
[databases]
db1.example.com
db2.example.com
[appservers]
app1.example.com
app2.example.com
# Nested groups (children)
[production:children]
webservers
databases
appservers
# Group variables
[webservers:vars]
nginx_port=80
max_connections=1000
[databases:vars]
postgres_version=14
# Host-specific variables
[webservers]
web1.example.com ansible_host=192.168.1.10
web2.example.com ansible_host=192.168.1.11
[all:vars]
ansible_user=ubuntu
ansible_python_interpreter=/usr/bin/python3
ansible_ssh_private_key_file=~/.ssh/id_rsa
inventory.yml
---
all:
children:
webservers:
hosts:
web1.example.com:
nginx_port: 80
web2.example.com:
nginx_port: 8080
databases:
hosts:
db1.example.com:
postgres_version: 14
db2.example.com:
postgres_version: 14
vars:
ansible_user: ubuntu
ansible_python_interpreter: /usr/bin/python3
┌─────────────────────────────────────────────────────────────────────────────┐
│ Inventory Patterns │
├─────────────────────────────────────────────────────────────────────────────┤
│ │
│ Pattern Examples: │
│ ┌─────────────────────────────────────────────────────────────────────┐ │
│ │ all All hosts │ │
│ │ * All hosts (wildcard) │ │
│ │ webservers All in webservers group │ │
│ │ !webservers All except webservers │ │
│ │ webservers:databases Union of groups │ │
│ │ webservers[0] First host in group │ │
│ │ webservers[0:2] First 3 hosts in group │ │
│ │ *.example.com Regex pattern │ │
│ │ ~web[0-9]+ Regex pattern │ │
│ └─────────────────────────────────────────────────────────────────────┘ │
│ │
│ Using patterns: │
│ ansible all -m ping │
│ ansible "webservers:!prod" -m setup │
│ ansible "web[1-3]" -i inventory.ini -a "hostname" │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
# inventory.ini with host variables
[webservers]
web1.example.com ansible_host=192.168.1.10 http_port=80
web2.example.com ansible_host=192.168.1.11 http_port=8080
web3.example.com
[databases]
db1.example.com db_port=5432 max_connections=100
# Or define separately
[webservers:vars]
http_port=80
# Host-specific file
# host_vars/web1.example.com
---
ansible_host: 192.168.1.10
http_port: 80
app_version: "2.0"
inventory.ini
[production]
prod-web1.example.com
prod-web2.example.com
prod-db1.example.com
[staging]
stage-web1.example.com
stage-db1.example.com
# Group variables
[production:vars]
environment=production
max_memory=4096
backup_enabled=true
[staging:vars]
environment=staging
max_memory=2048
backup_enabled=false
# Common variables for all
[all:vars]
ansible_user=ubuntu
ansible_python_interpreter=/usr/bin/python3
# Directory structure
inventory/
├── inventory.ini
├── group_vars/
│ ├── all.yml
│ ├── production.yml
│ ├── staging.yml
│ └── webservers.yml
└── host_vars/
├── web1.example.com.yml
└── db1.example.com.yml
group_vars/all.yml
---
ansible_user: ubuntu
ansible_python_interpreter: /usr/bin/python3
# group_vars/production.yml
---
environment: production
max_memory: 4096
log_level: info
# group_vars/webservers.yml
---
nginx_version: latest
php_version: "8.1"
Terminal window
# Install aws plugin
ansible-galaxy collection install amazon.aws
# Configure ec2.ini
# plugin: aws_ec2
inventory/ec2.ini
[aws_credentials]
profile = default
[ec2]
regions = us-east-1, us-west-2
[ec2_tags]
Environment = development
inventory/aws_ec2.yml
plugin: amazon.aws.aws_ec2
regions:
- us-east-1
filters:
instance-state-name: running
keyed_groups:
- prefix: tag
key: tags.Environment
- prefix: instance_type
key: instance_type
host_vars_prefix: aws_
Terminal window
# Point to dynamic inventory script
ansible-playbook -i inventory/aws_ec2.yml playbook.yml
# Or use ec2.py (older method)
chmod +x inventory/ec2.py
ansible-playbook -i inventory/ec2.py playbook.yml
inventory/dynamic_inventory.py
#!/usr/bin/env python3
import json
import os
def get_inventory():
inventory = {
"all": {
"hosts": {},
"children": {
"webservers": {
"hosts": {}
},
"databases": {
"hosts": {}
}
}
}
}
# Fetch from your source (API, database, etc.)
inventory["all"]["hosts"]["web1.example.com"] = {
"ansible_host": "192.168.1.10"
}
inventory["all"]["hosts"]["db1.example.com"] = {
"ansible_host": "192.168.2.10"
}
return inventory
if __name__ == "__main__":
print(json.dumps(get_inventory()))
┌─────────────────────────────────────────────────────────────────────────────┐
│ Inventory Best Practices │
├─────────────────────────────────────────────────────────────────────────────┤
│ │
│ 1. Organize with Groups │
│ ✓ Use environment (prod, staging, dev) │
│ ✓ Use function (webservers, databases) │
│ ✓ Use location (us-east, eu-west) │
│ │
│ 2. Separate Variables │
│ ✓ group_vars/ for group variables │
│ ✓ host_vars/ for host-specific variables │
│ │
│ 3. Use Dynamic Inventory for Cloud │
│ ✓ Auto-discover hosts │
│ ✓ Keep inventory up-to-date │
│ ✓ Tag-based grouping │
│ │
│ 4. Security │
│ ✓ Never commit secrets to version control │
│ ✓ Use ansible-vault for sensitive data │
│ ✓ Use secrets management │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
Terminal window
# List all hosts
ansible all --list-hosts
# List hosts in group
ansible webservers --list-hosts
# List group hierarchy
ansible-inventory -i inventory.ini --graph
# Show inventory as JSON
ansible-inventory -i inventory.ini --list
# Show specific host variables
ansible-inventory -i inventory.ini --host web1.example.com
# Validate inventory file
ansible-inventory -i inventory.ini --list --yaml
production/inventory.ini
# Define hosts
[loadbalancers]
lb1.example.com ansible_host=10.0.1.10
lb2.example.com ansible_host=10.0.1.11
[webservers]
web1.example.com ansible_host=10.0.2.10
web2.example.com ansible_host=10.0.2.11
web3.example.com ansible_host=10.0.2.12
[databases]
db1.example.com ansible_host=10.0.3.10
db2.example.com ansible_host=10.0.3.11
[cache]
cache1.example.com ansible_host=10.0.4.10
cache2.example.com ansible_host=10.0.4.11
# Environment group
[production:children]
loadbalancers
webservers
databases
cache
# Variables
[all:vars]
ansible_user=ubuntu
ansible_python_interpreter=/usr/bin/python3
ansible_ssh_common_args='-o StrictHostKeyChecking=no'
[webservers:vars]
nginx_workers=4
php_fpm_children=10
[databases:vars]
postgres_max_connections=200
postgres_shared_buffers=256MB

In this chapter, you learned:

  • Inventory Overview: What is inventory and why use it
  • Static Inventory: INI and YAML formats
  • Inventory Patterns: Selecting hosts with patterns
  • Host Variables: Host-specific configuration
  • Group Variables: Group-level configuration
  • Dynamic Inventory: AWS and custom scripts
  • Best Practices: Organizing and securing inventory