Recommended way to regex replace multiple lines in a configuration file

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

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

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

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…

Like the filter plugin?

How might that look in a playbook?

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