|
|
@ -42,10 +42,10 @@ options:
|
|
|
|
regexp:
|
|
|
|
regexp:
|
|
|
|
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
|
|
|
|
found will be replaced. For C(state=absent), the pattern of the line(s)
|
|
|
|
found will be replaced. For C(state=absent), the pattern of the line(s)
|
|
|
|
to remove. Uses Python regular expressions; see
|
|
|
|
to remove. Uses Python regular expressions.
|
|
|
|
U(http://docs.python.org/2/library/re.html).
|
|
|
|
See U(http://docs.python.org/2/library/re.html).
|
|
|
|
version_added: '1.7'
|
|
|
|
version_added: '1.7'
|
|
|
|
state:
|
|
|
|
state:
|
|
|
|
description:
|
|
|
|
description:
|
|
|
@ -282,7 +282,7 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create,
|
|
|
|
if firstmatch:
|
|
|
|
if firstmatch:
|
|
|
|
break
|
|
|
|
break
|
|
|
|
if insertbefore:
|
|
|
|
if insertbefore:
|
|
|
|
# + 1 for the previous line
|
|
|
|
# index[1] for the previous line
|
|
|
|
index[1] = lineno
|
|
|
|
index[1] = lineno
|
|
|
|
if firstmatch:
|
|
|
|
if firstmatch:
|
|
|
|
break
|
|
|
|
break
|
|
|
@ -301,44 +301,49 @@ 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
|
|
|
|
|
|
|
|
|
|
|
|
# Add lines when the regexp match already exists somewhere else in the file
|
|
|
|
# If a regexp is specified and a match is found anywhere in the file, do
|
|
|
|
if insertafter and insertafter != 'EOF':
|
|
|
|
# not insert the line before or after.
|
|
|
|
|
|
|
|
if regexp is None and m:
|
|
|
|
# Ensure there is a line separator after the found string
|
|
|
|
|
|
|
|
# at the end of the file.
|
|
|
|
# Insert lines
|
|
|
|
if b_lines and not b_lines[-1][-1:] in (b('\n'), b('\r')):
|
|
|
|
if insertafter and insertafter != 'EOF':
|
|
|
|
b_lines[-1] = b_lines[-1] + b_linesep
|
|
|
|
|
|
|
|
|
|
|
|
# Ensure there is a line separator after the found string
|
|
|
|
# If the line to insert after is at the end of the file
|
|
|
|
# at the end of the file.
|
|
|
|
# use the appropriate index value.
|
|
|
|
if b_lines and not b_lines[-1][-1:] in (b('\n'), b('\r')):
|
|
|
|
if len(b_lines) == index[1]:
|
|
|
|
b_lines[-1] = b_lines[-1] + b_linesep
|
|
|
|
if b_lines[index[1] - 1].rstrip(b('\r\n')) != b_line:
|
|
|
|
|
|
|
|
b_lines.append(b_line + b_linesep)
|
|
|
|
# If the line to insert after is at the end of the file
|
|
|
|
|
|
|
|
# use the appropriate index value.
|
|
|
|
|
|
|
|
if len(b_lines) == index[1]:
|
|
|
|
|
|
|
|
if b_lines[index[1] - 1].rstrip(b('\r\n')) != b_line:
|
|
|
|
|
|
|
|
b_lines.append(b_line + b_linesep)
|
|
|
|
|
|
|
|
msg = 'line added'
|
|
|
|
|
|
|
|
changed = True
|
|
|
|
|
|
|
|
elif b_lines[index[1]].rstrip(b('\r\n')) != b_line:
|
|
|
|
|
|
|
|
b_lines.insert(index[1], b_line + b_linesep)
|
|
|
|
msg = 'line added'
|
|
|
|
msg = 'line added'
|
|
|
|
changed = True
|
|
|
|
changed = True
|
|
|
|
elif b_lines[index[1]].rstrip(b('\r\n')) != b_line:
|
|
|
|
|
|
|
|
b_lines.insert(index[1], b_line + b_linesep)
|
|
|
|
elif insertbefore:
|
|
|
|
msg = 'line added'
|
|
|
|
# If the line to insert before is at the beginning of the file
|
|
|
|
changed = True
|
|
|
|
# use the appropriate index value.
|
|
|
|
|
|
|
|
if index[1] == 0:
|
|
|
|
elif insertbefore:
|
|
|
|
if b_lines[index[1]].rstrip(b('\r\n')) != b_line:
|
|
|
|
# If the line to insert before is at the beginning of the file
|
|
|
|
b_lines.insert(index[1], b_line + b_linesep)
|
|
|
|
# use the appropriate index value.
|
|
|
|
msg = 'line replaced'
|
|
|
|
if index[1] == 0:
|
|
|
|
changed = True
|
|
|
|
if b_lines[index[1]].rstrip(b('\r\n')) != b_line:
|
|
|
|
|
|
|
|
|
|
|
|
elif b_lines[index[1] - 1].rstrip(b('\r\n')) != b_line:
|
|
|
|
b_lines.insert(index[1], b_line + b_linesep)
|
|
|
|
b_lines.insert(index[1], b_line + b_linesep)
|
|
|
|
msg = 'line replaced'
|
|
|
|
msg = 'line replaced'
|
|
|
|
changed = True
|
|
|
|
changed = True
|
|
|
|
|
|
|
|
|
|
|
|
elif b_lines[index[1] - 1].rstrip(b('\r\n')) != b_line:
|
|
|
|
|
|
|
|
b_lines.insert(index[1], b_line + b_linesep)
|
|
|
|
|
|
|
|
msg = 'line replaced'
|
|
|
|
|
|
|
|
changed = True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
elif b_lines[index[0]] != b_new_line:
|
|
|
|
elif b_lines[index[0]] != b_new_line:
|
|
|
|
b_lines[index[0]] = b_new_line
|
|
|
|
b_lines[index[0]] = b_new_line
|
|
|
|
msg = 'line replaced'
|
|
|
|
msg = 'line replaced'
|
|
|
|
changed = True
|
|
|
|
changed = True
|
|
|
|
|
|
|
|
|
|
|
|
elif backrefs:
|
|
|
|
elif backrefs:
|
|
|
|
# Do absolutely nothing, since it's not safe generating the line
|
|
|
|
# Do absolutely nothing, since it's not safe generating the line
|
|
|
|
# without the regexp matching to populate the backrefs.
|
|
|
|
# without the regexp matching to populate the backrefs.
|
|
|
|