# emptying a file or replacing all its contents

**URL:** <https://forum.ansible.com/t/emptying-a-file-or-replacing-all-its-contents/22828>\
**Category:** Ansible Project\
**Created:** [October 19, 2016, 12:06pm UTC](https://forum.ansible.com/t/emptying-a-file-or-replacing-all-its-contents/22828 "2016-10-19T12:06:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alix\_Ak](https://avatars.discourse-cdn.com/v4/letter/a/e9c0ed/32.png) [@Alix\_Ak](https://forum.ansible.com/u/Alix_Ak)\
**Post date:** [October 19, 2016, 12:06pm UTC](https://forum.ansible.com/t/emptying-a-file-or-replacing-all-its-contents/22828/1 "2016-10-19T12:06:16Z")

</div>

Hallo,

in bash i use echo command to empty a file all replace it’s contents. I couldn’t figure this out using file or lineinfile modules.  
Using shell command, i get another server state since ansible states change although i echo the same line.  
Does anybody has an Idea ?

---

<div class="post-metadata">

**Author:** ![Josh\_Smift1](https://avatars.discourse-cdn.com/v4/letter/j/bcef8e/32.png) [@Josh\_Smift1](https://forum.ansible.com/u/Josh_Smift1)\
**Post date:** [October 19, 2016, 1:32pm UTC](https://forum.ansible.com/t/emptying-a-file-or-replacing-all-its-contents/22828/2 "2016-10-19T13:32:34Z")

</div>

> in bash i use echo command to empty a file all replace it's contents.  
> I couldn't figure this out using file or lineinfile modules. Using  
> shell command, i get another server state since ansible states change  
> although i echo the same line.

You just want to replace a file with an empty file? /dev/null is "an empty  
file" that you can copy:

&nbsp;&nbsp;- copy: src=/dev/null dest=/tmp/empty-file

That seems to work for me. (Reports 'changed' if the file wasn't already  
empty, 'ok' if the file was already empty.)

&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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-Josh (jbs@care.com)

(apologies for the automatic corporate disclaimer that follows)  
This email is intended for the person(s) to whom it is addressed and may contain information that is PRIVILEGED or CONFIDENTIAL. Any unauthorized use, distribution, copying, or disclosure by any person other than the addressee(s) is strictly prohibited. If you have received this email in error, please notify the sender immediately by return email and delete the message and any attachments from your system.
