Static Inventory (INI)
The simplest format:
[web]
web1 ansible_host=192.168.1.10
web2 ansible_host=192.168.1.11
[db]
db1 ansible_host=192.168.1.20
[web:vars]
ansible_user=ubuntu
http_port=80
[db:vars]
ansible_user=postgresYAML Format
More structured, better for complex setups:
all:
children:
web:
hosts:
web1:
ansible_host: 192.168.1.10
web2:
ansible_host: 192.168.1.11
vars:
ansible_user: ubuntu
http_port: 80
db:
hosts:
db1:
ansible_host: 192.168.1.20
vars:
ansible_user: postgresGroup Hierarchies
Nest groups for environment-based targeting:
[web_prod]
web-prod-1 ansible_host=10.0.1.10
web-prod-2 ansible_host=10.0.1.11
[web_staging]
web-stg-1 ansible_host=10.0.2.10
[db_prod]
db-prod-1 ansible_host=10.0.1.20
[prod:children]
web_prod
db_prod
[staging:children]
web_staging
[web:children]
web_prod
web_stagingNow you can target:
- ansible-playbook site.yml -l prod — all production
- ansible-playbook site.yml -l web — all web servers
- ansible-playbook site.yml -l web_prod — production web only
Learn this topic with guided video lessons
Go beyond reading — follow expert video guidance and try the examples in your own environment.
Browse Courses →Host and Group Variables
Store variables in files alongside your inventory:
inventory/
├── hosts.yml
├── group_vars/
│ ├── all.yml # Applies to everything
│ ├── web.yml # Web server vars
│ └── prod.yml # Production vars
└── host_vars/
└── web-prod-1.yml # Specific host varsgroup_vars/all.yml:
ntp_server: time.google.com
dns_servers:
- 8.8.8.8
- 8.8.4.4group_vars/prod.yml:
monitoring_enabled: true
log_level: warnDynamic Inventory — AWS
Auto-discover EC2 instances:
aws_ec2.yml:
plugin: amazon.aws.aws_ec2
regions:
- eu-west-1
filters:
tag:Environment:
- production
keyed_groups:
- key: tags.Role
prefix: role
- key: placement.availability_zone
prefix: az
compose:
ansible_host: private_ip_address# Test it
ansible-inventory -i aws_ec2.yml --list
# Use it
ansible-playbook -i aws_ec2.yml site.ymlDynamic Inventory — Azure
plugin: azure.azcollection.azure_rm
auth_source: auto
include_vm_resource_groups:
- my-resource-group
keyed_groups:
- key: tags.environment | default('unknown')
prefix: envGet weekly IT automation tips
Docker, Ansible, Terraform, MLOps — curated insights delivered to your inbox. No spam.
Subscribe Free →Verify Your Inventory
# List all hosts
ansible-inventory -i inventory/ --list
# Graph view
ansible-inventory -i inventory/ --graph
# Test connectivity
ansible all -i inventory/ -m pingBest Practices
- Use YAML for complex inventories — more readable than INI
- Use
group_vars/instead of inline variables - Use dynamic inventory for cloud — manual host lists drift
- Keep sensitive vars in Ansible Vault encrypted files
- Name groups meaningfully:
web_prodnotgroup1
Related Posts
- Ansible Automation in Minutes for getting started
- Ansible Roles: Reusable Automation for structuring playbooks
- Ansible Vault: Encrypt Secrets for securing variables
---
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 practical examples you can follow at your own pace.
Related Articles
Ansible Inventory Management Guide
Master Ansible inventory management with static and dynamic inventories, host groups, variables, patterns, and seamless cloud provider integration.
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.