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:
---
- 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']
  - debug: var=hostvars[groups[target][0]]['foo']['stdout']

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

  - debug: var=bar

Final Output:
wmspt@dtl01lnxap01a:/staging_manh/manhattanansible$ ansible-playbook 
pb-output-test.yml -e  "target=Test5" -i 'inventories/staging/inventory' 

PLAY [Target Host] 
*************************************************************

TASK [setup] 
*******************************************************************
ok: [ptl01a0fap006]

TASK [Register foo] 
************************************************************
changed: [ptl01a0fap006]

PLAY [Different Host] 
**********************************************************

TASK [setup] 
*******************************************************************
ok: [ptl01a0fap005]

TASK [debug] 
*******************************************************************
ok: [ptl01a0fap005] => {
    "hostvars[groups[target][0]]['foo']['stdout']": "Hello World"
}

TASK [set_fact bar] 
************************************************************
ok: [ptl01a0fap005]

TASK [debug] 
*******************************************************************
ok: [ptl01a0fap005] => {
    "bar": "Hello World"
}

PLAY RECAP 
*********************************************************************
ptl01a0fap005              : ok=4    changed=0    unreachable=0    failed=0 
  
ptl01a0fap006              : ok=2    changed=1    unreachable=0    failed=0 
  

wmspt@dtl01lnxap01a:/staging_manh/manhattanansible$ 

Thank you!

On Wednesday, July 12, 2017 at 9:02:21 AM UTC-5, Kai Stian Olstad wrote:
>
> On 12. juli 2017 06:36, [email protected] <javascript:> 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/d8d27d10-96e0-42dd-9cbd-7023f7b53ef1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to