Static inventories feel fine—until they don’t.
You start with a small cluster, maybe a handful of servers defined neatly in an inventory.ini. Then autoscaling kicks in. Instances come and go. Someone forgets to update the inventory. A deployment fails. Suddenly, the static approach becomes a liability.
This is exactly where Ansible dynamic inventories step in.
What Makes Dynamic Inventory Different?
Instead of maintaining a fixed list of hosts, dynamic inventory pulls infrastructure data directly from a source of truth—like a cloud provider, CMDB, or API.
Think of it as asking:
“What servers exist right now?” instead of “What servers did I write down last week?”
That shift is what enables real scalability.
A Quick Comparison
- Static inventory: Manual, file-based, error-prone at scale
- Dynamic inventory: Automated, API-driven, always up-to-date
Starting with a Simple Dynamic Inventory Script
Before diving into cloud plugins, it helps to understand the basics. At its core, a dynamic inventory is just a script that outputs JSON.
Here’s a minimal Python example:
1#!/usr/bin/env python3
2import json
3
4inventory = {
5 "web": {
6 "hosts": ["192.168.1.10", "192.168.1.11"]
7 },
8 "db": {
9 "hosts": ["192.168.1.20"]
10 }
11}
12
13print(json.dumps(inventory))
14Run it with:
1ansible-inventory -i inventory.py --listNot impressive yet—but this is the foundation. Replace hardcoded values with API calls, and you have a dynamic system.
Where Things Get Interesting: Cloud-Based Dynamic Inventory
Most teams don’t write scripts from scratch anymore. Ansible provides inventory plugins for major platforms.
AWS Example Using aws_ec2 Plugin
Create a config file:
1# aws_ec2.yml
2plugin: amazon.aws.aws_ec2
3regions:
4 - us-east-1
5filters:
6 tag:Environment: production
7keyed_groups:
8 - key: tags.Role
9 prefix: role
10hostnames:
11 - private-ip-address
12Then run:
1ansible-inventory -i aws_ec2.yml --graphThis automatically groups instances by tags like:
- role_web
- role_api
- role_db
No manual updates. Spin up a new EC2 instance with the right tag, and it’s instantly part of your inventory.
Dynamic Grouping: The Real Power Move
Dynamic inventories aren’t just about discovering hosts—they’re about organizing them intelligently.
Let’s say your infrastructure uses tags:
- Environment: staging, production
- Role: web, worker, db
You can generate groups like:
- production_web
- staging_worker
This allows highly targeted playbooks:
1- hosts: production_web
2 tasks:
3 - name: Deploy frontend
4 command: ./deploy.sh
5No need to maintain group definitions manually—they evolve with your infrastructure.
A Common Mistake Developers Make
Many teams adopt dynamic inventory but keep thinking in static terms.
Examples:
- Hardcoding assumptions about hostnames
- Ignoring tagging strategy
- Mixing static and dynamic inventories inconsistently
If your tagging or labeling isn’t clean, your dynamic inventory becomes chaotic.
Tip: Treat tags as part of your architecture, not an afterthought.
Performance Considerations at Scale
Querying APIs on every run can introduce latency, especially in large environments.
Here’s how teams handle it:
- Caching: Use Ansible’s inventory cache to reduce API calls
- Scoped queries: Filter by region, tags, or environment
- Parallelism: Combine with forks for faster execution
Example cache config:
1[inventory]
2enable_plugins = amazon.aws.aws_ec2
3cache = yes
4cache_plugin = jsonfile
5cache_timeout = 300
6This avoids hitting AWS APIs repeatedly during short deployment cycles.
When Dynamic Inventory Might Not Be Ideal
It’s not always the right tool.
- Very small, stable environments
- Air-gapped systems with no API access
- Simple one-off automation tasks
In those cases, static inventory is simpler and perfectly adequate.
Blending Static and Dynamic Inventories
You don’t have to choose one exclusively.
Many teams use:
- Dynamic inventory for cloud infrastructure
- Static inventory for legacy systems
Example:
1ansible-playbook -i aws_ec2.yml -i legacy.ini playbook.ymlAnsible merges both sources seamlessly.
Why Dynamic Inventory Changes How You Think
Once you adopt dynamic inventories, your mindset shifts:
- You stop tracking servers manually
- You rely on metadata (tags, labels)
- You design infrastructure to be discoverable
This aligns perfectly with modern DevOps practices like immutable infrastructure and autoscaling.
Instead of managing machines, you manage systems.
Final Thought
Dynamic inventory isn’t just a feature—it’s a scaling strategy.
If your infrastructure changes frequently (and it probably does), static files will eventually hold you back. Moving to dynamic inventory means fewer manual updates, fewer deployment surprises, and a system that reflects reality in real time.
And once you experience that, going back to static lists feels like editing DNS records by hand.