2012-08-29 22:04:23 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# (c) 2012, Daniel Hokka Zakrisson <daniel@hozac.com>
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import re
|
|
|
|
import os
|
2013-05-08 02:08:29 +00:00
|
|
|
import tempfile
|
2012-08-29 22:04:23 +00:00
|
|
|
|
2013-02-09 19:34:03 +00:00
|
|
|
DOCUMENTATION = """
|
2012-09-19 20:29:11 +00:00
|
|
|
---
|
|
|
|
module: lineinfile
|
2012-10-01 10:37:51 +00:00
|
|
|
author: Daniel Hokka Zakrisson
|
2013-03-26 21:00:34 +00:00
|
|
|
short_description: Ensure a particular line is in a file, or replace an
|
|
|
|
existing line using a back-referenced regular expression.
|
2012-09-19 20:29:11 +00:00
|
|
|
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
|
|
|
|
file only. For other cases, see the M(copy) or M(template) modules.
|
|
|
|
version_added: "0.7"
|
|
|
|
options:
|
2012-10-05 09:03:14 +00:00
|
|
|
dest:
|
2012-09-28 01:06:31 +00:00
|
|
|
required: true
|
2012-10-05 09:03:14 +00:00
|
|
|
aliases: [ name, destfile ]
|
2012-09-28 01:06:31 +00:00
|
|
|
description:
|
2013-03-26 21:00:34 +00:00
|
|
|
- The file to modify.
|
2012-09-28 01:06:31 +00:00
|
|
|
regexp:
|
|
|
|
required: true
|
|
|
|
description:
|
2013-03-26 21:00:34 +00:00
|
|
|
- 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
|
2012-12-20 16:41:45 +00:00
|
|
|
to remove. Uses Python regular expressions; see
|
|
|
|
U(http://docs.python.org/2/library/re.html).
|
2012-09-28 01:06:31 +00:00
|
|
|
state:
|
|
|
|
required: false
|
|
|
|
choices: [ present, absent ]
|
|
|
|
default: "present"
|
|
|
|
aliases: []
|
|
|
|
description:
|
|
|
|
- Whether the line should be there or not.
|
|
|
|
line:
|
|
|
|
required: false
|
|
|
|
description:
|
2012-10-01 07:18:54 +00:00
|
|
|
- Required for C(state=present). The line to insert/replace into the
|
2013-03-26 21:00:34 +00:00
|
|
|
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" ]
|
2013-03-30 19:44:34 +00:00
|
|
|
version_added: "1.1"
|
2013-03-26 21:00:34 +00:00
|
|
|
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;
|
2013-04-27 16:09:26 +00:00
|
|
|
insertbefore and insertafter will be ignored, and if the regexp
|
2013-03-26 21:00:34 +00:00
|
|
|
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.
|
2012-09-28 01:06:31 +00:00
|
|
|
insertafter:
|
|
|
|
required: false
|
|
|
|
default: EOF
|
|
|
|
description:
|
2012-10-01 07:18:54 +00:00
|
|
|
- Used with C(state=present). If specified, the line will be inserted
|
2013-02-11 06:46:06 +00:00
|
|
|
after the specified regular expression. A special value is
|
|
|
|
available; C(EOF) for inserting the line at the end of the file.
|
2013-03-26 21:00:34 +00:00
|
|
|
May not be used with backrefs.
|
2013-02-16 20:40:31 +00:00
|
|
|
choices: [ 'EOF', '*regex*' ]
|
2013-02-11 06:46:06 +00:00
|
|
|
insertbefore:
|
|
|
|
required: false
|
2013-03-30 19:44:34 +00:00
|
|
|
version_added: "1.1"
|
2013-02-11 06:46:06 +00:00
|
|
|
description:
|
|
|
|
- Used with C(state=present). If specified, the line will be inserted
|
|
|
|
before the specified regular expression. A value is available;
|
2013-03-26 21:00:34 +00:00
|
|
|
C(BOF) for inserting the line at the beginning of the file.
|
|
|
|
May not be used with backrefs.
|
2013-02-16 20:40:31 +00:00
|
|
|
choices: [ 'BOF', '*regex*' ]
|
2012-10-30 15:57:45 +00:00
|
|
|
create:
|
|
|
|
required: false
|
2013-03-12 12:18:12 +00:00
|
|
|
choices: [ "yes", "no" ]
|
|
|
|
default: "no"
|
2012-10-30 15:57:45 +00:00
|
|
|
description:
|
|
|
|
- Used with C(state=present). If specified, the file will be created
|
|
|
|
if it does not already exist. By default it will fail if the file
|
|
|
|
is missing.
|
2012-09-28 01:06:31 +00:00
|
|
|
backup:
|
|
|
|
required: false
|
2013-03-12 12:18:12 +00:00
|
|
|
default: "no"
|
|
|
|
choices: [ "yes", "no" ]
|
2012-09-28 01:06:31 +00:00
|
|
|
description:
|
|
|
|
- Create a backup file including the timestamp information so you can
|
|
|
|
get the original file back if you somehow clobbered it incorrectly.
|
2013-02-22 08:33:21 +00:00
|
|
|
others:
|
|
|
|
description:
|
2013-03-25 23:22:33 +00:00
|
|
|
- All arguments accepted by the M(file) module also work here.
|
2013-02-22 08:33:21 +00:00
|
|
|
required: false
|
2013-02-23 20:08:33 +00:00
|
|
|
"""
|
|
|
|
|
2013-03-26 21:00:34 +00:00
|
|
|
EXAMPLES = r"""
|
2013-06-14 09:53:43 +00:00
|
|
|
- lineinfile: dest=/etc/selinux/config regexp=^SELINUX= line=SELINUX=disabled
|
2013-02-23 20:08:33 +00:00
|
|
|
|
2013-06-14 09:53:43 +00:00
|
|
|
- lineinfile: dest=/etc/sudoers state=absent regexp="^%wheel"
|
2013-02-23 20:08:33 +00:00
|
|
|
|
2013-06-14 09:53:43 +00:00
|
|
|
- lineinfile: dest=/etc/hosts regexp='^127\.0\.0\.1' line='127.0.0.1 localhost' owner=root group=root mode=0644
|
2013-02-23 20:08:33 +00:00
|
|
|
|
2013-06-14 09:53:43 +00:00
|
|
|
- lineinfile: dest=/etc/httpd/conf/httpd.conf regexp="^Listen " insertafter="^#Listen " line="Listen 8080"
|
2013-02-23 20:08:33 +00:00
|
|
|
|
2013-06-14 09:53:43 +00:00
|
|
|
- lineinfile: dest=/etc/services regexp="^# port for http" insertbefore="^www.*80/tcp" line="# port for http by default"
|
2013-02-23 20:08:33 +00:00
|
|
|
|
2013-09-06 01:37:54 +00:00
|
|
|
# Fully quoted because of the ': ' on the line. See the Gotchas in the YAML docs.
|
|
|
|
- lineinfile: "dest=/etc/sudoers state=present regexp='^%wheel' line='%wheel ALL=(ALL) NOPASSWD: ALL'"
|
2013-03-07 07:19:40 +00:00
|
|
|
|
2013-06-14 09:53:43 +00:00
|
|
|
- lineinfile: dest=/opt/jboss-as/bin/standalone.conf regexp='^(.*)Xms(\d+)m(.*)$' line='\1Xms${xms}m\3' backrefs=yes
|
2013-02-09 19:34:03 +00:00
|
|
|
"""
|
2013-03-25 23:22:33 +00:00
|
|
|
|
2013-05-08 02:08:29 +00:00
|
|
|
def write_changes(module,lines,dest):
|
|
|
|
|
|
|
|
tmpfd, tmpfile = tempfile.mkstemp()
|
|
|
|
f = os.fdopen(tmpfd,'wb')
|
|
|
|
f.writelines(lines)
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
module.atomic_move(tmpfile, dest)
|
2012-09-19 20:29:11 +00:00
|
|
|
|
2013-02-22 16:30:19 +00:00
|
|
|
def check_file_attrs(module, changed, message):
|
2013-02-22 08:33:21 +00:00
|
|
|
|
|
|
|
file_args = module.load_file_common_arguments(module.params)
|
|
|
|
if module.set_file_attributes_if_different(file_args, False):
|
2013-02-22 16:30:19 +00:00
|
|
|
|
2013-02-22 08:33:21 +00:00
|
|
|
if changed:
|
|
|
|
message += " and "
|
|
|
|
changed = True
|
|
|
|
message += "ownership, perms or SE linux context changed"
|
|
|
|
|
2013-03-25 23:22:33 +00:00
|
|
|
return message, changed
|
|
|
|
|
2013-02-22 08:33:21 +00:00
|
|
|
|
2013-03-25 23:22:33 +00:00
|
|
|
def present(module, dest, regexp, line, insertafter, insertbefore, create,
|
|
|
|
backup, backrefs):
|
2012-10-30 15:57:45 +00:00
|
|
|
|
2013-05-08 02:08:29 +00:00
|
|
|
if not os.path.exists(dest):
|
2012-10-30 15:57:45 +00:00
|
|
|
if not create:
|
|
|
|
module.fail_json(rc=257, msg='Destination %s does not exist !' % dest)
|
|
|
|
destpath = os.path.dirname(dest)
|
|
|
|
if not os.path.exists(destpath):
|
|
|
|
os.makedirs(destpath)
|
|
|
|
lines = []
|
|
|
|
else:
|
|
|
|
f = open(dest, 'rb')
|
|
|
|
lines = f.readlines()
|
|
|
|
f.close()
|
2012-08-29 22:04:23 +00:00
|
|
|
|
2013-02-22 16:30:19 +00:00
|
|
|
msg = ""
|
|
|
|
|
2012-08-29 22:04:23 +00:00
|
|
|
mre = re.compile(regexp)
|
2013-03-25 23:22:33 +00:00
|
|
|
|
|
|
|
if insertafter not in (None, 'BOF', 'EOF'):
|
2013-02-11 06:46:06 +00:00
|
|
|
insre = re.compile(insertafter)
|
2013-03-25 23:22:33 +00:00
|
|
|
elif insertbefore not in (None, 'BOF'):
|
2013-02-13 03:55:40 +00:00
|
|
|
insre = re.compile(insertbefore)
|
|
|
|
else:
|
|
|
|
insre = None
|
2012-08-29 22:04:23 +00:00
|
|
|
|
2013-02-11 06:46:06 +00:00
|
|
|
# index[0] is the line num where regexp has been found
|
|
|
|
# index[1] is the line num where insertafter/inserbefore has been found
|
2012-08-29 22:04:23 +00:00
|
|
|
index = [-1, -1]
|
2013-03-07 07:19:40 +00:00
|
|
|
m = None
|
2013-03-25 23:22:33 +00:00
|
|
|
for lineno, cur_line in enumerate(lines):
|
|
|
|
match_found = mre.search(cur_line)
|
2013-03-07 07:19:40 +00:00
|
|
|
if match_found:
|
2012-08-29 22:04:23 +00:00
|
|
|
index[0] = lineno
|
2013-03-07 07:19:40 +00:00
|
|
|
m = match_found
|
2013-03-25 23:22:33 +00:00
|
|
|
elif insre is not None and insre.search(cur_line):
|
2013-02-11 06:46:06 +00:00
|
|
|
if insertafter:
|
|
|
|
# + 1 for the next line
|
|
|
|
index[1] = lineno + 1
|
|
|
|
if insertbefore:
|
|
|
|
# + 1 for the previous line
|
|
|
|
index[1] = lineno
|
2012-08-29 22:04:23 +00:00
|
|
|
|
2013-03-25 23:22:33 +00:00
|
|
|
msg = ''
|
|
|
|
changed = False
|
2012-08-29 22:04:23 +00:00
|
|
|
# Regexp matched a line in the file
|
|
|
|
if index[0] != -1:
|
2013-03-25 23:22:33 +00:00
|
|
|
if backrefs:
|
|
|
|
new_line = m.expand(line)
|
2012-08-29 22:04:23 +00:00
|
|
|
else:
|
2013-03-25 23:22:33 +00:00
|
|
|
# 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
|
2012-08-29 22:04:23 +00:00
|
|
|
msg = 'line replaced'
|
|
|
|
changed = True
|
2013-03-25 23:22:33 +00:00
|
|
|
elif backrefs:
|
|
|
|
# Do absolutely nothing, since it's not safe generating the line
|
|
|
|
# without the regexp matching to populate the backrefs.
|
|
|
|
pass
|
2012-08-29 22:04:23 +00:00
|
|
|
# Add it to the beginning of the file
|
2013-02-11 06:46:06 +00:00
|
|
|
elif insertbefore == 'BOF' or insertafter == 'BOF':
|
2012-08-29 22:04:23 +00:00
|
|
|
lines.insert(0, line + os.linesep)
|
|
|
|
msg = 'line added'
|
|
|
|
changed = True
|
2013-02-13 03:55:40 +00:00
|
|
|
# Add it to the end of the file if requested or
|
|
|
|
# if insertafter=/insertbefore didn't match anything
|
|
|
|
# (so default behaviour is to add at the end)
|
2013-03-22 10:43:44 +00:00
|
|
|
elif insertafter == 'EOF':
|
2012-08-29 22:04:23 +00:00
|
|
|
lines.append(line + os.linesep)
|
|
|
|
msg = 'line added'
|
|
|
|
changed = True
|
2013-03-25 23:22:33 +00:00
|
|
|
# Do nothing if insert* didn't match
|
2013-03-22 10:43:44 +00:00
|
|
|
elif index[1] == -1:
|
2013-03-25 23:22:33 +00:00
|
|
|
pass
|
|
|
|
# insert* matched, but not the regexp
|
2012-08-29 22:04:23 +00:00
|
|
|
else:
|
|
|
|
lines.insert(index[1], line + os.linesep)
|
|
|
|
msg = 'line added'
|
|
|
|
changed = True
|
|
|
|
|
2013-02-18 07:41:31 +00:00
|
|
|
if changed and not module.check_mode:
|
2012-11-07 15:33:43 +00:00
|
|
|
if backup and os.path.exists(dest):
|
2012-10-05 09:03:14 +00:00
|
|
|
module.backup_local(dest)
|
2013-05-08 02:08:29 +00:00
|
|
|
write_changes(module, lines, dest)
|
2012-08-29 22:04:23 +00:00
|
|
|
|
2013-03-25 23:22:33 +00:00
|
|
|
msg, changed = check_file_attrs(module, changed, msg)
|
2012-08-29 22:04:23 +00:00
|
|
|
module.exit_json(changed=changed, msg=msg)
|
|
|
|
|
2013-03-25 23:22:33 +00:00
|
|
|
|
2012-10-05 09:03:14 +00:00
|
|
|
def absent(module, dest, regexp, backup):
|
2013-02-22 08:33:21 +00:00
|
|
|
|
2013-05-08 02:08:29 +00:00
|
|
|
if not os.path.exists(dest):
|
2013-02-22 08:33:21 +00:00
|
|
|
module.exit_json(changed=False, msg="file not present")
|
|
|
|
|
|
|
|
msg = ""
|
2013-02-22 16:30:19 +00:00
|
|
|
|
2012-10-05 09:03:14 +00:00
|
|
|
f = open(dest, 'rb')
|
2012-08-29 22:04:23 +00:00
|
|
|
lines = f.readlines()
|
|
|
|
f.close()
|
|
|
|
cre = re.compile(regexp)
|
|
|
|
found = []
|
2012-10-31 00:42:07 +00:00
|
|
|
|
2012-08-29 22:04:23 +00:00
|
|
|
def matcher(line):
|
2012-09-08 18:53:34 +00:00
|
|
|
if cre.search(line):
|
2012-08-29 22:04:23 +00:00
|
|
|
found.append(line)
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
2012-10-31 00:42:07 +00:00
|
|
|
|
2012-08-29 22:04:23 +00:00
|
|
|
lines = filter(matcher, lines)
|
|
|
|
changed = len(found) > 0
|
2013-02-18 07:41:31 +00:00
|
|
|
if changed and not module.check_mode:
|
2012-09-04 12:20:52 +00:00
|
|
|
if backup:
|
2012-10-05 09:03:14 +00:00
|
|
|
module.backup_local(dest)
|
2013-05-08 02:08:29 +00:00
|
|
|
write_changes(module, lines, dest)
|
2013-02-22 08:33:21 +00:00
|
|
|
|
|
|
|
if changed:
|
|
|
|
msg = "%s line(s) removed" % len(found)
|
|
|
|
|
2013-03-25 23:22:33 +00:00
|
|
|
msg, changed = check_file_attrs(module, changed, msg)
|
2013-02-22 08:33:21 +00:00
|
|
|
module.exit_json(changed=changed, found=len(found), msg=msg)
|
2012-08-29 22:04:23 +00:00
|
|
|
|
2013-03-25 23:22:33 +00:00
|
|
|
|
2012-08-29 22:04:23 +00:00
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
2013-03-25 23:22:33 +00:00
|
|
|
argument_spec=dict(
|
2012-10-05 09:03:14 +00:00
|
|
|
dest=dict(required=True, aliases=['name', 'destfile']),
|
2012-08-29 22:04:23 +00:00
|
|
|
state=dict(default='present', choices=['absent', 'present']),
|
|
|
|
regexp=dict(required=True),
|
|
|
|
line=dict(aliases=['value']),
|
2013-02-13 03:55:40 +00:00
|
|
|
insertafter=dict(default=None),
|
|
|
|
insertbefore=dict(default=None),
|
2013-03-25 23:22:33 +00:00
|
|
|
backrefs=dict(default=False, type='bool'),
|
2013-02-23 21:56:45 +00:00
|
|
|
create=dict(default=False, type='bool'),
|
|
|
|
backup=dict(default=False, type='bool'),
|
2012-08-29 22:04:23 +00:00
|
|
|
),
|
2013-03-25 23:22:33 +00:00
|
|
|
mutually_exclusive=[['insertbefore', 'insertafter']],
|
|
|
|
add_file_common_args=True,
|
|
|
|
supports_check_mode=True
|
2012-08-29 22:04:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
params = module.params
|
2013-02-23 18:59:52 +00:00
|
|
|
create = module.params['create']
|
|
|
|
backup = module.params['backup']
|
2013-03-25 23:22:33 +00:00
|
|
|
backrefs = module.params['backrefs']
|
|
|
|
dest = os.path.expanduser(params['dest'])
|
2012-08-29 22:04:23 +00:00
|
|
|
|
2013-05-08 02:08:29 +00:00
|
|
|
|
|
|
|
if os.path.isdir(dest):
|
|
|
|
module.fail_json(rc=256, msg='Destination %s is a directory !' % dest)
|
|
|
|
|
2012-08-29 22:04:23 +00:00
|
|
|
if params['state'] == 'present':
|
2013-09-02 20:16:45 +00:00
|
|
|
if params.get('line', None) is None:
|
2012-08-29 22:04:23 +00:00
|
|
|
module.fail_json(msg='line= is required with state=present')
|
2013-03-25 23:22:33 +00:00
|
|
|
|
|
|
|
# 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'
|
|
|
|
|
2013-02-22 08:33:21 +00:00
|
|
|
present(module, dest, params['regexp'], params['line'],
|
2013-03-25 23:22:33 +00:00
|
|
|
ins_aft, ins_bef, create, backup, backrefs)
|
2012-08-29 22:04:23 +00:00
|
|
|
else:
|
2013-02-22 08:33:21 +00:00
|
|
|
absent(module, dest, params['regexp'], backup)
|
2012-08-29 22:04:23 +00:00
|
|
|
|
|
|
|
# this is magic, see lib/ansible/module_common.py
|
|
|
|
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
|
|
|
|
|
|
|
main()
|