From d388516046f786d1206c62451945edfacacb3e09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Gos=C5=82awski?= Date: Sun, 30 Nov 2014 12:32:08 +0100 Subject: [PATCH] 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. --- lib/ansible/modules/files/lineinfile.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/ansible/modules/files/lineinfile.py b/lib/ansible/modules/files/lineinfile.py index b9fc628e10..11fd47027e 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)