Caching & Multiple Inventory Sources
Merge and speed up large inventories.
Caching & Multiple Inventory Sources is a free Ansible Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ansible Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Queries Cost Time
Hitting the cloud API on every run is slow when you have hundreds of hosts. Each play start pays that lookup tax again. ⏱️
Cache the Results
Inventory caching stores the fetched host data so repeated runs reuse it instead of re-querying the cloud every time.
Turn Caching On
Enable it in the plugin config or ansible.cfg with the cache key set to true.
cache: true
cache_plugin: jsonfileChoose a Cache Backend
The cache_plugin picks where data lives, like jsonfile on disk or redis for a shared, faster store across machines.
Set a Sensible Timeout
The cache_timeout in seconds decides how long cached data stays valid before Ansible refreshes from the source.
cache_timeout: 3600Stale Cache Is a Trap
A long timeout is fast but risky. If instances changed, you may target ghosts. Balance speed against freshness for your fleet.
Combine Many Sources
You rarely have just one source. Point -i at a directory and Ansible loads every inventory file inside it.
ansible-playbook -i inventories/ site.ymlSources Merge Together
Hosts and groups from all files combine into one inventory, so AWS, Azure, and a static file can coexist in a single run.
Order Affects Variables
Files load alphabetically. When two sources set the same host variable, the later source wins, so naming order matters.
Mix Static and Dynamic
A static file can pin a special box while a plugin pulls the rest, giving you fixed anchors alongside elastic, auto-discovered hosts.
Verify the Merged View
Run ansible-inventory against the directory to see the final merged graph and confirm every source contributed as you expect.
ansible-inventory -i inventories/ --graphQuick Check
Recall how to combine inventory sources.
Recap
Use caching with a backend and timeout to cut API calls, and point -i at a directory to merge multiple inventory sources. 💾
Frequently asked questions
Is the “Caching & Multiple Inventory Sources” lesson free?
Yes — the full text of “Caching & Multiple Inventory Sources” is free to read here on the web, and the Ansible Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ansible Academy course, upgrade to CoddyKit PRO.
What will I learn in “Caching & Multiple Inventory Sources”?
Merge and speed up large inventories. You practise Ansible Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Ansible Academy?
No prior experience is required. Ansible Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Caching & Multiple Inventory Sources” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Ansible Academy lesson?
Yes. Every Ansible Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Static Limits & the Inventory Plugin Model
- The aws_ec2 Inventory Plugin
- constructed: Group Hosts by Tags
- Caching & Multiple Inventory Sources