# Modifying a file

**URL:** <https://forum.ansible.com/t/modifying-a-file/34506>\
**Category:** Ansible Project\
**Created:** [January 25, 2021, 6:58pm UTC](https://forum.ansible.com/t/modifying-a-file/34506 "2021-01-25T18:58:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aris\_Aguirre](https://avatars.discourse-cdn.com/v4/letter/a/d6d6ee/32.png) [@Aris\_Aguirre](https://forum.ansible.com/u/Aris_Aguirre)\
**Post date:** [January 25, 2021, 6:58pm UTC](https://forum.ansible.com/t/modifying-a-file/34506/1 "2021-01-25T18:58:45Z")

</div>

I can easily use a file module to delete the existing file and create a new one with File1 content.  
But I want to modifile the file using lineinfile or replace. Please help.  
Thanks

Existing File:  
This is an existing file / $ % with invalid characters &\*() Blahh

Expected Output:  
File1

---

<div class="post-metadata">

**Author:** ![Mauricio\_Tavares](https://avatars.discourse-cdn.com/v4/letter/m/b5e925/32.png) [@Mauricio\_Tavares](https://forum.ansible.com/u/Mauricio_Tavares)\
**Post date:** [January 25, 2021, 7:05pm UTC](https://forum.ansible.com/t/modifying-a-file/34506/2 "2021-01-25T19:05:16Z")

</div>

> I can easily use a file module to delete the existing file and create a new one with File1 content.  
> But I want to modifile the file using lineinfile or replace. Please help.  
> Thanks
> 
> Existing File:  
> This is an existing file / $ % with invalid characters &\*() Blahh
> 
> Expected Output:  
> File1

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If you can define it in regexp you can do it.

---

<div class="post-metadata">

**Author:** ![Aris\_Aguirre](https://avatars.discourse-cdn.com/v4/letter/a/d6d6ee/32.png) [@Aris\_Aguirre](https://forum.ansible.com/u/Aris_Aguirre)\
**Post date:** [January 25, 2021, 7:37pm UTC](https://forum.ansible.com/t/modifying-a-file/34506/3 "2021-01-25T19:37:31Z")

</div>

you cant…cos a lot of characters there… i cant find a way how to delete them in regex.

---

<div class="post-metadata">

**Author:** ![Antony\_Stone](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Antony\_Stone](https://forum.ansible.com/u/Antony_Stone)\
**Post date:** [January 25, 2021, 7:46pm UTC](https://forum.ansible.com/t/modifying-a-file/34506/4 "2021-01-25T19:46:01Z")

</div>

> you cant...cos a lot of characters there.. i cant find a way how to delete  
> them in regex.

Can you explain to us exactly what you want the command to identify?

Are there, for example, certain characters which should never appear in a file,  
and if they do, you want the content to be replaced?

If that's not the case, then what do you want to recognise and replace? How  
would you tell a person what to do?

---

<div class="post-metadata">

**Author:** ![nagaraju\_balusa](https://avatars.discourse-cdn.com/v4/letter/n/f07891/32.png) [@nagaraju\_balusa](https://forum.ansible.com/u/nagaraju_balusa)\
**Post date:** [January 26, 2021, 10:06am UTC](https://forum.ansible.com/t/modifying-a-file/34506/5 "2021-01-26T10:06:15Z")

</div>

please _don’t_ CC me.

---

<div class="post-metadata">

**Author:** ![Aris\_Aguirre](https://avatars.discourse-cdn.com/v4/letter/a/d6d6ee/32.png) [@Aris\_Aguirre](https://forum.ansible.com/u/Aris_Aguirre)\
**Post date:** [January 28, 2021, 5:12am UTC](https://forum.ansible.com/t/modifying-a-file/34506/6 "2021-01-28T05:12:52Z")

</div>

- name: Delete Everything in the file  
lineinfile:  
path: /tmp/filetomodify  
regexp: “\*”  
line: This is for File1

state: absent

Existing File:  
This is an existing file / $ % with invalid characters &\*() Blahh, a lot of characters here… Everything in this file should be delete and be replace by “This is for File1”

Expected Output:  
This is for File1

My solution is below. But I would like the lineinfile as solution not as below.  
Thanks

- name: Delete file  
file:  
path: /tmp/filetomodify

state: absent

- name: Add the line  
file:  
path: /tmp/filetomodify

content: This is for File1

---

<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:** [January 28, 2021, 7:43am UTC](https://forum.ansible.com/t/modifying-a-file/34506/7 "2021-01-28T07:43:33Z")

</div>

> - name: Delete Everything in the file  
> lineinfile:  
> path: /tmp/filetomodify  
> regexp: "\*"  
> line: This is for File1  
> state: absent
> 
> Existing File:  
> This is an existing file / $ % with invalid characters &\*() Blahh, a lot of characters here.. Everything in this file  
> should be delete and be replace by "This is for File1"
> 
> Expected Output:  
> This is for File1
> 
> My solution is below. But I would like the lineinfile as solution not as below.  
> Thanks

lineinfile is definitely the wrong module if you are going to update the whole file. Try copy or content module.

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

---

<div class="post-metadata">

**Author:** ![Aris\_Aguirre](https://avatars.discourse-cdn.com/v4/letter/a/d6d6ee/32.png) [@Aris\_Aguirre](https://forum.ansible.com/u/Aris_Aguirre)\
**Post date:** [January 28, 2021, 7:50pm UTC](https://forum.ansible.com/t/modifying-a-file/34506/8 "2021-01-28T19:50:54Z")

</div>

I know, lineinfile. I was thinking of file or copy module. Like just absent/present and content as argument. My question is, how about replace? you think the module replace can help?  
Thanks
