Modified to follow Daniel Hokka Zakrisson's recommendations.
This commit is contained in:
parent
fb933cb30c
commit
1cc9fa0fc8
1 changed files with 11 additions and 17 deletions
26
lineinfile
26
lineinfile
|
@ -64,17 +64,14 @@ options:
|
||||||
after the specified regular expression. A special value is
|
after the 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.
|
||||||
choices: [ EOF, *regex* ]
|
choices: [ EOF, *regex* ]
|
||||||
default: EOF
|
|
||||||
insertbefore:
|
insertbefore:
|
||||||
required: false
|
required: false
|
||||||
default: BOF
|
|
||||||
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 specified regular expression. A value is available;
|
||||||
C(BOF) for inserting the line at the beginning of the
|
C(BOF) for inserting the line at the beginning of the
|
||||||
file.
|
file.
|
||||||
choices: [ BOF, *regex* ]
|
choices: [ BOF, *regex* ]
|
||||||
default: BOF
|
|
||||||
create:
|
create:
|
||||||
required: false
|
required: false
|
||||||
choices: [ yes, no ]
|
choices: [ yes, no ]
|
||||||
|
@ -117,17 +114,12 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create, backu
|
||||||
if not mre.search(line):
|
if not mre.search(line):
|
||||||
module.fail_json(msg="usage error: line= doesn't match regexp (%s)" % regexp)
|
module.fail_json(msg="usage error: line= doesn't match regexp (%s)" % regexp)
|
||||||
|
|
||||||
if insertbefore and insertafter:
|
if insertafter is not None and insertafter not in ('BOF', 'EOF'):
|
||||||
module.fail_json(msg="usage error: \"insertbefore\" and \"insertafter\" cannot be combined")
|
insre = re.compile(insertafter)
|
||||||
|
elif insertbefore is not None and insertbefore not in ('BOF'):
|
||||||
|
|
||||||
if insertafter in ('BOF', 'EOF', False):
|
|
||||||
if insertbefore in ('BOF', False):
|
|
||||||
insre = None
|
|
||||||
else:
|
|
||||||
insre = re.compile(insertbefore)
|
insre = re.compile(insertbefore)
|
||||||
else:
|
else:
|
||||||
insre = re.compile(insertafter)
|
insre = None
|
||||||
|
|
||||||
# index[0] is the line num where regexp has been found
|
# index[0] is the line num where regexp has been found
|
||||||
# index[1] is the line num where insertafter/inserbefore has been found
|
# index[1] is the line num where insertafter/inserbefore has been found
|
||||||
|
@ -153,12 +145,13 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create, backu
|
||||||
msg = 'line replaced'
|
msg = 'line replaced'
|
||||||
changed = True
|
changed = True
|
||||||
# Add it to the beginning of the file
|
# Add it to the beginning of the file
|
||||||
# we keep insertafter == 'BOF' for backwards compatibility, but it should be removed eventually
|
|
||||||
elif insertbefore == 'BOF' or insertafter == 'BOF':
|
elif insertbefore == 'BOF' or insertafter == 'BOF':
|
||||||
lines.insert(0, line + os.linesep)
|
lines.insert(0, line + os.linesep)
|
||||||
msg = 'line added'
|
msg = 'line added'
|
||||||
changed = True
|
changed = True
|
||||||
# Add it to the end of the file if requested or if insertafter=/insertbefore didn't match
|
# Add it to the end of the file if requested or
|
||||||
|
# if insertafter=/insertbefore didn't match anything
|
||||||
|
# (so default behaviour is to add at the end)
|
||||||
elif insertafter == 'EOF' or index[1] == -1:
|
elif insertafter == 'EOF' or index[1] == -1:
|
||||||
lines.append(line + os.linesep)
|
lines.append(line + os.linesep)
|
||||||
msg = 'line added'
|
msg = 'line added'
|
||||||
|
@ -209,11 +202,12 @@ def main():
|
||||||
state=dict(default='present', choices=['absent', 'present']),
|
state=dict(default='present', choices=['absent', 'present']),
|
||||||
regexp=dict(required=True),
|
regexp=dict(required=True),
|
||||||
line=dict(aliases=['value']),
|
line=dict(aliases=['value']),
|
||||||
insertafter=dict(default=False),
|
insertafter=dict(default=None),
|
||||||
insertbefore=dict(default=False),
|
insertbefore=dict(default=None),
|
||||||
create=dict(default=False, choices=BOOLEANS),
|
create=dict(default=False, choices=BOOLEANS),
|
||||||
backup=dict(default=False, choices=BOOLEANS),
|
backup=dict(default=False, choices=BOOLEANS),
|
||||||
),
|
),
|
||||||
|
mutually_exclusive = [['insertbefore', 'insertafter']]
|
||||||
)
|
)
|
||||||
|
|
||||||
params = module.params
|
params = module.params
|
||||||
|
|
Loading…
Reference in a new issue