Ansible Inventory Design Prompt
Design Ansible inventories — static vs dynamic, group hierarchy, host_vars / group_vars, multi-environment patterns.
- Target user
- Ansible engineers organizing infrastructure
- Difficulty
- Intermediate
- Tools
- Claude, ChatGPT
The prompt
You are a senior automation engineer who has organized Ansible inventories for fleets of thousands — multi-env, multi-region, dynamic sources. I will provide: - The inventory size + structure - Current approach (single file, multi-file, dynamic) - Symptom (wrong hosts targeted, vars not applying, slow gather) Your job: 1. **Static vs dynamic**: - **Static** — INI / YAML; explicit lists - **Dynamic** — script / plugin; cloud-derived - Most production: hybrid 2. **For static structure**: - One file per env: `inventories/production`, `inventories/staging` - `hosts.yml` with grouped hosts - `group_vars/` and `host_vars/` directories 3. **For dynamic plugins**: - `amazon.aws.aws_ec2` for EC2 - `google.cloud.gcp_compute` for GCP - `azure.azcollection.azure_rm` for Azure - YAML config in inventory file 4. **For group hierarchy**: - Parent groups + child groups - `children:` key - Vars inherit child → parent 5. **For host_vars / group_vars**: - One file per host or group - YAML - Loaded automatically per inventory 6. **For multi-env**: - Separate inventory dirs per env - Same playbook - `-i inventories/production` 7. **For special groups**: - `all` — everything - `ungrouped` — no group - Group names are case-sensitive 8. **For combining inventories**: - Multiple `-i` flags - Plugins compose Mark DESTRUCTIVE: targeting wrong inventory in production, broad `all` matches across envs, dynamic inventory with API auth failures. --- Inventory size + structure: [DESCRIBE] Current approach: [DESCRIBE] Symptom: [DESCRIBE]
Why this prompt works
Inventory is the foundation. This prompt walks design.
How to use it
- Pick static vs dynamic per env.
- Group thoughtfully for vars.
- Multi-env via separate dirs.
- Test with
--list-hosts.
Useful commands
# Show inventory
ansible-inventory -i inventories/production --list
ansible-inventory -i inventories/production --graph
# Test which hosts a pattern matches
ansible -i inventories/production webservers --list-hosts
ansible -i inventories/production all --list-hosts
# Dynamic inventory test
ansible-inventory -i aws_ec2.yml --list
# Validate
ansible-inventory -i inventories/production --syntax-check
Patterns
Static multi-file structure
inventories/
├── production/
│ ├── hosts.yml
│ ├── group_vars/
│ │ ├── all.yml
│ │ ├── webservers.yml
│ │ └── databases.yml
│ └── host_vars/
│ └── db-master-01.yml
├── staging/
│ ├── hosts.yml
│ └── group_vars/
└── dev/
└── hosts.yml
# inventories/production/hosts.yml
all:
children:
webservers:
hosts:
web-01.example.com:
web-02.example.com:
web-03.example.com:
vars:
nginx_workers: 8
databases:
children:
db_primary:
hosts:
db-master-01.example.com:
db_replicas:
hosts:
db-replica-01.example.com:
db-replica-02.example.com:
production:
children:
webservers:
databases:
vars:
env: production
Dynamic inventory (AWS)
# inventories/aws_ec2.yml
plugin: amazon.aws.aws_ec2
regions: [us-east-1, us-west-2]
filters:
tag:Environment: production
instance-state-name: running
keyed_groups:
- key: tags.Service
prefix: service
- key: tags.Tier
prefix: tier
hostnames:
- private-dns-name
compose:
ansible_host: private_ip_address
Hybrid (static + dynamic combined)
ansible-playbook -i inventories/production -i aws_ec2.yml playbook.yml
Common findings this catches
- Playbook hits wrong hosts → check
--list-hostsfirst. - Vars not applying → wrong group_vars file name.
- Dynamic inventory empty → API auth or filter.
- Stale cache →
--flush-cache. - Group hierarchy unexpected → use
--graph. allmatches everything in combined inventories.- Hostname format wrong in dynamic plugin → use
compose.
When to escalate
- Cross-team inventory ownership — coordinate.
- Multi-region / multi-cloud — strategic.
- Migration to dynamic — staged.
Related prompts
-
Ansible Dynamic Inventory for Cloud Prompt
Configure dynamic inventory plugins — AWS (aws_ec2), GCP (gcp_compute), Azure (azure_rm), keyed_groups, filters, caching.
-
Ansible Role Generator Prompt
Generate a complete, idempotent Ansible role with proper directory structure, defaults, handlers, molecule tests, and OS-family conditionals.
-
Ansible Variable Precedence Prompt
Debug Ansible variable scope — precedence rules, override behavior, hostvars, magic vars, set_fact lifetime.