DevOps / Ansible Interview questions
Explain the internal working of Ansible's variable precedence resolution?
Internally, Ansible builds up a variable's final value by layering every source that defines it, in a fixed precedence order, so the value that "wins" is whichever source sits highest in that order, regardless of the order files happen to be read from disk.
As Ansible processes a playbook, it maintains an internal merged variable namespace per host; each layer above simply overwrites the same key if it's also defined at a lower layer, rather than merging values together (with the notable exception of hash/dictionary merging, which can be configured via hash_behaviour, though the default is still overwrite, not merge). Because -e extra vars sit at the very top, they always win regardless of what's set anywhere else in roles, inventory, or playbooks - this is deliberate, since it gives an operator a reliable way to force a value at runtime without needing to trace through every file that might otherwise define it.
More Related questions...