From 3c8b4bd4b931105038633aabf8c81d612ff077e4 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 1/2] 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. --- files/lineinfile.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/files/lineinfile.py b/files/lineinfile.py index c72b7f9d9a9..e1a7980f38a 100644 --- a/files/lineinfile.py +++ b/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) From a5b1a599e20ea9230956783036d6f5aaf5b154ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Gos=C5=82awski?= Date: Thu, 4 Dec 2014 17:07:03 +0100 Subject: [PATCH 2/2] update docs for insertbefore/insertafter --- files/lineinfile.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/files/lineinfile.py b/files/lineinfile.py index e1a7980f38a..e02274b0027 100644 --- a/files/lineinfile.py +++ b/files/lineinfile.py @@ -85,8 +85,9 @@ options: default: EOF description: - Used with C(state=present). If specified, the line will be inserted - after the specified regular expression. A special value is - available; C(EOF) for inserting the line at the end of the file. + after the last match of specified regular expression. A special value is + available; C(EOF) for inserting the line at the end of the file. + If specified regular expresion has no matches, EOF will be used instead. May not be used with C(backrefs). choices: [ 'EOF', '*regex*' ] insertbefore: @@ -94,9 +95,10 @@ options: version_added: "1.1" description: - Used with C(state=present). If specified, the line will be inserted - before the specified regular expression. A value is available; - C(BOF) for inserting the line at the beginning of the file. - May not be used with C(backrefs). + before the last match of specified regular expression. A value is + available; C(BOF) for inserting the line at the beginning of the file. + If specified regular expresion has no matches, C(insertbefore) will be + ignored. May not be used with C(backrefs). choices: [ 'BOF', '*regex*' ] create: required: false