Merge branch 'lineinfile' of git://github.com/Tinche/ansible into lif
This commit is contained in:
commit
d69c13347b
1 changed files with 75 additions and 37 deletions
112
lineinfile
112
lineinfile
|
@ -25,7 +25,8 @@ DOCUMENTATION = """
|
|||
---
|
||||
module: lineinfile
|
||||
author: Daniel Hokka Zakrisson
|
||||
short_description: Ensure a particular line is in a file
|
||||
short_description: Ensure a particular line is in a file, or replace an
|
||||
existing line using a back-referenced regular expression.
|
||||
description:
|
||||
- This module will search a file for a line, and ensure that it is present or absent.
|
||||
- This is primarily useful when you want to change a single line in a
|
||||
|
@ -36,12 +37,13 @@ options:
|
|||
required: true
|
||||
aliases: [ name, destfile ]
|
||||
description:
|
||||
- The file to modify
|
||||
- The file to modify.
|
||||
regexp:
|
||||
required: true
|
||||
description:
|
||||
- The regular expression to look for in the file. For C(state=present),
|
||||
the pattern to replace. For C(state=absent), the pattern of the line
|
||||
- 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
|
||||
found will be replaced. For C(state=absent), the pattern of the line
|
||||
to remove. Uses Python regular expressions; see
|
||||
U(http://docs.python.org/2/library/re.html).
|
||||
state:
|
||||
|
@ -55,7 +57,22 @@ options:
|
|||
required: false
|
||||
description:
|
||||
- Required for C(state=present). The line to insert/replace into the
|
||||
file. May contain backreferences.
|
||||
file. If backrefs is set, may contain backreferences that will get
|
||||
expanded with the regexp capture groups if the regexp matches. The
|
||||
backreferences should be double escaped (see examples).
|
||||
backrefs:
|
||||
required: false
|
||||
default: "no"
|
||||
choices: [ "yes", "no" ]
|
||||
version_added: 1.1
|
||||
description:
|
||||
- Used with C(state=present). If set, line can contain backreferences
|
||||
(both positional and named) that will get populated if the regexp
|
||||
matches. This flag changes the operation of the module slightly;
|
||||
insertbefore) and insertafter will be ignored, and if the regexp
|
||||
doesn't match anywhere in the file, the file will be left unchanged.
|
||||
If the regexp does match, the last matching line will be replaced by
|
||||
the expanded line parameter.
|
||||
insertafter:
|
||||
required: false
|
||||
default: EOF
|
||||
|
@ -63,6 +80,7 @@ options:
|
|||
- Used with C(state=present). If specified, the line will be inserted
|
||||
after the specified regular expression. A special value is
|
||||
available; C(EOF) for inserting the line at the end of the file.
|
||||
May not be used with backrefs.
|
||||
choices: [ 'EOF', '*regex*' ]
|
||||
insertbefore:
|
||||
required: false
|
||||
|
@ -70,8 +88,8 @@ options:
|
|||
description:
|
||||
- Used with C(state=present). If specified, the line will be inserted
|
||||
before the specified regular expression. A value is available;
|
||||
C(BOF) for inserting the line at the beginning of the
|
||||
file.
|
||||
C(BOF) for inserting the line at the beginning of the file.
|
||||
May not be used with backrefs.
|
||||
choices: [ 'BOF', '*regex*' ]
|
||||
create:
|
||||
required: false
|
||||
|
@ -90,11 +108,11 @@ options:
|
|||
get the original file back if you somehow clobbered it incorrectly.
|
||||
others:
|
||||
description:
|
||||
- All arguments accepted by the M(file) module also work here.
|
||||
- All arguments accepted by the M(file) module also work here.
|
||||
required: false
|
||||
"""
|
||||
|
||||
EXAMPLES = """
|
||||
EXAMPLES = r"""
|
||||
lineinfile: dest=/etc/selinux/config regexp=^SELINUX= line=SELINUX=disabled
|
||||
|
||||
lineinfile: dest=/etc/sudoers state=absent regexp="^%wheel"
|
||||
|
@ -105,11 +123,11 @@ EXAMPLES = """
|
|||
|
||||
lineinfile: dest=/etc/services regexp="^# port for http" insertbefore="^www.*80/tcp" line="# port for http by default"
|
||||
|
||||
lineinfile: \\\"dest=/etc/sudoers state=present regexp='^%wheel' line ='%wheel ALL=(ALL) NOPASSWD: ALL'\\\"
|
||||
lineinfile: dest=/etc/sudoers state=present regexp='^%wheel' line ='%wheel ALL=(ALL) NOPASSWD: ALL'
|
||||
|
||||
lineinfile dest=/tmp/grub.conf state=present regexp='^(splashimage=.*)$' line="#\\1"
|
||||
lineinfile: dest=/opt/jboss-as/bin/standalone.conf state=present regexp='^(.*)Xms(\d+)m(.*)$' line='\\1Xms${xms}m\\3'
|
||||
"""
|
||||
|
||||
|
||||
|
||||
def check_file_attrs(module, changed, message):
|
||||
|
||||
|
@ -121,9 +139,11 @@ def check_file_attrs(module, changed, message):
|
|||
changed = True
|
||||
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):
|
||||
module.fail_json(rc=256, msg='Destination %s is a directory !' % dest)
|
||||
|
@ -142,10 +162,10 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create, backu
|
|||
msg = ""
|
||||
|
||||
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)
|
||||
elif insertbefore is not None and insertbefore not in ('BOF'):
|
||||
elif insertbefore not in (None, 'BOF'):
|
||||
insre = re.compile(insertbefore)
|
||||
else:
|
||||
insre = None
|
||||
|
@ -154,12 +174,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, -1]
|
||||
m = None
|
||||
for lineno in range(0, len(lines)):
|
||||
match_found = mre.search(lines[lineno])
|
||||
for lineno, cur_line in enumerate(lines):
|
||||
match_found = mre.search(cur_line)
|
||||
if match_found:
|
||||
index[0] = lineno
|
||||
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:
|
||||
# + 1 for the next line
|
||||
index[1] = lineno + 1
|
||||
|
@ -167,15 +187,24 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create, backu
|
|||
# + 1 for the previous line
|
||||
index[1] = lineno
|
||||
|
||||
msg = ''
|
||||
changed = False
|
||||
# Regexp matched a line in the file
|
||||
if index[0] != -1:
|
||||
if lines[index[0]] == m.expand(line) + os.linesep:
|
||||
msg = ''
|
||||
changed = False
|
||||
if backrefs:
|
||||
new_line = m.expand(line)
|
||||
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'
|
||||
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
|
||||
elif insertbefore == 'BOF' or insertafter == 'BOF':
|
||||
lines.insert(0, line + os.linesep)
|
||||
|
@ -188,11 +217,10 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create, backu
|
|||
lines.append(line + os.linesep)
|
||||
msg = 'line added'
|
||||
changed = True
|
||||
# Do nothing if regexp didn't match
|
||||
# Do nothing if insert* didn't match
|
||||
elif index[1] == -1:
|
||||
msg = ''
|
||||
changed = False
|
||||
# insertafter/insertbefore= matched
|
||||
pass
|
||||
# insert* matched, but not the regexp
|
||||
else:
|
||||
lines.insert(index[1], line + os.linesep)
|
||||
msg = 'line added'
|
||||
|
@ -205,9 +233,10 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create, backu
|
|||
f.writelines(lines)
|
||||
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)
|
||||
|
||||
|
||||
def absent(module, dest, regexp, backup):
|
||||
|
||||
if os.path.isdir(dest):
|
||||
|
@ -242,36 +271,46 @@ def absent(module, dest, regexp, backup):
|
|||
if changed:
|
||||
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)
|
||||
|
||||
|
||||
def main():
|
||||
module = AnsibleModule(
|
||||
argument_spec = dict(
|
||||
argument_spec=dict(
|
||||
dest=dict(required=True, aliases=['name', 'destfile']),
|
||||
state=dict(default='present', choices=['absent', 'present']),
|
||||
regexp=dict(required=True),
|
||||
line=dict(aliases=['value']),
|
||||
insertafter=dict(default=None),
|
||||
insertbefore=dict(default=None),
|
||||
backrefs=dict(default=False, type='bool'),
|
||||
create=dict(default=False, type='bool'),
|
||||
backup=dict(default=False, type='bool'),
|
||||
),
|
||||
mutually_exclusive = [['insertbefore', 'insertafter']],
|
||||
add_file_common_args = True,
|
||||
supports_check_mode = True
|
||||
mutually_exclusive=[['insertbefore', 'insertafter']],
|
||||
add_file_common_args=True,
|
||||
supports_check_mode=True
|
||||
)
|
||||
|
||||
params = module.params
|
||||
create = module.params['create']
|
||||
backup = module.params['backup']
|
||||
dest = os.path.expanduser(params['dest'])
|
||||
backrefs = module.params['backrefs']
|
||||
dest = os.path.expanduser(params['dest'])
|
||||
|
||||
if params['state'] == 'present':
|
||||
if 'line' not in params:
|
||||
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'],
|
||||
params['insertafter'], params['insertbefore'], create, backup)
|
||||
ins_aft, ins_bef, create, backup, backrefs)
|
||||
else:
|
||||
absent(module, dest, params['regexp'], backup)
|
||||
|
||||
|
@ -279,4 +318,3 @@ def main():
|
|||
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
||||
|
||||
main()
|
||||
|
||||
|
|
Loading…
Reference in a new issue