summaryrefslogtreecommitdiff
path: root/files
diff options
context:
space:
mode:
authorToshio Kuratomi <a.badger@gmail.com>2015-03-03 11:32:18 -0800
committerToshio Kuratomi <a.badger@gmail.com>2015-03-03 11:32:18 -0800
commit8ec43e5bbc6f7285fd81cedd4194138edb4f2818 (patch)
tree257e7bda28f01ac1e59f2e71d30a036c3f2f470e /files
parent939ecd33d4a6706e7ed3d021043143ebff177f8b (diff)
parenta5b1a599e20ea9230956783036d6f5aaf5b154ab (diff)
downloadansible-modules-core-8ec43e5bbc6f7285fd81cedd4194138edb4f2818.tar.gz
Merge pull request #411 from p-tr0/devel
Fix behavior when insert* doesn't match anything
Diffstat (limited to 'files')
-rw-r--r--files/lineinfile.py19
1 files changed, 9 insertions, 10 deletions
diff --git a/files/lineinfile.py b/files/lineinfile.py
index b9fc628e..a89dcf0b 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
@@ -256,9 +258,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 +269,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)