# Save output to text file

**URL:** <https://forum.ansible.com/t/save-output-to-text-file/33382>\
**Category:** Ansible Project\
**Created:** [July 12, 2020, 10:25pm UTC](https://forum.ansible.com/t/save-output-to-text-file/33382 "2020-07-12T22:25:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Juraj\_Papic1](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@Juraj\_Papic1](https://forum.ansible.com/u/Juraj_Papic1)\
**Post date:** [July 12, 2020, 10:25pm UTC](https://forum.ansible.com/t/save-output-to-text-file/33382/1 "2020-07-12T22:25:43Z")

</div>

Hello

I have my playbook that will just execute df -h and ls -ll teh issue is that is not being able to save the output to a txt file

---

<div class="post-metadata">

**Author:** ![Luca\_remix\_tj\_Lorenz](https://avatars.discourse-cdn.com/v4/letter/l/ea666f/32.png) [@Luca\_remix\_tj\_Lorenz](https://forum.ansible.com/u/Luca_remix_tj_Lorenz)\
**Post date:** [July 13, 2020, 5:47am UTC](https://forum.ansible.com/t/save-output-to-text-file/33382/2 "2020-07-13T05:47:46Z")

</div>

Hello,

Since you’re collecting your results with a loop, you’ll have several results registered in enviro variable.

enviro.results[0].stdout will point to the stdout of the first command, and so on.

Luca

---

<div class="post-metadata">

**Author:** ![Juraj\_Papic1](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@Juraj\_Papic1](https://forum.ansible.com/u/Juraj_Papic1)\
**Post date:** [July 15, 2020, 8:36pm UTC](https://forum.ansible.com/t/save-output-to-text-file/33382/3 "2020-07-15T20:36:22Z")

</div>

Hello ,

I tried with this configuration

- name: SAVE OUTPUT TO FILE  
copy:  
content: “{{ enviro.results[0].stdout }}”  
dest: “/root/playbooks/results/{{ inventory\_hostname }}-space.txt”  
tags: save

But still having the same issue

fatal: [localhost]: FAILED! =\> {

“msg”: “The task includes an option with an undefined variable. The error was: ‘dict object’ has no attribute ‘stdout’\n\nThe error appears to be in ‘/root/playbooks/space\_mail.yml’: line 20, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: SAVE OUTPUT TO FILE\n ^ here\n”  
}

Thanks.

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [July 16, 2020, 10:50am UTC](https://forum.ansible.com/t/save-output-to-text-file/33382/4 "2020-07-16T10:50:13Z")

</div>

> Hello ,
> 
> I tried with this configuration
> 
> - name: SAVE OUTPUT TO FILE  
> copy:  
> content: "{{ enviro.results[0].stdout }}"  
> dest: "/root/playbooks/results/{{ inventory\_hostname }}-space.txt"  
> tags: save
> 
> But still having the same issue
> 
> fatal: [localhost]: FAILED! =\> {  
> "msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute  
> 'stdout'\n\nThe error appears to be in '/root/playbooks/space\_mail.yml': line 20, column 7, but may\nbe elsewhere in the  
> file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: SAVE OUTPUT TO FILE\n  
> ^ here\n"  
> }
> 
> Thanks.

This works for me:

&nbsp;&nbsp;&nbsp;&nbsp;"{{ enviro.results[0]['stdout'] }}"

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke
