lineinfile module behavior

Hi,

I want to have a discussion about “lineinfile” module specifications, and the behavior in several cases.

Here’s the algorithm of lineinfile module, when “state=present” :

1) line mentioned
Searching for “line”.
If line is found, then do nothing
If line is not found, then add the line at the very end of the file (because if insertafter and insertbefore are not filled, then insertafter is forced to EOF).

2a) line mentioned and insertbefore=BOF
If line is found, then do nothing
If line is not found, then add the line at the beginning of the file

2b) line mentioned and insertbefore=BOF and regexp = regexp2
if line is found then do nothing
if line is not found and regexp is found then change the “regexp2” line by “line” (insertbefore is ignored)
if line is not found and regexp is not found, then line is added at the beginning of the file.

2c) line mentioned and insertbefore = regexp
if line is found, then do nothing
if line is not found, and insertbefore regexp is not found, then add the line at the end of the file
If line is not found, and insertbefore regexp is found, then add the line before the ‘insertbefore’ line.

2d) line mentioned and insertbefore = regexp and regexp = regexp2
if line is found, then do nothing
if line is not found and insertbefore regexp is found and regexp is found then change the “regepx2” line by “line”
if line is not found and insertbefore regexp is found and regexp is not found, then insert “line” before “regexp” line
if line is not found and insertbefore regexp is not found and regexp is found, then change “regexp2” by “line”
if line is not found and insertbefore regexp is not found and regexp is not found, then add the line at the end of the file.

3a, 3b, 3c and 3d) same as 2 but with insertafter in spite of insertbefore
see relevant 2x) section.

Now, considering the behavior describes here-before, I have several comments :

  • I can understand that, if you search for a line without mentioning “regexp”, and you don’t find the line in the file, you add it at the end of the file. The description of the module is pretty explicit “This module will search a file for a line, and ensure that it is present or absent.”
  • However, IMHO, when you specify a regexp, it means that you want to change a line. So, to me, if the regexp is not matched, the file should be left intact, without any addition / modification. The documentation is not clear about the behavior of the module in this case.
    So I would like to discuss with you guys, the behavior of the module when you use “regexp” (and possibly insertbefore, insertafter). I would like also to hear the thoughts of the some Ansible users that think it’s a normal behavior to have regexp mentioned, that doesn’t matched, but have the line still added at the end of the file.

Thanks,

up ?