|
|
|
@ -44,6 +44,17 @@ options:
|
|
|
|
|
type: str
|
|
|
|
|
aliases: [ regex ]
|
|
|
|
|
version_added: '1.7'
|
|
|
|
|
search_string:
|
|
|
|
|
description:
|
|
|
|
|
- The literal string to look for in every line of the file. This does not have to match the entire line.
|
|
|
|
|
- For C(state=present), the line to replace if the string is found in the file. Only the last line found will be replaced.
|
|
|
|
|
- For C(state=absent), the line(s) to remove if the string is in the line.
|
|
|
|
|
- If the literal expression is not matched, the line will be
|
|
|
|
|
added to the file in keeping with C(insertbefore) or C(insertafter)
|
|
|
|
|
settings.
|
|
|
|
|
- Mutually exclusive with C(backrefs) and C(regexp).
|
|
|
|
|
type: str
|
|
|
|
|
version_added: '2.11'
|
|
|
|
|
state:
|
|
|
|
|
description:
|
|
|
|
|
- Whether the line should be there or not.
|
|
|
|
@ -68,6 +79,7 @@ options:
|
|
|
|
|
does not match anywhere in the file, the file will be left unchanged.
|
|
|
|
|
- If the C(regexp) does match, the last matching line will be replaced by
|
|
|
|
|
the expanded line parameter.
|
|
|
|
|
- Mutually exclusive with C(search_string).
|
|
|
|
|
type: bool
|
|
|
|
|
default: no
|
|
|
|
|
version_added: "1.1"
|
|
|
|
@ -136,6 +148,7 @@ seealso:
|
|
|
|
|
author:
|
|
|
|
|
- Daniel Hokka Zakrissoni (@dhozac)
|
|
|
|
|
- Ahti Kitsik (@ahtik)
|
|
|
|
|
- Jose Angel Munoz (@imjoseangel)
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
EXAMPLES = r'''
|
|
|
|
@ -161,6 +174,15 @@ EXAMPLES = r'''
|
|
|
|
|
group: root
|
|
|
|
|
mode: '0644'
|
|
|
|
|
|
|
|
|
|
- name: Replace a localhost entry searching for a literal string to avoid escaping
|
|
|
|
|
lineinfile:
|
|
|
|
|
path: /etc/hosts
|
|
|
|
|
search_string: '127.0.0.1'
|
|
|
|
|
line: 127.0.0.1 localhost
|
|
|
|
|
owner: root
|
|
|
|
|
group: root
|
|
|
|
|
mode: '0644'
|
|
|
|
|
|
|
|
|
|
- name: Ensure the default Apache port is 8080
|
|
|
|
|
ansible.builtin.lineinfile:
|
|
|
|
|
path: /etc/httpd/conf/httpd.conf
|
|
|
|
@ -168,6 +190,13 @@ EXAMPLES = r'''
|
|
|
|
|
insertafter: '^#Listen '
|
|
|
|
|
line: Listen 8080
|
|
|
|
|
|
|
|
|
|
- name: Ensure php extension matches new pattern
|
|
|
|
|
lineinfile:
|
|
|
|
|
path: /etc/httpd/conf/httpd.conf
|
|
|
|
|
search_string: '<FilesMatch ".php[45]?$">'
|
|
|
|
|
insertafter: '^\t<Location \/>\n'
|
|
|
|
|
line: ' <FilesMatch ".php[34]?$">'
|
|
|
|
|
|
|
|
|
|
- name: Ensure we have our own comment added to /etc/services
|
|
|
|
|
ansible.builtin.lineinfile:
|
|
|
|
|
path: /etc/services
|
|
|
|
@ -253,7 +282,7 @@ def check_file_attrs(module, changed, message, diff):
|
|
|
|
|
return message, changed
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def present(module, dest, regexp, line, insertafter, insertbefore, create,
|
|
|
|
|
def present(module, dest, regexp, search_string, line, insertafter, insertbefore, create,
|
|
|
|
|
backup, backrefs, firstmatch):
|
|
|
|
|
|
|
|
|
|
diff = {'before': '',
|
|
|
|
@ -301,8 +330,8 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create,
|
|
|
|
|
# "If regular expressions are passed to both regexp and
|
|
|
|
|
# insertafter, insertafter is only honored if no match for regexp is found."
|
|
|
|
|
# Therefore:
|
|
|
|
|
# 1. regexp was found -> ignore insertafter, replace the founded line
|
|
|
|
|
# 2. regexp was not found -> insert the line after 'insertafter' or 'insertbefore' line
|
|
|
|
|
# 1. regexp or search_string was found -> ignore insertafter, replace the founded line
|
|
|
|
|
# 2. regexp or search_string was not found -> insert the line after 'insertafter' or 'insertbefore' line
|
|
|
|
|
|
|
|
|
|
# Given the above:
|
|
|
|
|
# 1. First check that there is no match for regexp:
|
|
|
|
@ -315,7 +344,17 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create,
|
|
|
|
|
if firstmatch:
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
# 2. When no match found on the previous step,
|
|
|
|
|
# 2. Second check that there is no match for search_string:
|
|
|
|
|
if search_string is not None:
|
|
|
|
|
for lineno, b_cur_line in enumerate(b_lines):
|
|
|
|
|
match_found = to_bytes(search_string, errors='surrogate_or_strict') in b_cur_line
|
|
|
|
|
if match_found:
|
|
|
|
|
index[0] = lineno
|
|
|
|
|
match = match_found
|
|
|
|
|
if firstmatch:
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
# 3. When no match found on the previous step,
|
|
|
|
|
# parse for searching insertafter/insertbefore:
|
|
|
|
|
if not match:
|
|
|
|
|
for lineno, b_cur_line in enumerate(b_lines):
|
|
|
|
@ -350,9 +389,9 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create,
|
|
|
|
|
if not b_new_line.endswith(b_linesep):
|
|
|
|
|
b_new_line += b_linesep
|
|
|
|
|
|
|
|
|
|
# If no regexp was given and no line match is found anywhere in the file,
|
|
|
|
|
# If no regexp or search_string was given and no line match is found anywhere in the file,
|
|
|
|
|
# insert the line appropriately if using insertbefore or insertafter
|
|
|
|
|
if regexp is None and match is None and not exact_line_match:
|
|
|
|
|
if (regexp, search_string, match) == (None, None, None) and not exact_line_match:
|
|
|
|
|
|
|
|
|
|
# Insert lines
|
|
|
|
|
if insertafter and insertafter != 'EOF':
|
|
|
|
@ -428,7 +467,7 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create,
|
|
|
|
|
msg = 'line added'
|
|
|
|
|
changed = True
|
|
|
|
|
|
|
|
|
|
# insert matched, but not the regexp
|
|
|
|
|
# insert matched, but not the regexp or search_string
|
|
|
|
|
else:
|
|
|
|
|
b_lines.insert(index[1], b_line + b_linesep)
|
|
|
|
|
msg = 'line added'
|
|
|
|
@ -456,7 +495,7 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create,
|
|
|
|
|
module.exit_json(changed=changed, msg=msg, backup=backupdest, diff=difflist)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def absent(module, dest, regexp, line, backup):
|
|
|
|
|
def absent(module, dest, regexp, search_string, line, backup):
|
|
|
|
|
|
|
|
|
|
b_dest = to_bytes(dest, errors='surrogate_or_strict')
|
|
|
|
|
if not os.path.exists(b_dest):
|
|
|
|
@ -483,6 +522,8 @@ def absent(module, dest, regexp, line, backup):
|
|
|
|
|
def matcher(b_cur_line):
|
|
|
|
|
if regexp is not None:
|
|
|
|
|
match_found = bre_c.search(b_cur_line)
|
|
|
|
|
elif search_string is not None:
|
|
|
|
|
match_found = to_bytes(search_string, errors='surrogate_or_strict') in b_cur_line
|
|
|
|
|
else:
|
|
|
|
|
match_found = b_line == b_cur_line.rstrip(b'\r\n')
|
|
|
|
|
if match_found:
|
|
|
|
@ -521,6 +562,7 @@ def main():
|
|
|
|
|
path=dict(type='path', required=True, aliases=['dest', 'destfile', 'name']),
|
|
|
|
|
state=dict(type='str', default='present', choices=['absent', 'present']),
|
|
|
|
|
regexp=dict(type='str', aliases=['regex']),
|
|
|
|
|
search_string=dict(type='str'),
|
|
|
|
|
line=dict(type='str', aliases=['value']),
|
|
|
|
|
insertafter=dict(type='str'),
|
|
|
|
|
insertbefore=dict(type='str'),
|
|
|
|
@ -530,7 +572,8 @@ def main():
|
|
|
|
|
firstmatch=dict(type='bool', default=False),
|
|
|
|
|
validate=dict(type='str'),
|
|
|
|
|
),
|
|
|
|
|
mutually_exclusive=[['insertbefore', 'insertafter']],
|
|
|
|
|
mutually_exclusive=[
|
|
|
|
|
['insertbefore', 'insertafter'], ['regexp', 'search_string'], ['backrefs', 'search_string']],
|
|
|
|
|
add_file_common_args=True,
|
|
|
|
|
supports_check_mode=True,
|
|
|
|
|
)
|
|
|
|
@ -542,13 +585,17 @@ def main():
|
|
|
|
|
path = params['path']
|
|
|
|
|
firstmatch = params['firstmatch']
|
|
|
|
|
regexp = params['regexp']
|
|
|
|
|
search_string = params['search_string']
|
|
|
|
|
line = params['line']
|
|
|
|
|
|
|
|
|
|
if regexp == '':
|
|
|
|
|
module.warn(
|
|
|
|
|
"The regular expression is an empty string, which will match every line in the file. "
|
|
|
|
|
"This may have unintended consequences, such as replacing the last line in the file rather than appending. "
|
|
|
|
|
"If this is desired, use '^' to match every line in the file and avoid this warning.")
|
|
|
|
|
if '' in [regexp, search_string]:
|
|
|
|
|
msg = ("The %s is an empty string, which will match every line in the file. "
|
|
|
|
|
"This may have unintended consequences, such as replacing the last line in the file rather than appending.")
|
|
|
|
|
param_name = 'search string'
|
|
|
|
|
if regexp == '':
|
|
|
|
|
param_name = 'regular expression'
|
|
|
|
|
msg += " If this is desired, use '^' to match every line in the file and avoid this warning."
|
|
|
|
|
module.warn(msg % param_name)
|
|
|
|
|
|
|
|
|
|
b_path = to_bytes(path, errors='surrogate_or_strict')
|
|
|
|
|
if os.path.isdir(b_path):
|
|
|
|
@ -567,13 +614,13 @@ def main():
|
|
|
|
|
if ins_bef is None and ins_aft is None:
|
|
|
|
|
ins_aft = 'EOF'
|
|
|
|
|
|
|
|
|
|
present(module, path, regexp, line,
|
|
|
|
|
present(module, path, regexp, search_string, line,
|
|
|
|
|
ins_aft, ins_bef, create, backup, backrefs, firstmatch)
|
|
|
|
|
else:
|
|
|
|
|
if regexp is None and line is None:
|
|
|
|
|
module.fail_json(msg='one of line or regexp is required with state=absent')
|
|
|
|
|
if (regexp, search_string, line) == (None, None, None):
|
|
|
|
|
module.fail_json(msg='one of line, search_string, or regexp is required with state=absent')
|
|
|
|
|
|
|
|
|
|
absent(module, path, regexp, line, backup)
|
|
|
|
|
absent(module, path, regexp, search_string, line, backup)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|