# Variable resolution issue, bug or feature?

**URL:** <https://forum.ansible.com/t/variable-resolution-issue-bug-or-feature/31242>\
**Category:** Ansible Project\
**Created:** [September 9, 2019, 4:33pm UTC](https://forum.ansible.com/t/variable-resolution-issue-bug-or-feature/31242 "2019-09-09T16:33:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mcronenworth](https://avatars.discourse-cdn.com/v4/letter/m/f19dbf/32.png) [@mcronenworth](https://forum.ansible.com/u/mcronenworth)\
**Post date:** [September 9, 2019, 4:33pm UTC](https://forum.ansible.com/t/variable-resolution-issue-bug-or-feature/31242/1 "2019-09-09T16:33:06Z")

</div>

Hi,

I have a role that when it runs is only partially resolving variables.

Variable\_A: defined in defaults - string

Variable\_B: defined in defaults - hostvars reference to Variable\_C that will be loaded through include\_vars

Variable\_C: defined in file and loaded through include\_vars - string to reference Variable\_A

The role task is to use Variable\_B. When the role runs the task ends up being set to “{{ Variable \_A }}” instead of the string like Ansible stopped resolving variables.

If I switch the role to use a Variable\_A loaded through include\_vars instead Ansible resolves the variable completely.

Is this expected or is it a bug?

Thanks,  
Michael

---

<div class="post-metadata">

**Author:** ![mcronenworth](https://avatars.discourse-cdn.com/v4/letter/m/f19dbf/32.png) [@mcronenworth](https://forum.ansible.com/u/mcronenworth)\
**Post date:** [September 14, 2019, 12:40am UTC](https://forum.ansible.com/t/variable-resolution-issue-bug-or-feature/31242/2 "2019-09-14T00:40:19Z")

</div>

Anyone? I’m going to file a bug next week if there is no other comment.

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [September 14, 2019, 4:15am UTC](https://forum.ansible.com/t/variable-resolution-issue-bug-or-feature/31242/3 "2019-09-14T04:15:55Z")

</div>

Hi,

> I have a role that when it runs is only partially resolving variables.
> 
> Variable\_A: defined in defaults - string  
> Variable\_B: defined in defaults - hostvars reference to Variable\_C that  
> will be loaded through include\_vars  
> Variable\_C: defined in file and loaded through include\_vars - string to  
> reference Variable\_A
> 
> The role task is to use Variable\_B. When the role runs the task ends up  
> being set to "{{ Variable \_A }}" instead of the string like Ansible stopped  
> resolving variables.
> 
> If I switch the role to use a Variable\_A loaded through include\_vars  
> instead Ansible resolves the variable completely.
> 
> Is this expected or is it a bug?

You should post at least the declaration of the variables. "Variable\_A" and  
"Variable\_C" is clear. But what do you mean with "defined in defaults -  
hostvar reference" ?

&nbsp;&nbsp;"Variable\_B: defined in defaults - hostvars reference to Variable\_C"

It would be just guessing without complete, minimal, reproducible example.

Cheers,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Michael\_Cronenworth](https://avatars.discourse-cdn.com/v4/letter/m/74df32/32.png) [@Michael\_Cronenworth](https://forum.ansible.com/u/Michael_Cronenworth)\
**Post date:** [September 14, 2019, 2:05pm UTC](https://forum.ansible.com/t/variable-resolution-issue-bug-or-feature/31242/4 "2019-09-14T14:05:55Z")

</div>

git clone [https://github.com/mooninite/ansible-test](https://github.com/mooninite/ansible-test)  
cd ansible-test  
ansible-playbook ./site.yml

Thanks,  
Michael

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [September 14, 2019, 3:27pm UTC](https://forum.ansible.com/t/variable-resolution-issue-bug-or-feature/31242/5 "2019-09-14T15:27:23Z")

</div>

It's neither a bug nor a feature. It's all by design, I think.

"include\_vars" does not evaluate "Variable\_C". This complies with "lazy  
evaluation" when "Ansible evaluates any variables in playbook content at the  
last possible second".  
[https://docs.ansible.com/ansible/latest/reference\_appendices/glossary.html](https://docs.ansible.com/ansible/latest/reference_appendices/glossary.html)

&nbsp;&nbsp;&nbsp;&nbsp;"hostvars.localhost": {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Variable\_C": "{{ Variable\_A }}",  
&nbsp;&nbsp;&nbsp;&nbsp;...

The apparent problem comes from the difference how the module "debug"  
evaluates the parameters "msg" and "var". The example below

&nbsp;&nbsp;&nbsp;&nbsp;$ cat roles/test/tasks/main.yml  
&nbsp;&nbsp;&nbsp;&nbsp;- include\_vars: test.yml  
&nbsp;&nbsp;&nbsp;&nbsp;- debug: msg="{{ hostvars[inventory\_hostname].Variable\_C }}"  
&nbsp;&nbsp;&nbsp;&nbsp;- debug: var="{{ hostvars[inventory\_hostname].Variable\_C }}"  
&nbsp;&nbsp;&nbsp;&nbsp;- debug: msg="{{ Variable\_B }}"  
&nbsp;&nbsp;&nbsp;&nbsp;- debug: var="{{ Variable\_B }}"

gives

&nbsp;&nbsp;&nbsp;&nbsp;"msg": "{{ Variable\_A }}"  
&nbsp;&nbsp;&nbsp;&nbsp;"{{ Variable\_A }}": "a string"  
&nbsp;&nbsp;&nbsp;&nbsp;"msg": "{{ Variable\_A }}"  
&nbsp;&nbsp;&nbsp;&nbsp;"{{ Variable\_A }}": "a string"

Testing with the module "set\_fact" shows that all is working as expected. The  
example below

&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_list: "{{ my\_list|default() +  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[Variable\_B] +  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[hostvars[inventory\_hostname].Variable\_C ] }}"  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: my\_list

gives

&nbsp;&nbsp;&nbsp;&nbsp;"my\_list": [  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"a string"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"a string"  
&nbsp;&nbsp;&nbsp;&nbsp;]

HTH. Cheers,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [September 14, 2019, 4:37pm UTC](https://forum.ansible.com/t/variable-resolution-issue-bug-or-feature/31242/6 "2019-09-14T16:37:24Z")

</div>

It's closed issue (locked and limited conversation to collaborators on Jul 25)  
Hostvars of hostvars aren't evaluated #16351  
[https://github.com/ansible/ansible/issues/16351](https://github.com/ansible/ansible/issues/16351)

- debug: msg="{{ hostvars[... its not recursively evaluating

HTH, Cheers,

&nbsp;&nbsp;-vlado
