Interview Prep/Ansible/Inventory and Targeting

You inherited a 400-line INI inventory with repeated variables. How do you clean it up without breaking anything?

IntermediateScenarioInventory and Targeting
Answer

Situation read → Refactor with a safety net: the effective variables per host must not change.

What I'd do →

  1. Snapshot: ansible-inventory -i old --list > before.json.
  2. Move shared variables into group_vars/ and per-host ones into host_vars/.
  3. Snapshot again and diff.
ansible-inventory -i inventory --list > after.json
diff <(jq -S . before.json) <(jq -S . after.json)

Why → ansible-inventory --list shows merged hostvars, so an empty diff proves the refactor is behaviour-neutral.

Interview tip → Use --graph --vars for a quick human-readable view of the group tree.

All Ansible questions Roadmap: Ansible

More on Inventory and Targeting