Interview Prep/Ansible/Variables, Facts and Precedence

Give a summary of variable precedence.

IntermediateConceptVariables, Facts and Precedence
Answer

There are 22 levels. A practical summary from lowest to highest:

  1. role defaults
  2. inventory group vars, then host vars (group_vars/all < other groups < host_vars)
  3. playbook group_vars and host_vars
  4. host facts and cached set_fact
  5. play vars, vars_prompt, vars_files
  6. role vars, block vars, task vars
  7. include_vars, set_fact, registered variables
  8. role and include parameters
  9. extra vars (-e)

Rule of thumb: the closer to the task, and the more explicit, the higher it wins. Extra vars always win.

All Ansible questions Roadmap: Ansible

More on Variables, Facts and Precedence