# module lineinfile

**URL:** <https://forum.ansible.com/t/module-lineinfile/12463>\
**Category:** Ansible Project\
**Created:** [March 6, 2013, 8:12pm UTC](https://forum.ansible.com/t/module-lineinfile/12463 "2013-03-06T20:12:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andreas\_Piesk](https://avatars.discourse-cdn.com/v4/letter/a/d78d45/32.png) [@Andreas\_Piesk](https://forum.ansible.com/u/Andreas_Piesk)\
**Post date:** [March 6, 2013, 8:12pm UTC](https://forum.ansible.com/t/module-lineinfile/12463/1 "2013-03-06T20:12:55Z")

</div>

Hello list,

while playing around with Ansible and looking at the code i noticed, that the module lineinfile  
cannot replace a line found by regexp with an arbitrary line, there's an explicit check in it.

another thing: it would be great if lineinfile can replace lines found by regexp with  
backreferences. for instance, i want disable by commenting out the splashscreen in grub.conf but the  
line can vary from host to host.

^s(plashimage=.\*)$

to

#\1

same can be used to comment stuff in or comment stuff in and change some values.

As i'm no python developer, this is my humble approach to tackle the problem:

diff --git a/library/lineinfile b/library/lineinfile  
index ed5e28b..dd3b8e9 100644  
--- a/library/lineinfile  
+++ b/library/lineinfile  
@@ -139,8 +139,8 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create, backu  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg = ""

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mre = re.compile(regexp)  
- if not mre.search(line):  
- module.fail\_json(msg="usage error: line= doesn't match regexp (%s)" % regexp)  
+# if not mre.search(line):  
+# module.fail\_json(msg="usage error: line= doesn't match regexp (%s)" % regexp)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if insertafter is not None and insertafter not in ('BOF', 'EOF'):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;insre = re.compile(insertafter)  
@@ -152,9 +152,12 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create, backu  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# index[0] is the line num where regexp has been found  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# index[1] is the line num where insertafter/inserbefore has been found  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;index = [-1, -1]  
+ m = None  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for lineno in range(0, len(lines)):  
- if mre.search(lines[lineno]):  
+ a = mre.search(lines[lineno])  
+ if a:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;index[0] = lineno  
+ m = a  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elif insre is not None and insre.search(lines[lineno]):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if insertafter:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# + 1 for the next line  
@@ -165,11 +168,11 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create, backu

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Regexp matched a line in the file  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if index[0] != -1:  
- if lines[index[0]] == line + os.linesep:  
+ if lines[index[0]] == m.expand(line) + os.linesep:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg = ''  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;changed = False  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else:  
- lines[index[0]] = line + os.linesep  
+ lines[index[0]] = m.expand(line) + os.linesep  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg = 'line replaced'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;changed = True  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Add it to the beginning of the file

---

<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:** [March 7, 2013, 5:09am UTC](https://forum.ansible.com/t/module-lineinfile/12463/2 "2013-03-07T05:09:55Z")

</div>

Sure, I'm open to improvements.

Patches should be sent in by github pull requests if you want  
something reviewed though, as we've got a lot of stuff coming in, and  
it's far easier if everything comes in the same way.

Thanks!
