Why can I debug this variable syntax but not set_fact the same variable syntax?

37 views
Skip to first unread message

burn...@umn.edu

unread,
Jul 12, 2017, 12:36:58 AM7/12/17
to Ansible Project
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

Kai Stian Olstad

unread,
Jul 12, 2017, 10:02:21 AM7/12/17
to ansible...@googlegroups.com
On 12. juli 2017 06:36, burn...@umn.edu 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

burn...@umn.edu

unread,
Jul 12, 2017, 4:57:33 PM7/12/17
to Ansible Project, ansible-pr...@olstad.com
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!
Reply all
Reply to author
Forward
0 new messages