# lineinfile module to replace a string

**URL:** <https://forum.ansible.com/t/lineinfile-module-to-replace-a-string/38125>\
**Category:** Ansible Project\
**Created:** [June 19, 2023, 4:47pm UTC](https://forum.ansible.com/t/lineinfile-module-to-replace-a-string/38125 "2023-06-19T16:47:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Veera](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/veera/32/4107_2.png) [@Veera](https://forum.ansible.com/u/Veera)\
**Post date:** [June 19, 2023, 4:47pm UTC](https://forum.ansible.com/t/lineinfile-module-to-replace-a-string/38125/1 "2023-06-19T16:47:23Z")

</div>

Hi,

I am trying to replace a line/string in a 224 lines file .  
Trying with replace and lineinfile module , but seems i am missing a bit..

below is the string to consider:

logging.files:

# Configure the path where the logs are written. The default is the logs directory

# under the home path (the binary location).

path: /var/log/original

.  
Here I want to replace the " /var/log/original" to " /var/log/newfile" and I have to search based on the string “logging.files:” which is 3 lines above.

_- name: Configuring the inputs for the file_  
_replace:_  
_path: /etc/myagent/myfile_  
_after: ‘logging.files:’ here i want to search 4th line below the match.. similar to grep -A3)_  
_regexp: ’ path: /var/log/original ’_  
_replace: " path: /var/log/newfile "_

_I have multiple lines with similar entries and I cannot replace directly (based on first match too)_

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [June 19, 2023, 6:54pm UTC](https://forum.ansible.com/t/lineinfile-module-to-replace-a-string/38125/2 "2023-06-19T18:54:22Z")

</div>

What kind of file is this?  
It looks like yaml?

---

<div class="post-metadata">

**Author:** ![Veera](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/veera/32/4107_2.png) [@Veera](https://forum.ansible.com/u/Veera)\
**Post date:** [June 19, 2023, 7:36pm UTC](https://forum.ansible.com/t/lineinfile-module-to-replace-a-string/38125/3 "2023-06-19T19:36:52Z")

</div>

Yes… It’s basically another yaml file.

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [June 19, 2023, 8:15pm UTC](https://forum.ansible.com/t/lineinfile-module-to-replace-a-string/38125/4 "2023-06-19T20:15:46Z")

</div>

I’m asking because string manipulation of otherwise structured data will become messy quickly - as you have found out.  
If you must keep all the comments then you’ll have to use logic to handle that.  
Whenever someone edits the file, or by some software update, your logic might break.  
I would either template the entire thing out, based on a config that you have in your ansible code.  
Or, use a combination of slurp and write out the structure. This will remove any comments, but one could argue that that is not required anyway.  
For example:

vars:  
filename: _/etc/myagent/myfile_  
config\_opts:  
logging.files:  
path: /var/log/newfile

tasks:

- slurp:  
src: “{{ filename }}”  
register: foo

- set\_fact:  
foo: “{{ foo.content | b64decode | from\_yaml | combine(config\_opts) }}”

- copy:  
content: “{{ foo | to\_nice\_yaml }}”  
dest: “{{ filename }}”

---

<div class="post-metadata">

**Author:** ![Veera](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/veera/32/4107_2.png) [@Veera](https://forum.ansible.com/u/Veera)\
**Post date:** [June 21, 2023, 8:21am UTC](https://forum.ansible.com/t/lineinfile-module-to-replace-a-string/38125/5 "2023-06-21T08:21:27Z")

</div>

Thanks . It worked as expected
