# Saving output of commands passed through 'with\_items' into a single file

**URL:** <https://forum.ansible.com/t/saving-output-of-commands-passed-through-with-items-into-a-single-file/28107>\
**Category:** Ansible Developer\
**Created:** [July 20, 2018, 6:46pm UTC](https://forum.ansible.com/t/saving-output-of-commands-passed-through-with-items-into-a-single-file/28107 "2018-07-20T18:46:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Soham\_Chakraborty](https://avatars.discourse-cdn.com/v4/letter/s/9f8e36/32.png) [@Soham\_Chakraborty](https://forum.ansible.com/u/Soham_Chakraborty)\
**Post date:** [July 20, 2018, 6:46pm UTC](https://forum.ansible.com/t/saving-output-of-commands-passed-through-with-items-into-a-single-file/28107/1 "2018-07-20T18:46:09Z")

</div>

Hi all,

I am trying to run few commands and save output of those commands into a single file for checking later.

So far, I have come up with the following playbook:

---

<div class="post-metadata">

**Author:** ![Marcin\_Dulak](https://avatars.discourse-cdn.com/v4/letter/m/ecc23a/32.png) [@Marcin\_Dulak](https://forum.ansible.com/u/Marcin_Dulak)\
**Post date:** [July 20, 2018, 7:50pm UTC](https://forum.ansible.com/t/saving-output-of-commands-passed-through-with-items-into-a-single-file/28107/2 "2018-07-20T19:50:37Z")

</div>

`

`

Since you are saving to a file, maybe just save the output directly.

`

- hosts: localhost  
tasks:
- name: Clear the file  
shell: “echo \> info.txt”
- name: Capture some information about a host  
shell: “echo shell: ‘{{ item }}’ \>\> info.txt 2\>&1 && {{ item }} \>\> info.txt 2\>&1”  
with\_items:
- echo `date +"%Y-%m-%d %T"` info
- ls wrong
- echo `date +"%Y-%m-%d %T"` info
- echo test  
ignore\_errors: yes

`

`

shell: echo `date +"%Y-%m-%d %T"` info  
2018-07-20 21:48:45 info  
shell: ls wrong  
ls: cannot access ‘wrong’: No such file or directory  
shell: echo `date +"%Y-%m-%d %T"` info  
2018-07-20 21:48:45 info  
shell: echo test  
test

`

---

<div class="post-metadata">

**Author:** ![Ed\_C](https://avatars.discourse-cdn.com/v4/letter/e/c67d28/32.png) [@Ed\_C](https://forum.ansible.com/u/Ed_C)\
**Post date:** [July 20, 2018, 8:49pm UTC](https://forum.ansible.com/t/saving-output-of-commands-passed-through-with-items-into-a-single-file/28107/3 "2018-07-20T20:49:58Z")

</div>

Alternatively you can actually do an inline Jina loop. e.g.

- name: Save the output  
local\_action:  
module: copy  
content: |  
{% for item in result.results %}  
{{ item.stdout }}  
{% endfor %}  
dest: /root/info.txt

---

<div class="post-metadata">

**Author:** ![Soham\_Chakraborty](https://avatars.discourse-cdn.com/v4/letter/s/9f8e36/32.png) [@Soham\_Chakraborty](https://forum.ansible.com/u/Soham_Chakraborty)\
**Post date:** [July 21, 2018, 4:29pm UTC](https://forum.ansible.com/t/saving-output-of-commands-passed-through-with-items-into-a-single-file/28107/4 "2018-07-21T16:29:22Z")

</div>

Hi Marcin,

Your solution works perfectly. I modified it a bit to suit my needs. So thank you very much.

Hi Ed,

I tried your solution but it is giving me a syntax error that I am not sure how to approach:

---

<div class="post-metadata">

**Author:** ![Abhijit\_Menon-Sen1](https://avatars.discourse-cdn.com/v4/letter/a/8e7dd6/32.png) [@Abhijit\_Menon-Sen1](https://forum.ansible.com/u/Abhijit_Menon-Sen1)\
**Post date:** [July 21, 2018, 4:32pm UTC](https://forum.ansible.com/t/saving-output-of-commands-passed-through-with-items-into-a-single-file/28107/5 "2018-07-21T16:32:26Z")

</div>

> From your mail:

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;content: | …  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest=/root/info.txt

And from the mail you quoted:

> > content: | …  
> > dest: /root/info.txt

-- Abhijit

---

<div class="post-metadata">

**Author:** ![Soham\_Chakraborty](https://avatars.discourse-cdn.com/v4/letter/s/9f8e36/32.png) [@Soham\_Chakraborty](https://forum.ansible.com/u/Soham_Chakraborty)\
**Post date:** [July 21, 2018, 4:35pm UTC](https://forum.ansible.com/t/saving-output-of-commands-passed-through-with-items-into-a-single-file/28107/6 "2018-07-21T16:35:31Z")

</div>

Oh wow,

/me hides face.

Thank you 🙂
