A DevOps engineer is designing a dynamic inventory script for a cloud provider. The script must return host variables in a specific JSON format. According to Ansible best practices, which top-level keys should be present in the script output?
Required format for dynamic inventory output.
Why this answer
Ansible dynamic inventory scripts must return JSON with group names as top-level keys, each containing 'hosts' (a list of hostnames) and optionally 'vars' (dictionary of group variables). This structure allows Ansible to map hosts to groups and apply group-level variables, which is essential for inventory organization and playbook targeting. Option A correctly describes this required format per Ansible best practices.
Exam trap
The trap here is that candidates confuse the dynamic inventory JSON schema with the structure of an Ansible inventory file (INI or YAML) or with the output of the 'ansible-inventory' command, leading them to select options like 'all, groups, hosts' or 'inventory, hosts, vars' instead of recognizing that group names must be the top-level keys.
How to eliminate wrong answers
Option B is wrong because 'all', 'groups', and 'hosts' are not the required top-level keys; 'all' is a default group in Ansible but not a mandatory key in the script output, and 'groups' and 'hosts' are not valid top-level keys for the inventory JSON structure. Option C is wrong because a list of host objects does not provide the group-based hierarchy Ansible expects; the script must return a dictionary with group names as keys, not a flat list. Option D is wrong because 'inventory', 'hosts', and 'vars' are not the correct top-level keys; the inventory script output must use group names as keys, and 'inventory' is not a recognized key in the dynamic inventory JSON schema.