ansible-inventory: Usage & Common CI Errors
Inspect exactly which hosts and groups Ansible sees.
ansible-inventory shows the parsed inventory - hosts, groups, and variables - so you can debug static and dynamic sources before running a playbook against the wrong (or empty) set of hosts.
What it does
ansible-inventory parses an inventory source and renders it. --list emits the full inventory as JSON, --graph draws the group/host tree, and --host shows the variables for one host. It is the fastest way to confirm a dynamic inventory plugin (AWS EC2, GCP) actually returns hosts.
Common usage
# Full inventory as JSON
ansible-inventory -i inventory.ini --list
# Human-readable group/host tree
ansible-inventory -i inventory_aws_ec2.yml --graph
# Show resolved vars for a single host
ansible-inventory -i inventory.ini --host web01Common error in CI: dynamic inventory returns no hosts
A run does nothing because ansible-inventory --graph shows only "@all:" with no hosts, or errors "Failed to parse ... with auto plugin". The dynamic plugin lacks credentials/filters or the file is not enabled. Fix: confirm the plugin is enabled (enable_plugins in ansible.cfg), supply cloud credentials to the runner (e.g. AWS_REGION + role), and validate filters by running ansible-inventory --list first - never run a playbook against an inventory you have not confirmed is non-empty.
Key options
| Option | Purpose |
|---|---|
| --list | Output full inventory as JSON |
| --graph | Show the group/host tree |
| --host NAME | Show one host’s variables |
| -i SOURCE | Inventory file or plugin config |
| -y | Output in YAML instead of JSON |