Hello,
I know that you can, but it seems ambiguous on whether one should.
For example:
There is a role named "role_1", which has templated variables in both "defaults/main.yml" and "vars/main.yml".
I could create a playbook which prints those variables for a set of hosts, and the variables would change for each host.
For the sake of discussion, here is what those role variables could be:
---
# defaults file for role_1
my_default_inventory: "{{inventory_hostname}}-sample"
---
# vars file for role_1
some_var: "{{ansible_hostname}}"
However, many examples from documentation and from around the web seem to place all non-templated role variables in either the roles vars or defaults.
This seems especially true if the variables are templated from facts.
Instead, these examples will often use the "set_fact" module to set these variables.
The ambiguity seems to rise from the understanding of "variable scope". The documentation does not clearly state when role variables are re-templated. So, if you have a variable that you know will change from host to host but only needs to be set once per role, it seems that people rely on explicitly placing variables into the "facts". I'm guessing that the use of "set_fact" is used under the assumption that authors are unclear on whether they can rely on role variables being in the "host" scope.
So, please help me clear up my guesses and speculations!
Does the example stated above work by design, or by accident? Specifically, can I rely on role variables being re-templated within the "host" scope?
Please let me know if you need more clarification.
Thank you.