Fixed to the lineinfile module.

Added the backrefs parameter to the lineinfile module.
Added tests for the backrefs functionality of the lineinfile module.
reviewable/pr18780/r1
tin 12 years ago
parent 43cf3b0768
commit 9c1e4d5388

@ -121,9 +121,11 @@ def check_file_attrs(module, changed, message):
changed = True changed = True
message += "ownership, perms or SE linux context changed" message += "ownership, perms or SE linux context changed"
return [ message, changed ] return message, changed
def present(module, dest, regexp, line, insertafter, insertbefore, create, backup):
def present(module, dest, regexp, line, insertafter, insertbefore, create,
backup, backrefs):
if os.path.isdir(dest): if os.path.isdir(dest):
module.fail_json(rc=256, msg='Destination %s is a directory !' % dest) module.fail_json(rc=256, msg='Destination %s is a directory !' % dest)
@ -143,9 +145,9 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create, backu
mre = re.compile(regexp) mre = re.compile(regexp)
if insertafter is not None and insertafter not in ('BOF', 'EOF'): if insertafter not in (None, 'BOF', 'EOF'):
insre = re.compile(insertafter) insre = re.compile(insertafter)
elif insertbefore is not None and insertbefore not in ('BOF'): elif insertbefore not in (None, 'BOF'):
insre = re.compile(insertbefore) insre = re.compile(insertbefore)
else: else:
insre = None insre = None
@ -154,12 +156,12 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create, backu
# index[1] is the line num where insertafter/inserbefore has been found # index[1] is the line num where insertafter/inserbefore has been found
index = [-1, -1] index = [-1, -1]
m = None m = None
for lineno in range(0, len(lines)): for lineno, cur_line in enumerate(lines):
match_found = mre.search(lines[lineno]) match_found = mre.search(cur_line)
if match_found: if match_found:
index[0] = lineno index[0] = lineno
m = match_found m = match_found
elif insre is not None and insre.search(lines[lineno]): elif insre is not None and insre.search(cur_line):
if insertafter: if insertafter:
# + 1 for the next line # + 1 for the next line
index[1] = lineno + 1 index[1] = lineno + 1
@ -167,15 +169,24 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create, backu
# + 1 for the previous line # + 1 for the previous line
index[1] = lineno index[1] = lineno
# Regexp matched a line in the file
if index[0] != -1:
if lines[index[0]] == m.expand(line) + os.linesep:
msg = '' msg = ''
changed = False changed = False
# Regexp matched a line in the file
if index[0] != -1:
if backrefs:
new_line = m.expand(line)
else: else:
lines[index[0]] = m.expand(line) + os.linesep # Don't do backref expansion if not asked.
new_line = line
if lines[index[0]] != new_line + os.linesep:
lines[index[0]] = new_line + os.linesep
msg = 'line replaced' msg = 'line replaced'
changed = True changed = True
elif backrefs:
# Do absolutely nothing, since it's not safe generating the line
# without the regexp matching to populate the backrefs.
pass
# Add it to the beginning of the file # Add it to the beginning of the file
elif insertbefore == 'BOF' or insertafter == 'BOF': elif insertbefore == 'BOF' or insertafter == 'BOF':
lines.insert(0, line + os.linesep) lines.insert(0, line + os.linesep)
@ -188,11 +199,10 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create, backu
lines.append(line + os.linesep) lines.append(line + os.linesep)
msg = 'line added' msg = 'line added'
changed = True changed = True
# Do nothing if regexp didn't match # Do nothing if insert* didn't match
elif index[1] == -1: elif index[1] == -1:
msg = '' pass
changed = False # insert* matched, but not the regexp
# insertafter/insertbefore= matched
else: else:
lines.insert(index[1], line + os.linesep) lines.insert(index[1], line + os.linesep)
msg = 'line added' msg = 'line added'
@ -205,9 +215,10 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create, backu
f.writelines(lines) f.writelines(lines)
f.close() f.close()
[ msg, changed ] = check_file_attrs(module, changed, msg) msg, changed = check_file_attrs(module, changed, msg)
module.exit_json(changed=changed, msg=msg) module.exit_json(changed=changed, msg=msg)
def absent(module, dest, regexp, backup): def absent(module, dest, regexp, backup):
if os.path.isdir(dest): if os.path.isdir(dest):
@ -242,36 +253,46 @@ def absent(module, dest, regexp, backup):
if changed: if changed:
msg = "%s line(s) removed" % len(found) msg = "%s line(s) removed" % len(found)
[ msg, changed ] = check_file_attrs(module, changed, msg) msg, changed = check_file_attrs(module, changed, msg)
module.exit_json(changed=changed, found=len(found), msg=msg) module.exit_json(changed=changed, found=len(found), msg=msg)
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec = dict( argument_spec=dict(
dest=dict(required=True, aliases=['name', 'destfile']), dest=dict(required=True, aliases=['name', 'destfile']),
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=None), insertafter=dict(default=None),
insertbefore=dict(default=None), insertbefore=dict(default=None),
backrefs=dict(default=False, type='bool'),
create=dict(default=False, type='bool'), create=dict(default=False, type='bool'),
backup=dict(default=False, type='bool'), backup=dict(default=False, type='bool'),
), ),
mutually_exclusive = [['insertbefore', 'insertafter']], mutually_exclusive=[['insertbefore', 'insertafter']],
add_file_common_args = True, add_file_common_args=True,
supports_check_mode = True supports_check_mode=True
) )
params = module.params params = module.params
create = module.params['create'] create = module.params['create']
backup = module.params['backup'] backup = module.params['backup']
backrefs = module.params['backrefs']
dest = os.path.expanduser(params['dest']) dest = os.path.expanduser(params['dest'])
if params['state'] == 'present': if params['state'] == 'present':
if 'line' not in params: if 'line' not in params:
module.fail_json(msg='line= is required with state=present') module.fail_json(msg='line= is required with state=present')
# Deal with the insertafter default value manually, to avoid errors
# because of the mutually_exclusive mechanism.
ins_bef, ins_aft = params['insertbefore'], params['insertafter']
if ins_bef is None and ins_aft is None:
ins_aft = 'EOF'
present(module, dest, params['regexp'], params['line'], present(module, dest, params['regexp'], params['line'],
params['insertafter'], params['insertbefore'], create, backup) ins_aft, ins_bef, create, backup, backrefs)
else: else:
absent(module, dest, params['regexp'], backup) absent(module, dest, params['regexp'], backup)
@ -279,4 +300,3 @@ def main():
#<<INCLUDE_ANSIBLE_MODULE_COMMON>> #<<INCLUDE_ANSIBLE_MODULE_COMMON>>
main() main()

Loading…
Cancel
Save