Ansible inventory defines which hosts you manage and how to connect to them. It is the foundation of every Ansible deployment.
Static Inventory
INI Format
# inventory/hosts.ini
[webservers]
web1.example.com
web2.example.com
web3.example.com ansible_port=2222
[dbservers]
db1.example.com
db2.example.com
[loadbalancers]
lb1.example.com
[production:children]
webservers
dbservers
loadbalancers
[production:vars]
ansible_user=deploy
ansible_ssh_private_key_file=~/.ssh/deploy_keyYAML Format (Preferred)
# inventory/hosts.yml
all:
children:
production:
children:
webservers:
hosts:
web1.example.com:
web2.example.com:
web3.example.com:
ansible_port: 2222
vars:
http_port: 80
app_env: production
dbservers:
hosts:
db1.example.com:
postgresql_max_connections: 200
db2.example.com:
postgresql_max_connections: 100
vars:
db_port: 5432
loadbalancers:
hosts:
lb1.example.com:
vars:
ansible_user: deploy
ansible_ssh_private_key_file: ~/.ssh/deploy_keyHost and Group Variables
Directory Structure
inventory/
hosts.yml
group_vars/
all.yml # Variables for all hosts
webservers.yml # Variables for webservers group
dbservers.yml # Variables for dbservers group
production.yml # Variables for production group
host_vars/
web1.example.com.yml # Variables for specific host
db1.example.com.ymlgroup_vars/all.yml
---
ntp_servers:
- 0.pool.ntp.org
- 1.pool.ntp.org
timezone: Europe/Rome
admin_email: ops@example.comgroup_vars/webservers.yml
---
http_port: 80
https_port: 443
document_root: /var/www/html
nginx_worker_processes: autohost_vars/web1.example.com.yml
---
# This host has extra memory, can handle more connections
nginx_worker_connections: 4096
ssl_certificate: /etc/ssl/certs/web1.pemMaster this topic with hands-on labs
Go beyond reading — build real projects in sandboxed environments with expert video guidance.
Browse Courses →Variable Precedence
From lowest to highest:
group_vars/allgroup_vars/host_vars/- Playbook
vars: - Playbook
vars_files: - Task
vars: - Extra vars (
-e) — highest priority
# Override any variable at runtime
ansible-playbook site.yml -e "http_port=8080"Dynamic Inventory
AWS EC2
# inventory/aws_ec2.yml
plugin: aws_ec2
regions:
- eu-west-1
filters:
tag:Environment:
- production
instance-state-name: running
keyed_groups:
- key: tags.Role
prefix: role
- key: placement.availability_zone
prefix: az
compose:
ansible_host: private_ip_address# List discovered hosts
ansible-inventory -i inventory/aws_ec2.yml --list
# Use in playbook
ansible-playbook -i inventory/aws_ec2.yml site.ymlCustom Script
#!/usr/bin/env python3
# inventory/custom_inventory.py
import json
import requests
def get_inventory():
# Fetch from your CMDB, API, or database
hosts = requests.get("https://cmdb.internal/api/hosts").json()
inventory = {
"webservers": {
"hosts": [h["fqdn"] for h in hosts if h["role"] == "web"],
"vars": {"http_port": 80}
},
"dbservers": {
"hosts": [h["fqdn"] for h in hosts if h["role"] == "db"],
},
"_meta": {
"hostvars": {
h["fqdn"]: {"ansible_host": h["ip"]}
for h in hosts
}
}
}
return inventory
if __name__ == "__main__":
print(json.dumps(get_inventory(), indent=2))chmod +x inventory/custom_inventory.py
ansible-playbook -i inventory/custom_inventory.py site.ymlInventory Patterns
Target specific hosts in commands:
# All hosts
ansible all -m ping
# Specific group
ansible webservers -m ping
# Multiple groups
ansible 'webservers:dbservers' -m ping
# Intersection (hosts in BOTH groups)
ansible 'webservers:&production' -m ping
# Exclusion
ansible 'all:!loadbalancers' -m ping
# Pattern matching
ansible 'web*.example.com' -m ping
# Regex
ansible '~web[0-9]+\.example\.com' -m ping
# First host in group
ansible 'webservers[0]' -m ping
# Range
ansible 'webservers[0:2]' -m pingGet weekly IT automation tips
Docker, Ansible, Terraform, MLOps — curated insights delivered to your inbox. No spam.
Subscribe Free →Multi-Environment Setup
inventory/
production/
hosts.yml
group_vars/
all.yml # production-specific defaults
staging/
hosts.yml
group_vars/
all.yml # staging-specific defaults
development/
hosts.yml
group_vars/
all.yml # dev-specific defaultsansible-playbook -i inventory/staging site.yml
ansible-playbook -i inventory/production site.ymlDebugging Inventory
# List all hosts
ansible-inventory -i inventory/ --list
# Show specific host variables
ansible-inventory -i inventory/ --host web1.example.com
# Graph view
ansible-inventory -i inventory/ --graph
# Test connectivity
ansible all -i inventory/ -m pingWhat's Next?
Our Ansible Automation in 30 Minutes course covers inventory management with hands-on labs. First lesson is free.
---
Ready to go deeper? Check out our hands-on course: Ansible Quickstart — practical exercises you can follow along on your own machine.
Ready to learn by doing?
Stop reading tutorials — start building. Expert video courses with hands-on labs in real sandboxed environments.
Related Articles
Ansible Inventory: Static and Dynamic
Master Ansible inventory in INI and YAML formats. Learn host groups, variables, dynamic inventory plugins for AWS and Azure, and patterns for scaling.
Ansible vs Terraform When to Use
Ansible and Terraform solve different infrastructure problems. Learn when to use each, when to use both together, and how they complement each other.
Ansible Infrastructure as Code Guide
Use Ansible as your Infrastructure as Code tool. Learn how playbooks define, version, and automate your entire infrastructure.
Ansible Jinja2 Templates Guide
Master Jinja2 templates in Ansible for dynamic configuration. Variables, filters, loops, conditionals, and real-world config file generation examples.
Ansible Playbook for Docker Install
Automate Docker installation on Ubuntu and RHEL with an Ansible playbook. Includes Compose plugin and post-install steps.
Ansible Playbook Error Handling
Handle errors gracefully in Ansible playbooks. Learn ignore_errors, rescue blocks, retries, assertions, and proven recovery patterns for reliable automation.
Explore topics
Browse more articles on the topics covered here.