# How to pass stdout of a variable belonging to different Ansible play ?

**URL:** <https://forum.ansible.com/t/how-to-pass-stdout-of-a-variable-belonging-to-different-ansible-play/31176>\
**Category:** Ansible Project\
**Created:** [August 30, 2019, 8:22am UTC](https://forum.ansible.com/t/how-to-pass-stdout-of-a-variable-belonging-to-different-ansible-play/31176 "2019-08-30T08:22:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Easy\_King](https://avatars.discourse-cdn.com/v4/letter/e/c2a13f/32.png) [@Easy\_King](https://forum.ansible.com/u/Easy_King)\
**Post date:** [August 30, 2019, 8:22am UTC](https://forum.ansible.com/t/how-to-pass-stdout-of-a-variable-belonging-to-different-ansible-play/31176/1 "2019-08-30T08:22:08Z")

</div>

How to pass “stdout” of a variable “command\_result” belonging to Ansible Play-1 to shell module of a different Play-2?

My playbook looks like below:

`

- name: Play 1  
hosts: localhost  
tasks:

- name: “Collect Output”  
command: ls -ltr \*  
register: command\_result

- name: Play 2  
hosts: remotehost  
tasks:

- shell: “~/rollback.sh {{ Number }} ‘{{ InstallDir }}’ ‘{{ hostvars["localhost"]["command\_result"].results|map(attribute=/‘stdout/’)|list }}’ \> ~/rollback.log”

`

I get runtime error executing shell which is of the nature below:

> fatal: [10.12.34.43]: FAILED! =\> {“msg”: “template error while templating string: unexpected ‘/’. String: ~/rollback.sh {{ Number }} ‘{{ InstallDir }}’ ‘{{ hostvars["localhost"]["command\_result"].results|map(attribute=/‘stdout/’)|list }}’ \> ~/rollback.log”}

I tried several tweaks but not sure how to get this to work ?

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [August 30, 2019, 8:33am UTC](https://forum.ansible.com/t/how-to-pass-stdout-of-a-variable-belonging-to-different-ansible-play/31176/2 "2019-08-30T08:33:11Z")

</div>

> How to pass "stdout" of a variable "command\_result" belonging to Ansible  
> Play-1 to shell module of a different Play-2?
> 
> My playbook looks like below:
> 
> - name: Play 1  
> &nbsp;&nbsp;hosts: localhost  
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;- name: "Collect Output"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command: ls -ltr \*  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: command\_result

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command\_result: "{{ command\_result }}"

> - name: Play 2  
> &nbsp;&nbsp;hosts: remotehost  
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;- shell: "~/rollback.sh {{ Number }} '{{ InstallDir }}' '{{  
> hostvars[\"localhost\"][\"command\_result\"].results|map(attribute=/'stdout/')|list  
> }}' \> ~/rollback.log"

Make the scope of "command\_result" playbook-wide with "set\_fact". Registered  
variables belong to the host which is running the play. i.e. "localhost" in  
your case. The second play runs "remotehost" who know nothing about varaibles  
registered by "localhost".

Cheers,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [August 30, 2019, 9:08am UTC](https://forum.ansible.com/t/how-to-pass-stdout-of-a-variable-belonging-to-different-ansible-play/31176/3 "2019-08-30T09:08:57Z")

</div>

The problem is the string is shell. Try to create the parameters first. For  
example

&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_list: "{{ hostvars['localhost'].command\_result.results|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map(attribute='stdout')|list }}"

&nbsp;&nbsp;- shell: "~/rollback.sh {{ Number }} '{{ InstallDir }}' '{{ my\_list }}' \> ~/rollback.log"

---

<div class="post-metadata">

**Author:** ![Easy\_King](https://avatars.discourse-cdn.com/v4/letter/e/c2a13f/32.png) [@Easy\_King](https://forum.ansible.com/u/Easy_King)\
**Post date:** [August 30, 2019, 9:30am UTC](https://forum.ansible.com/t/how-to-pass-stdout-of-a-variable-belonging-to-different-ansible-play/31176/4 "2019-08-30T09:30:30Z")

</div>

I tried the set\_fact suggestion but seems I need more help / understanding.

Below is the error i get:

> FAILED! =\> {“msg”: "The task includes an option with an undefined variable. The error was: ‘command\_result’ is undefined\n\nThe error appears to be in ‘/app/rollback.yml’: line 88, column 6, but may\nbe elsewhere in the file depending on the exact syntax problem.

Below is my updated playbook:

`

- name: Play 1  
hosts: localhost  
tasks:

- name: “Collect Output”  
command: ls -ltr \*  
register: command\_result

- set\_fact:  
command\_result: “{{ command\_result }}”

- name: Play 2  
hosts: remotehost  
tasks:

- shell: “~/rollback.sh {{ Number }} ‘{{ InstallDir }}’ ‘{{ command\_result.stdout }}’ \> ~/rollback.log”

`

---

<div class="post-metadata">

**Author:** ![Easy\_King](https://avatars.discourse-cdn.com/v4/letter/e/c2a13f/32.png) [@Easy\_King](https://forum.ansible.com/u/Easy_King)\
**Post date:** [August 30, 2019, 9:41am UTC](https://forum.ansible.com/t/how-to-pass-stdout-of-a-variable-belonging-to-different-ansible-play/31176/5 "2019-08-30T09:41:12Z")

</div>

The solution given with “my\_list” is complaining of the below error:

> FAILED! =\> {“msg”: "The task includes an option with an undefined variable. The error was: ‘dict object’ has no attribute ‘results’\n\nThe error appears to be in

Guess we need to relook at this proposed code snippet:

`my_list: "{{ hostvars['localhost'].command_result.results|map(attribute='stdout')|list }}"`

Something is still wrong.

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [August 31, 2019, 8:24am UTC](https://forum.ansible.com/t/how-to-pass-stdout-of-a-variable-belonging-to-different-ansible-play/31176/6 "2019-08-31T08:24:03Z")

</div>

[SOLVED]

The playbook below

- hosts: localhost  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Number: '42'  
&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;InstallDir: '/usr/local'  
&nbsp;&nbsp;&nbsp;&nbsp;- name: 'Collect Output'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command: "sh -c 'ls -1 /scratch/tmp/\*.db'"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: result

- hosts: test\_01  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;My\_list: "{{ hostvars['localhost'].result.stdout\_lines|join(',') }}"  
&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Number: "{{ hostvars['localhost'].Number }}"  
&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;InstallDir: "{{ hostvars['localhost'].InstallDir }}"  
&nbsp;&nbsp;&nbsp;&nbsp;- shell: "/root/rollback.sh {{ Number }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ InstallDir }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ My\_list }} \> /root/rollback.log"  
...

With the script

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;root@test\_01:~ # cat /root/rollback.sh  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#!/usr/local/bin/bash  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo $1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo $2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo $3

gives

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;root@test\_01:~ # cat /root/rollback.log  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;42  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/usr/local  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/scratch/tmp/test1.db,/scratch/tmp/test2.db,/scratch/tmp/test3.db
