> However, when I try to obtain variable in another host this rule is broken again: `
> hostvars[ansible_host].testvar02` expands to `var_in_role: {{var_in_role}}`.
> The var_in_role is defined in role vars.
>
> ...
>
> vars:
> testvar01: "set in group"
> testvar02: "var_in_role: {{var_in_role}}"
> testvar03: "var_in_role_default: {{var_in_role_default}}"
>
> ...
>
> - hosts: all
> connection: local
> roles:
> - test_var_substitution
> tasks:
> - debug:
> var: testvar02
> - debug:
> var: hostvars[ansible_host].testvar02
>
> ...
>
> TASK [debug]
> ************************************************************************************************
> ok: [localhost] => {
> "testvar02": "var_in_role: define in role only"
> }
>
> ...
>
> TASK [debug]
> ************************************************************************************************
> ok: [localhost] => {
> "hostvars[ansible_host].testvar02": "var_in_role: {{var_in_role}}"
> }
Use *set_fact* when you want to keep the "current" aka "instantiated"
(I'm not sure the wording is correct) value of the variable in
hostvars, e.g.
- set_fact:
testvar02: "{{ testvar02 }}"
- debug:
var: hostvars[ansible_host].testvar02
would give
hostvars[ansible_host].testvar02: 'var_in_role: define in role only'
--
Vladimir Botka