diff --git a/lib/ansible/modules/files/lineinfile.py b/lib/ansible/modules/files/lineinfile.py index b9fc628e10c..11fd47027e9 100644 --- a/lib/ansible/modules/files/lineinfile.py +++ b/lib/ansible/modules/files/lineinfile.py @@ -256,9 +256,9 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create, msg = 'line added' changed = True # 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) - 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 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) msg = 'line added' changed = True - # Do nothing if insert* didn't match - elif index[1] == -1: - pass # insert* matched, but not the regexp else: lines.insert(index[1], line + os.linesep)