Ansible Dynamic Inventory
The exam asks for a static inventory, and a static inventory stops working the moment something else creates and destroys your hosts. A dynamic inventory is any program that prints the right JSON - that is the entire contract. This builds one, then uses the constructed plugin to add groups without code, including the limitation that catches everybody.
Bonus: Beyond the Blueprint Guide 41 of 45 Advanced
- OSUbuntu 26.04 LTS
- ansible-core2.20.1
- Python3.14.4
- TimeAbout 16 min
- Reviewed23 August 2026
Written against the versions above. Ansible accepts three kinds of inventory source: a file, a **directory** (all sources merged, in filename order), and an **executable** that prints JSON. The executable must answer `--list` and `--host <name>`, though a `_meta` key containing all hostvars means `--host` is never actually called - which is why every practical implementation includes it.
| Server Name | IP Address | OS | Roles | CPU | RAM | HDD |
|---|---|---|---|---|---|---|
| ANS-CTL01 | 192.168.0.36 | Ubuntu 26.04 LTS | Ansible Control Node | 2 Core | 3 GB | 50 GB |
| ANS-A01 | 192.168.0.37 | Ubuntu 26.04 LTS | Managed Node (group: web) | 2 Core | 3 GB | 50 GB |
| ANS-B01 | 192.168.0.38 | Ubuntu 26.04 LTS | Managed Node (group: db) | 2 Core | 3 GB | 50 GB |
Before you start
- A control node configured as shown in the first step.
- The session writes
~/dyn-inventory.pyand an~/inv/directory, and removes both.
-
The configuration this guide assumes
-
An inventory that is a program
-
Ansible reads it the same way
-
The two questions a script must answer
-
The constructed plugin adds groups without code
-
The key must be a string
-
Why it cannot group on facts