Interview Prep/Ansible/Variables, Facts and Precedence

What is the precedence relationship between `set_fact`, `include_vars`, task vars and role vars?

AdvancedConceptVariables, Facts and Precedence
Answer

From lower to higher: role vars < block vars < task vars < include_vars < set_fact/registered < role parameters < include parameters < extra vars.

A practical implication: set_fact overrides an inventory variable for the rest of the run and cannot be undone by group_vars. Another: a variable set with set_fact will also beat a task vars value of the same name in a later task, which has led to many "why is it not changing" bugs. Use a different name or avoid set_fact for values that should remain overridable.

All Ansible questions Roadmap: Ansible

More on Variables, Facts and Precedence