|
|
@ -40,7 +40,7 @@ options:
|
|
|
|
description:
|
|
|
|
description:
|
|
|
|
- The file to modify.
|
|
|
|
- The file to modify.
|
|
|
|
regexp:
|
|
|
|
regexp:
|
|
|
|
required: true
|
|
|
|
required: false
|
|
|
|
description:
|
|
|
|
description:
|
|
|
|
- The regular expression to look for in every line of the file. For
|
|
|
|
- The regular expression to look for in every line of the file. For
|
|
|
|
C(state=present), the pattern to replace if found; only the last line
|
|
|
|
C(state=present), the pattern to replace if found; only the last line
|
|
|
@ -186,6 +186,9 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create,
|
|
|
|
lines = f.readlines()
|
|
|
|
lines = f.readlines()
|
|
|
|
f.close()
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
msg = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if regexp is not None:
|
|
|
|
mre = re.compile(regexp)
|
|
|
|
mre = re.compile(regexp)
|
|
|
|
|
|
|
|
|
|
|
|
if insertafter not in (None, 'BOF', 'EOF'):
|
|
|
|
if insertafter not in (None, 'BOF', 'EOF'):
|
|
|
@ -200,7 +203,10 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create,
|
|
|
|
index = [-1, -1]
|
|
|
|
index = [-1, -1]
|
|
|
|
m = None
|
|
|
|
m = None
|
|
|
|
for lineno, cur_line in enumerate(lines):
|
|
|
|
for lineno, cur_line in enumerate(lines):
|
|
|
|
|
|
|
|
if regexp is not None:
|
|
|
|
match_found = mre.search(cur_line)
|
|
|
|
match_found = mre.search(cur_line)
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
match_found = line == cur_line.rstrip('\r\n')
|
|
|
|
if match_found:
|
|
|
|
if match_found:
|
|
|
|
index[0] = lineno
|
|
|
|
index[0] = lineno
|
|
|
|
m = match_found
|
|
|
|
m = match_found
|
|
|
@ -260,7 +266,7 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create,
|
|
|
|
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, line, backup):
|
|
|
|
|
|
|
|
|
|
|
|
if not os.path.exists(dest):
|
|
|
|
if not os.path.exists(dest):
|
|
|
|
module.exit_json(changed=False, msg="file not present")
|
|
|
|
module.exit_json(changed=False, msg="file not present")
|
|
|
@ -270,15 +276,18 @@ def absent(module, dest, regexp, backup):
|
|
|
|
f = open(dest, 'rb')
|
|
|
|
f = open(dest, 'rb')
|
|
|
|
lines = f.readlines()
|
|
|
|
lines = f.readlines()
|
|
|
|
f.close()
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
if regexp is not None:
|
|
|
|
cre = re.compile(regexp)
|
|
|
|
cre = re.compile(regexp)
|
|
|
|
found = []
|
|
|
|
found = []
|
|
|
|
|
|
|
|
|
|
|
|
def matcher(line):
|
|
|
|
def matcher(cur_line):
|
|
|
|
if cre.search(line):
|
|
|
|
if regexp is not None:
|
|
|
|
found.append(line)
|
|
|
|
match_found = cre.search(cur_line)
|
|
|
|
return False
|
|
|
|
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
match_found = line == cur_line.rstrip('\r\n')
|
|
|
|
|
|
|
|
if match_found:
|
|
|
|
|
|
|
|
found.append(cur_line)
|
|
|
|
|
|
|
|
return not match_found
|
|
|
|
|
|
|
|
|
|
|
|
lines = filter(matcher, lines)
|
|
|
|
lines = filter(matcher, lines)
|
|
|
|
changed = len(found) > 0
|
|
|
|
changed = len(found) > 0
|
|
|
@ -299,7 +308,7 @@ def main():
|
|
|
|
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(default=None),
|
|
|
|
line=dict(aliases=['value']),
|
|
|
|
line=dict(aliases=['value']),
|
|
|
|
insertafter=dict(default=None),
|
|
|
|
insertafter=dict(default=None),
|
|
|
|
insertbefore=dict(default=None),
|
|
|
|
insertbefore=dict(default=None),
|
|
|
@ -324,6 +333,9 @@ def main():
|
|
|
|
module.fail_json(rc=256, msg='Destination %s is a directory !' % dest)
|
|
|
|
module.fail_json(rc=256, msg='Destination %s is a directory !' % dest)
|
|
|
|
|
|
|
|
|
|
|
|
if params['state'] == 'present':
|
|
|
|
if params['state'] == 'present':
|
|
|
|
|
|
|
|
if backrefs and params['regexp'] is None:
|
|
|
|
|
|
|
|
module.fail_json(msg='regexp= is required with backrefs=true')
|
|
|
|
|
|
|
|
|
|
|
|
if params.get('line', None) is None:
|
|
|
|
if params.get('line', None) is None:
|
|
|
|
module.fail_json(msg='line= is required with state=present')
|
|
|
|
module.fail_json(msg='line= is required with state=present')
|
|
|
|
|
|
|
|
|
|
|
@ -336,7 +348,10 @@ def main():
|
|
|
|
present(module, dest, params['regexp'], params['line'],
|
|
|
|
present(module, dest, params['regexp'], params['line'],
|
|
|
|
ins_aft, ins_bef, create, backup, backrefs)
|
|
|
|
ins_aft, ins_bef, create, backup, backrefs)
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
absent(module, dest, params['regexp'], backup)
|
|
|
|
if params['regexp'] is None and params.get('line', None) is None:
|
|
|
|
|
|
|
|
module.fail_json(msg='one of line= or regexp= is required with state=absent')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
absent(module, dest, params['regexp'], params.get('line', None), backup)
|
|
|
|
|
|
|
|
|
|
|
|
# this is magic, see lib/ansible/module_common.py
|
|
|
|
# this is magic, see lib/ansible/module_common.py
|
|
|
|
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
|
|
|
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
|
|
|