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.
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?BeginnerGive a summary of variable precedence.IntermediateWhat 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?Beginner