> 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

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/20210401072427.1ee152ad%40gmail.com.

Attachment: pgpB0YCEIQFbJ.pgp
Description: OpenPGP digital signature

Reply via email to