# Lineinfile - replacing regex instead of placing text after insertafter

**URL:** <https://forum.ansible.com/t/lineinfile-replacing-regex-instead-of-placing-text-after-insertafter/24586>\
**Category:** Ansible Project\
**Created:** [May 28, 2017, 9:53am UTC](https://forum.ansible.com/t/lineinfile-replacing-regex-instead-of-placing-text-after-insertafter/24586 "2017-05-28T09:53:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Listing](https://avatars.discourse-cdn.com/v4/letter/l/3d9bf3/32.png) [@Listing](https://forum.ansible.com/u/Listing)\
**Post date:** [May 28, 2017, 9:53am UTC](https://forum.ansible.com/t/lineinfile-replacing-regex-instead-of-placing-text-after-insertafter/24586/1 "2017-05-28T09:53:05Z")

</div>

Hi guys,

I have some problems with the lineinfile module. I try to add ‘inserted\_text’ after ‘#insertbelow’ when ‘this’ has been found in /etc/ansible/lineinfile/lol. This is my task:

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [May 28, 2017, 5:38pm UTC](https://forum.ansible.com/t/lineinfile-replacing-regex-instead-of-placing-text-after-insertafter/24586/2 "2017-05-28T17:38:08Z")

</div>

That is not how lineinfile work.  
It searches for a string, and replace it or add the line.

To do this you would need to combine find module to search for "this" and then run lineinfile on the file.

---

<div class="post-metadata">

**Author:** ![Listing](https://avatars.discourse-cdn.com/v4/letter/l/3d9bf3/32.png) [@Listing](https://forum.ansible.com/u/Listing)\
**Post date:** [May 29, 2017, 2:26pm UTC](https://forum.ansible.com/t/lineinfile-replacing-regex-instead-of-placing-text-after-insertafter/24586/3 "2017-05-29T14:26:53Z")

</div>

Hi Kai,

Thank you for your reply. But what is the difference then between regexp and insertafter etc? Is regexp always being replaced? Or what is exactly the logic?

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [May 30, 2017, 7:02am UTC](https://forum.ansible.com/t/lineinfile-replacing-regex-instead-of-placing-text-after-insertafter/24586/4 "2017-05-30T07:02:21Z")

</div>

regexp: and insertafter: is separate features in lineinfile.

When lineinfile find the regexp in regexp: it replaces this line with the string in line:  
If it can't find the regexp in regexp: the sting in line: is append to end of file(EOF).

When lineinfile find the regexp in insertafter: it add the string in line: after this line, if it's not already exist.  
If it can't find the regexp in insertafter: the string in line: is append to end of file(EOF).

These two feature is not meant to be combine in one task, but used in separate tasks.

---

<div class="post-metadata">

**Author:** ![Listing](https://avatars.discourse-cdn.com/v4/letter/l/3d9bf3/32.png) [@Listing](https://forum.ansible.com/u/Listing)\
**Post date:** [May 30, 2017, 11:33am UTC](https://forum.ansible.com/t/lineinfile-replacing-regex-instead-of-placing-text-after-insertafter/24586/5 "2017-05-30T11:33:23Z")

</div>

Very clear, thank you!

---

<div class="post-metadata">

**Author:** ![Listing](https://avatars.discourse-cdn.com/v4/letter/l/3d9bf3/32.png) [@Listing](https://forum.ansible.com/u/Listing)\
**Post date:** [July 15, 2017, 3:19pm UTC](https://forum.ansible.com/t/lineinfile-replacing-regex-instead-of-placing-text-after-insertafter/24586/6 "2017-07-15T15:19:30Z")

</div>

Hi Kai,

When looking in ansible-doc lineinfile, it does combine both regexp and insertafter in one task:

- lineinfile:  
path: /etc/httpd/conf/httpd.conf  
regexp: '^Listen ’  
insertafter: '^#Listen ’  
line: 'Listen 8080

Is this an error in the docs or are we (I?) missing something?

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 22, 2017, 1:14pm UTC](https://forum.ansible.com/t/lineinfile-replacing-regex-instead-of-placing-text-after-insertafter/24586/7 "2017-07-22T13:14:29Z")

</div>

If you remove the regexp line it will work exactly the same.
