Give a summary of variable precedence.
IntermediateConceptVariables, Facts and Precedence
Answer
There are 22 levels. A practical summary from lowest to highest:
- role
defaults - inventory group vars, then host vars (
group_vars/all< other groups <host_vars) - playbook
group_varsandhost_vars - host facts and cached
set_fact - play
vars,vars_prompt,vars_files - role
vars, block vars, task vars include_vars,set_fact, registered variables- role and include parameters
- extra vars (
-e)
Rule of thumb: the closer to the task, and the more explicit, the higher it wins. Extra vars always win.
More on Variables, Facts and Precedence
Where can you define variables in Ansible?BeginnerHow do you reference a variable, and when must you quote it?BeginnerWhat are the lowest and highest precedence sources, and why does that matter?IntermediateWhat is the difference between a role's `defaults/` and `vars/`?IntermediateWhat are facts and how are they gathered?BeginnerWhat is the difference between `ansible_facts['x']` and the older `ansible_x` variables?Intermediate