On 12. juli 2017 06:36, [email protected] wrote:
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
   hosts: "{{ target }}"
   tasks:
   - name: Register foo
     command: 'echo Hello World'
     register: foo

- name: Different Host
   hosts: ptl01a0fap005
   tasks:
   - debug: var=hostvars[groups['{{ target }}'][0]]['foo']['stdout']

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

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


   - name: set_fact bar
     set_fact:
       bar=hostvars['{{ target }}'][0]]['foo']['stdout']

<snip />

   - name: set_fact bar
     set_fact:
       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

  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.

--
Kai Stian Olstad

--
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 post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/b0335cfc-61b9-1d8b-ccf3-f100a4d23921%40olstad.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to