Fix behavior when insert* doesn't match anything

If insertbefore/insertafter didn't match anything, lineinfile module was doing nothing, instead of adding the line at end of fille as it's supposed to.
This commit is contained in:
Piotr Gosławski 2014-11-30 12:32:08 +01:00 committed by Matt Clay
parent 302c6f9b82
commit d388516046

View file

@ -256,9 +256,9 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create,
msg = 'line added' msg = 'line added'
changed = True changed = True
# Add it to the end of the file if requested or # Add it to the end of the file if requested or
# if insertafter=/insertbefore didn't match anything # if insertafter/insertbefore didn't match anything
# (so default behaviour is to add at the end) # (so default behaviour is to add at the end)
elif insertafter == 'EOF': elif insertafter == 'EOF' or index[1] == -1:
# If the file is not empty then ensure there's a newline before the added line # If the file is not empty then ensure there's a newline before the added line
if len(lines)>0 and not (lines[-1].endswith('\n') or lines[-1].endswith('\r')): if len(lines)>0 and not (lines[-1].endswith('\n') or lines[-1].endswith('\r')):
@ -267,9 +267,6 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create,
lines.append(line + os.linesep) lines.append(line + os.linesep)
msg = 'line added' msg = 'line added'
changed = True changed = True
# Do nothing if insert* didn't match
elif index[1] == -1:
pass
# insert* matched, but not the regexp # insert* matched, but not the regexp
else: else:
lines.insert(index[1], line + os.linesep) lines.insert(index[1], line + os.linesep)