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