# Why can I debug this variable syntax but not set\_fact the same variable syntax?

**URL:** <https://forum.ansible.com/t/why-can-i-debug-this-variable-syntax-but-not-set-fact-the-same-variable-syntax/24959>\
**Category:** Ansible Project\
**Created:** [July 12, 2017, 4:36am UTC](https://forum.ansible.com/t/why-can-i-debug-this-variable-syntax-but-not-set-fact-the-same-variable-syntax/24959 "2017-07-12T04:36:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![burns498](https://avatars.discourse-cdn.com/v4/letter/b/b487fb/32.png) [@burns498](https://forum.ansible.com/u/burns498)\
**Post date:** [July 12, 2017, 4:36am UTC](https://forum.ansible.com/t/why-can-i-debug-this-variable-syntax-but-not-set-fact-the-same-variable-syntax/24959/1 "2017-07-12T04:36:58Z")

</div>

Hello - I’m trying to register a variable on dynamic host {{ target }} and use it on a different host ptl01a0fap005. The dynamic host {{ target }} is passed in as an extra-var on the command line.

I’ve found that I can successfully debug the variable I want on the different host, in green below. However, if I try to set\_fact the with the same syntax it fails. The last attempt would normally work, but in this case doesn’t due to the nested variable.

Why does this syntax work for debug but not set\_fact? How can I actually use the debug output on the other host?

**Playbook:**

`

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 12, 2017, 2:01pm UTC](https://forum.ansible.com/t/why-can-i-debug-this-variable-syntax-but-not-set-fact-the-same-variable-syntax/24959/2 "2017-07-12T14:01:42Z")

</div>

> Why does this syntax work for debug but not set\_fact? How can I actually  
> use the debug output on the other host?

Pure luck that the debug work and you yaml syntax is wrong of set\_fact.

> \*Playbook:\*  
> ---  
> - name: Target Host  
> &nbsp;&nbsp;&nbsp;hosts: "{{ target }}"  
> &nbsp;&nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;- name: Register foo  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command: 'echo Hello World'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: foo
> 
> - name: Different Host  
> &nbsp;&nbsp;&nbsp;hosts: ptl01a0fap005  
> &nbsp;&nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;- debug: var=hostvars[groups['{{ target }}'][0]]['foo']['stdout']

You should not use {{ }} and ', correct syntax is

&nbsp;&nbsp;&nbsp;- debug: var=hostvars[groups[target][0]]['foo']['stdout']

> &nbsp;&nbsp;&nbsp;- name: set\_fact bar  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set\_fact:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bar=hostvars['{{ target }}'][0]]['foo']['stdout']

\<snip /\>

> &nbsp;&nbsp;&nbsp;- name: set\_fact bar  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set\_fact:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bar="{{ hostvars['{{ target }}'][0]]['foo']['stdout'] }}"

This is wrong yaml syntax.

bar= should be bar:

And also here you can't use {{ }} inside a template, and a '' indicate a string not a variable.

Correct syntax is

&nbsp;&nbsp;&nbsp;bar: "{{ hostvars[target][0]]['foo']['stdout'] }}"

But I guess this will also fail since target=Test5 that is a group, so it will not find hostvars for a host called Test5. Maybe you forgot to include the group as you did in debug task.

---

<div class="post-metadata">

**Author:** ![burns498](https://avatars.discourse-cdn.com/v4/letter/b/b487fb/32.png) [@burns498](https://forum.ansible.com/u/burns498)\
**Post date:** [July 12, 2017, 8:57pm UTC](https://forum.ansible.com/t/why-can-i-debug-this-variable-syntax-but-not-set-fact-the-same-variable-syntax/24959/3 "2017-07-12T20:57:33Z")

</div>

Kai - Thank you very much. Everything you mentioned was exactly right. Helped me learn a thing or two about proper syntax…and reminding me not to code too late into the evening. haha.

Final Playbook:

`
