# Recommended way to regex replace multiple lines in a configuration file

**URL:** <https://forum.ansible.com/t/recommended-way-to-regex-replace-multiple-lines-in-a-configuration-file/14264>\
**Category:** Ansible Project\
**Created:** [January 4, 2014, 6:04am UTC](https://forum.ansible.com/t/recommended-way-to-regex-replace-multiple-lines-in-a-configuration-file/14264 "2014-01-04T06:04:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![eskhool](https://avatars.discourse-cdn.com/v4/letter/e/f14d63/32.png) [@eskhool](https://forum.ansible.com/u/eskhool)\
**Post date:** [January 4, 2014, 6:04am UTC](https://forum.ansible.com/t/recommended-way-to-regex-replace-multiple-lines-in-a-configuration-file/14264/1 "2014-01-04T06:04:43Z")

</div>

Hi,  
If we don’t have the option to use templates as the file may vary across system, is there a way to find and replace multiple lines based on a regex…something like an option in lineinfile such as ‘all’. What other mechanism can be recommended? Any reason to not have this option? And if contributed will such a change be accepted in ansible core?

Thanks

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [January 4, 2014, 4:32pm UTC](https://forum.ansible.com/t/recommended-way-to-regex-replace-multiple-lines-in-a-configuration-file/14264/2 "2014-01-04T16:32:57Z")

</div>

It seems you would just use the shell module and invoke sed.

---

<div class="post-metadata">

**Author:** ![PhilEv](https://avatars.discourse-cdn.com/v4/letter/p/ee59a6/32.png) [@PhilEv](https://forum.ansible.com/u/PhilEv)\
**Post date:** [January 4, 2014, 5:55pm UTC](https://forum.ansible.com/t/recommended-way-to-regex-replace-multiple-lines-in-a-configuration-file/14264/3 "2014-01-04T17:55:24Z")

</div>

another way not as optimal as using sed like proposed by Michael is  
to do it via 2 tasks.

tasks 1: count the number of lines to replace (via shell module using and grep -E | wc -l )  
using register to remember the linecounted

task 2: using lineinfile and with\_sequence: linecounted

one caveat is that with sequence does not accept a 0 count ( a potential future improvement ?)

so just add 1 to the count (you do not care about doing it one more time)

then you can run those 2 tasks multiples times

works for me on grub.conf

---

<div class="post-metadata">

**Author:** ![eskhool](https://avatars.discourse-cdn.com/v4/letter/e/f14d63/32.png) [@eskhool](https://forum.ansible.com/u/eskhool)\
**Post date:** [June 10, 2014, 4:22pm UTC](https://forum.ansible.com/t/recommended-way-to-regex-replace-multiple-lines-in-a-configuration-file/14264/4 "2014-06-10T16:22:11Z")

</div>

Sorry to dig out an old post but Michael would an enhancement to the regex plugin to do multi line be accepted? Seems like pretty useful functionality and the re module in python has a find\_iter for just the task…

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [June 11, 2014, 10:34pm UTC](https://forum.ansible.com/t/recommended-way-to-regex-replace-multiple-lines-in-a-configuration-file/14264/5 "2014-06-11T22:34:06Z")

</div>

Like the filter plugin?

How might that look in a playbook?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [June 12, 2014, 12:43pm UTC](https://forum.ansible.com/t/recommended-way-to-regex-replace-multiple-lines-in-a-configuration-file/14264/6 "2014-06-12T12:43:40Z")

</div>

for targets systems the ‘replace’ module can do this nicely, I would use that even in ‘localhost’ ​
