Merge pull request #411 from p-tr0/devel

Fix behavior when insert* doesn't match anything
reviewable/pr18780/r1
Toshio Kuratomi 10 years ago
commit 8ec43e5bbc

@ -85,8 +85,9 @@ options:
default: EOF default: EOF
description: description:
- Used with C(state=present). If specified, the line will be inserted - Used with C(state=present). If specified, the line will be inserted
after the specified regular expression. A special value is 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. 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). May not be used with C(backrefs).
choices: [ 'EOF', '*regex*' ] choices: [ 'EOF', '*regex*' ]
insertbefore: insertbefore:
@ -94,9 +95,10 @@ options:
version_added: "1.1" version_added: "1.1"
description: description:
- Used with C(state=present). If specified, the line will be inserted - Used with C(state=present). If specified, the line will be inserted
before the specified regular expression. A value is available; before the last match of specified regular expression. A value is
C(BOF) for inserting the line at the beginning of the file. available; C(BOF) for inserting the line at the beginning of the file.
May not be used with C(backrefs). If specified regular expresion has no matches, C(insertbefore) will be
ignored. May not be used with C(backrefs).
choices: [ 'BOF', '*regex*' ] choices: [ 'BOF', '*regex*' ]
create: create:
required: false required: false
@ -256,9 +258,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 +269,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)

Loading…
Cancel
Save