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:*
---
- 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']

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

  - debug: var=bar

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

  - debug: var=bar

*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['Test5'][0]]['foo']['stdout']": "Hello World"
}

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

TASK [debug] 
*******************************************************************
ok: [ptl01a0fap005] => {
    "bar": "hostvars['Test5'][0]]['foo']['stdout']"
}

TASK [set_fact bar] 
************************************************************
fatal: [ptl01a0fap005]: FAILED! => {"failed": true, "msg": "template error 
while templating string: unexpected ']'. String: {{ hostvars['{{ target 
}}'][0]]['foo']['stdout'] }}"}
        to retry, use: --limit 
@/staging_manh/manhattanansible/pb-output-test.retry

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

wmspt@dtl01lnxap01a:/staging_manh/manhattanansible$ 


Thank you

-- 
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/2a643cd8-d9bb-430b-8c64-58c6846ae84a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to