# Ansible retrieve stdout with items loop

**URL:** <https://forum.ansible.com/t/ansible-retrieve-stdout-with-items-loop/30117>\
**Category:** Ansible Project\
**Created:** [April 16, 2019, 6:53am UTC](https://forum.ansible.com/t/ansible-retrieve-stdout-with-items-loop/30117 "2019-04-16T06:53:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![wanexa77](https://avatars.discourse-cdn.com/v4/letter/w/eb8c5e/32.png) [@wanexa77](https://forum.ansible.com/u/wanexa77)\
**Post date:** [April 16, 2019, 6:53am UTC](https://forum.ansible.com/t/ansible-retrieve-stdout-with-items-loop/30117/1 "2019-04-16T06:53:30Z")

</div>

Hi All

I want to retrieve multiple output from a command with a with items loop  
the playbook is like this

- name: test  
command: openssl rand -base64 20  
register: pwd  
delegate\_to: localhost  
with\_items:
- test1
- test

the result is like this with -vvv command

changed: [127.0.0.1 → localhost] =\> (item=test) =\> {  
“changed”: true,  
“cmd”: [  
“openssl”,  
“rand”,  
“-base64”,  
“20”  
],  
“delta”: “0:00:00.008819”,  
“end”: “2019-04-16 08:47:32.782792”,  
“invocation”: {  
“module\_args”: {  
“\_raw\_params”: “openssl rand -base64 20”,  
“\_uses\_shell”: false,  
“argv”: null,  
“chdir”: null,  
“creates”: null,  
“executable”: null,  
“removes”: null,  
“stdin”: null,  
“warn”: true  
}  
},  
“item”: “test”,  
“rc”: 0,  
“start”: “2019-04-16 08:47:32.773973”,  
“stderr”: “”,  
“stderr\_lines”: ,  
“stdout”: “IWRXhTs1Q9dKxIKgaXXkYoYwn0s=”,  
“stdout\_lines”: [  
“IWRXhTs1Q9dKxIKgaXXkYoYwn0s=”  
]  
}

I just want the stdout lines per items (test and test1 ) and register those last as variables for other command like this one

- name: Test2  
command: “echo {{ item.stdout }} \> /tmp/test”  
with\_items: “{{ pwd.results }}”

Can t get rid of it this register the whole output with at the end my desired variable, how to do this ?

ok: [127.0.0.1] =\> (item={‘\_ansible\_parsed’: True, ‘stderr\_lines’: , u’changed’: True, u’stdout’: u’Fkm9ulL1kMf9liKquTdECo6UynI=‘, ‘\_ansible\_delegated\_vars’: {‘ansible\_delegated\_host’: u’localhost’, ‘ansible\_host’: u’localhost’}, ‘\_ansible\_item\_result’: True, u’delta’: u’0:00:00.009828’, ‘stdout\_lines’: [u’Fkm9ulL1kMf9liKquTdECo6UynI=‘], ‘\_ansible\_item\_label’: u’test1’, u’end’: u’2019-04-16 08:52:09.797091’, ‘\_ansible\_no\_log’: False, ‘failed’: False, u’cmd’: [u’openssl’, u’rand’, u’-base64’, u’20’], ‘item’: u’test1’, u’stderr’: u’‘, u’rc’: 0, u’invocation’: {u’module\_args’: {u’warn’: True, u’executable’: None, u’\_uses\_shell’: False, u’\_raw\_params’: u’openssl rand -base64 20’, u’removes’: None, u’argv’: None, u’creates’: None, u’chdir’: None, u’stdin’: None}}, u’start’: u’2019-04-16 08:52:09.787263’, ‘\_ansible\_ignore\_errors’: None}) =\> {  
“item”: {  
“changed”: true,  
“cmd”: [  
“openssl”,  
“rand”,  
“-base64”,  
“20”  
],  
“delta”: “0:00:00.009828”,  
“end”: “2019-04-16 08:52:09.797091”,  
“failed”: false,  
“invocation”: {  
“module\_args”: {  
“\_raw\_params”: “openssl rand -base64 20”,  
“\_uses\_shell”: false,  
“argv”: null,  
“chdir”: null,  
“creates”: null,  
“executable”: null,  
“removes”: null,  
“stdin”: null,  
“warn”: true  
}  
},  
“item”: “test1”,  
“rc”: 0,  
“start”: “2019-04-16 08:52:09.787263”,  
“stderr”: “”,  
“stderr\_lines”: ,  
“stdout”: “Fkm9ulL1kMf9liKquTdECo6UynI=”,  
“stdout\_lines”: [  
“Fkm9ulL1kMf9liKquTdECo6UynI=”  
]  
},  
“item.stdout”: “Fkm9ulL1kMf9liKquTdECo6UynI=”  
}  
ok: [127.0.0.1] =\> (item={‘\_ansible\_parsed’: True, ‘stderr\_lines’: , u’changed’: True, u’stdout’: u’rQkmbSbF67S0RFKyOGq5NzzDy4o=‘, ‘\_ansible\_delegated\_vars’: {‘ansible\_delegated\_host’: u’localhost’, ‘ansible\_host’: u’localhost’}, ‘\_ansible\_item\_result’: True, u’delta’: u’0:00:00.009044’, ‘stdout\_lines’: [u’rQkmbSbF67S0RFKyOGq5NzzDy4o=‘], ‘\_ansible\_item\_label’: u’test’, u’end’: u’2019-04-16 08:52:10.020054’, ‘\_ansible\_no\_log’: False, ‘failed’: False, u’cmd’: [u’openssl’, u’rand’, u’-base64’, u’20’], ‘item’: u’test’, u’stderr’: u’‘, u’rc’: 0, u’invocation’: {u’module\_args’: {u’warn’: True, u’executable’: None, u’\_uses\_shell’: False, u’\_raw\_params’: u’openssl rand -base64 20’, u’removes’: None, u’argv’: None, u’creates’: None, u’chdir’: None, u’stdin’: None}}, u’start’: u’2019-04-16 08:52:10.011010’, ‘\_ansible\_ignore\_errors’: None}) =\> {  
“item”: {  
“changed”: true,  
“cmd”: [  
“openssl”,  
“rand”,  
“-base64”,  
“20”  
],  
“delta”: “0:00:00.009044”,  
“end”: “2019-04-16 08:52:10.020054”,  
“failed”: false,  
“invocation”: {  
“module\_args”: {  
“\_raw\_params”: “openssl rand -base64 20”,  
“\_uses\_shell”: false,  
“argv”: null,  
“chdir”: null,  
“creates”: null,  
“executable”: null,  
“removes”: null,  
“stdin”: null,  
“warn”: true  
}  
},  
“item”: “test”,  
“rc”: 0,  
“start”: “2019-04-16 08:52:10.011010”,  
“stderr”: “”,  
“stderr\_lines”: ,  
“stdout”: “rQkmbSbF67S0RFKyOGq5NzzDy4o=”,  
“stdout\_lines”: [  
“rQkmbSbF67S0RFKyOGq5NzzDy4o=”  
]  
},  
“item.stdout”: “rQkmbSbF67S0RFKyOGq5NzzDy4o=”  
}

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [April 16, 2019, 7:36am UTC](https://forum.ansible.com/t/ansible-retrieve-stdout-with-items-loop/30117/2 "2019-04-16T07:36:34Z")

</div>

Looking at the two tasks and what you seem to be trying to achieve, you could probably just use a password lookup to generate it:

[https://docs.ansible.com/ansible/latest/plugins/lookup/password.html](https://docs.ansible.com/ansible/latest/plugins/lookup/password.html)

Dick

---

<div class="post-metadata">

**Author:** ![wanexa77](https://avatars.discourse-cdn.com/v4/letter/w/eb8c5e/32.png) [@wanexa77](https://forum.ansible.com/u/wanexa77)\
**Post date:** [April 16, 2019, 9:30am UTC](https://forum.ansible.com/t/ansible-retrieve-stdout-with-items-loop/30117/3 "2019-04-16T09:30:29Z")

</div>

You re right thanks

Jérome Denéchaud

> **From:** [dick.visser@geant.org](mailto:dick.visser@geant.org)  
> **Sent:** April 16, 2019 09:36  
> **To:** [ansible-project@googlegroups.com](mailto:ansible-project@googlegroups.com)  
> **Reply-to:** [ansible-project@googlegroups.com](mailto:ansible-project@googlegroups.com)  
> **Subject:** Re: [ansible-project] Ansible retrieve stdout with items loop  
>   
> |
> 
> - |

Looking at the two tasks and what you seem to be trying to achieve, you could probably just use a password lookup to generate it:

[https://docs.ansible.com/ansible/latest/plugins/lookup/password.html](https://docs.ansible.com/ansible/latest/plugins/lookup/password.html)

Dick
