CertGrid CertGrid
Hands-on Lab·Ansible

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

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.

Both managed nodes, addressed by a script rather than by the static inventory file.
Server NameIP AddressOSRolesCPURAMHDD
ANS-CTL01192.168.0.36Ubuntu 26.04 LTSAnsible Control Node2 Core3 GB50 GB
ANS-A01192.168.0.37Ubuntu 26.04 LTSManaged Node (group: web)2 Core3 GB50 GB
ANS-B01192.168.0.38Ubuntu 26.04 LTSManaged Node (group: db)2 Core3 GB50 GB

This guide includes

Use this when a static inventory file has stopped being maintainable. This matters because an inventory can be a program - it has to answer two specific questions, and the constructed plugin adds groups without you writing any code.

Before you start

  1. The configuration this guide assumes

  2. An inventory that is a program

  3. Ansible reads it the same way

  4. The two questions a script must answer

  5. The constructed plugin adds groups without code

  6. The key must be a string

  7. Why it cannot group on facts

Official sources