# lineinfile module: replace multiple matching lines?

**URL:** <https://forum.ansible.com/t/lineinfile-module-replace-multiple-matching-lines/12271>\
**Category:** Ansible Project\
**Created:** [January 31, 2013, 10:37am UTC](https://forum.ansible.com/t/lineinfile-module-replace-multiple-matching-lines/12271 "2013-01-31T10:37:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arne\_Jacobs](https://avatars.discourse-cdn.com/v4/letter/a/9f8e36/32.png) [@Arne\_Jacobs](https://forum.ansible.com/u/Arne_Jacobs)\
**Post date:** [January 31, 2013, 10:37am UTC](https://forum.ansible.com/t/lineinfile-module-replace-multiple-matching-lines/12271/1 "2013-01-31T10:37:16Z")

</div>

As it is implemented now, the lineinfile module replaces only the last matching line in the given file. I guess the requirement for the _line_ argument (“Must match the value given to _regexp_”) is there to guarantee idempotence if there are multiple matching lines in the file.

If I’m not mistaken, this requirement would not be necessary if replacing ALL matching lines in the file, would it?

Are there any other reasons for the limitation to the last match?

I’m asking because I just stumbled upon a use case for both the replacement of all matching lines and the replacement line not matching the original expression - we have a file with the same key=value line occuring multiple times where the key should be renamed…

On a sidenote: would it be difficult to have the replacement line include text “captured” from the original line (via regular expression capturing groups)? (I’m a bit reluctant to start developing Ansible modules myself at this time - but I hope this will change 😉

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [January 31, 2013, 12:39pm UTC](https://forum.ansible.com/t/lineinfile-module-replace-multiple-matching-lines/12271/2 "2013-01-31T12:39:20Z")

</div>

Perhaps blasphemy, but I'd do this with a shell module call invoking  
sed, with an if statement in there with grep to achieve the  
idempotence issues. Easy global replace.

But I'm open to new flags.

---

<div class="post-metadata">

**Author:** ![Arne\_Jacobs](https://avatars.discourse-cdn.com/v4/letter/a/9f8e36/32.png) [@Arne\_Jacobs](https://forum.ansible.com/u/Arne_Jacobs)\
**Post date:** [January 31, 2013, 2:54pm UTC](https://forum.ansible.com/t/lineinfile-module-replace-multiple-matching-lines/12271/3 "2013-01-31T14:54:55Z")

</div>

> Perhaps blasphemy, but I’d do this with a shell module call invoking  
> sed, with an if statement in there with grep to achieve the  
> idempotence issues. Easy global replace.

Ok, this is what we’re doing right now (in a shell script)… just wanted to try and replace it with a dedicated module, as the lineinfile module seemed so fitting.

But well, no need to reinvent the wheel in this case I guess 🙂

---

<div class="post-metadata">

**Author:** ![John\_Wong](https://avatars.discourse-cdn.com/v4/letter/j/6f9a4e/32.png) [@John\_Wong](https://forum.ansible.com/u/John_Wong)\
**Post date:** [February 1, 2013, 6:06am UTC](https://forum.ansible.com/t/lineinfile-module-replace-multiple-matching-lines/12271/4 "2013-02-01T06:06:37Z")

</div>

Another option: use template.  
For many things I find template easier to deal with and debug because sometimes regexp can be dangerous and difficult to work with.
