2012-08-30 00:04:23 +02: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-02-09 20:34:03 +01:00
|
|
|
DOCUMENTATION = """
|
2012-09-19 22:29:11 +02:00
|
|
|
---
|
|
|
|
module: lineinfile
|
2012-10-01 12:37:51 +02:00
|
|
|
author: Daniel Hokka Zakrisson
|
2012-09-19 22:29:11 +02:00
|
|
|
short_description: Ensure a particular line is in a file
|
|
|
|
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 11:03:14 +02:00
|
|
|
dest:
|
2012-09-28 03:06:31 +02:00
|
|
|
required: true
|
2012-10-05 11:03:14 +02:00
|
|
|
aliases: [ name, destfile ]
|
2012-09-28 03:06:31 +02:00
|
|
|
description:
|
|
|
|
- The file to modify
|
|
|
|
regexp:
|
|
|
|
required: true
|
|
|
|
description:
|
2012-10-01 09:18:54 +02:00
|
|
|
- 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
|
2012-12-20 17:41:45 +01:00
|
|
|
to remove. Uses Python regular expressions; see
|
|
|
|
U(http://docs.python.org/2/library/re.html).
|
2012-09-28 03:06:31 +02: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 09:18:54 +02:00
|
|
|
- Required for C(state=present). The line to insert/replace into the
|
2012-11-21 18:49:30 +01:00
|
|
|
file. Must match the value given to I(regexp).
|
2012-09-28 03:06:31 +02:00
|
|
|
insertafter:
|
|
|
|
required: false
|
|
|
|
default: EOF
|
|
|
|
description:
|
2012-10-01 09:18:54 +02:00
|
|
|
- Used with C(state=present). If specified, the line will be inserted
|
2013-02-11 07:46:06 +01:00
|
|
|
after the specified regular expression. A special value is
|
|
|
|
available; C(EOF) for inserting the line at the end of the file.
|
2013-02-16 21:40:31 +01:00
|
|
|
choices: [ 'EOF', '*regex*' ]
|
2013-02-11 07:46:06 +01:00
|
|
|
insertbefore:
|
|
|
|
required: false
|
2013-02-23 12:03:59 +01:00
|
|
|
version_added: 1.1
|
2013-02-11 07:46:06 +01:00
|
|
|
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.
|
2013-02-16 21:40:31 +01:00
|
|
|
choices: [ 'BOF', '*regex*' ]
|
2012-10-30 16:57:45 +01:00
|
|
|
create:
|
|
|
|
required: false
|
|
|
|
choices: [ yes, no ]
|
|
|
|
default: no
|
|
|
|
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 03:06:31 +02:00
|
|
|
backup:
|
|
|
|
required: false
|
|
|
|
default: no
|
|
|
|
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 09:33:21 +01:00
|
|
|
others:
|
|
|
|
description:
|
|
|
|
- All arguments accepted by the M(file) module also work here. If you
|
|
|
|
use file arguments with C(state=absent) and the file exists, it's perms,
|
|
|
|
ownership or SE linux context will be updated if needed.
|
|
|
|
required: false
|
2012-09-19 22:29:11 +02:00
|
|
|
examples:
|
2013-02-09 20:34:03 +01:00
|
|
|
- code: 'lineinfile: dest=/etc/selinux/config regexp=^SELINUX= line=SELINUX=disabled'
|
2012-10-23 15:14:01 +02:00
|
|
|
- code: 'lineinfile: dest=/etc/sudoers state=absent regexp="^%wheel"'
|
2013-02-22 09:33:21 +01:00
|
|
|
- code: "lineinfile: dest=/etc/host regexp='^127\.0\.0\.1' line='127.0.0.1 localhost' owner=root group=root mode=0644"
|
2013-02-06 09:25:30 +01:00
|
|
|
- code: 'lineinfile: dest=/etc/httpd/conf/httpd.conf regexp="^Listen " insertafter="^#Listen " line="Listen 8080"'
|
2013-02-11 07:46:06 +01:00
|
|
|
- code: 'lineinfile: dest=/etc/services regexp="^# port for http" insertbefore="^www.*80/tcp" line="# port for http by default"'
|
2013-02-16 21:40:31 +01:00
|
|
|
- code: "lineinfile: \\\"dest=/etc/sudoers state=present regexp='^%wheel' line ='%wheel ALL=(ALL) NOPASSWD: ALL'\\\""
|
2013-02-09 20:34:03 +01:00
|
|
|
"""
|
2013-02-16 21:40:31 +01:00
|
|
|
|
2012-09-19 22:29:11 +02:00
|
|
|
|
2013-02-22 17:30:19 +01:00
|
|
|
def check_file_attrs(module, changed, message):
|
2013-02-22 09:33:21 +01:00
|
|
|
|
|
|
|
file_args = module.load_file_common_arguments(module.params)
|
|
|
|
if module.set_file_attributes_if_different(file_args, False):
|
2013-02-22 17:30:19 +01:00
|
|
|
|
2013-02-22 09:33:21 +01:00
|
|
|
if changed:
|
|
|
|
message += " and "
|
|
|
|
changed = True
|
|
|
|
message += "ownership, perms or SE linux context changed"
|
|
|
|
|
|
|
|
return [ message, changed ]
|
|
|
|
|
2013-02-11 07:46:06 +01:00
|
|
|
def present(module, dest, regexp, line, insertafter, insertbefore, create, backup):
|
2012-10-30 16:57:45 +01:00
|
|
|
|
|
|
|
if os.path.isdir(dest):
|
|
|
|
module.fail_json(rc=256, msg='Destination %s is a directory !' % dest)
|
|
|
|
elif not os.path.exists(dest):
|
|
|
|
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-30 00:04:23 +02:00
|
|
|
|
2013-02-22 17:30:19 +01:00
|
|
|
msg = ""
|
|
|
|
|
2012-08-30 00:04:23 +02:00
|
|
|
mre = re.compile(regexp)
|
2012-09-08 03:03:30 +02:00
|
|
|
if not mre.search(line):
|
|
|
|
module.fail_json(msg="usage error: line= doesn't match regexp (%s)" % regexp)
|
2013-02-11 07:46:06 +01:00
|
|
|
|
2013-02-13 04:55:40 +01:00
|
|
|
if insertafter is not None and insertafter not in ('BOF', 'EOF'):
|
2013-02-11 07:46:06 +01:00
|
|
|
insre = re.compile(insertafter)
|
2013-02-13 04:55:40 +01:00
|
|
|
elif insertbefore is not None and insertbefore not in ('BOF'):
|
|
|
|
insre = re.compile(insertbefore)
|
|
|
|
else:
|
|
|
|
insre = None
|
2012-08-30 00:04:23 +02:00
|
|
|
|
2013-02-11 07:46:06 +01: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-30 00:04:23 +02:00
|
|
|
index = [-1, -1]
|
|
|
|
for lineno in range(0, len(lines)):
|
2012-09-08 20:53:34 +02:00
|
|
|
if mre.search(lines[lineno]):
|
2012-08-30 00:04:23 +02:00
|
|
|
index[0] = lineno
|
2013-02-11 07:46:06 +01:00
|
|
|
elif insre is not None and insre.search(lines[lineno]):
|
|
|
|
if insertafter:
|
|
|
|
# + 1 for the next line
|
|
|
|
index[1] = lineno + 1
|
|
|
|
if insertbefore:
|
|
|
|
# + 1 for the previous line
|
|
|
|
index[1] = lineno
|
2012-08-30 00:04:23 +02:00
|
|
|
|
|
|
|
# Regexp matched a line in the file
|
|
|
|
if index[0] != -1:
|
|
|
|
if lines[index[0]] == line + os.linesep:
|
|
|
|
msg = ''
|
|
|
|
changed = False
|
|
|
|
else:
|
|
|
|
lines[index[0]] = line + os.linesep
|
|
|
|
msg = 'line replaced'
|
|
|
|
changed = True
|
|
|
|
# Add it to the beginning of the file
|
2013-02-11 07:46:06 +01:00
|
|
|
elif insertbefore == 'BOF' or insertafter == 'BOF':
|
2012-08-30 00:04:23 +02:00
|
|
|
lines.insert(0, line + os.linesep)
|
|
|
|
msg = 'line added'
|
|
|
|
changed = True
|
2013-02-13 04:55:40 +01: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)
|
2012-08-30 00:04:23 +02:00
|
|
|
elif insertafter == 'EOF' or index[1] == -1:
|
|
|
|
lines.append(line + os.linesep)
|
|
|
|
msg = 'line added'
|
|
|
|
changed = True
|
2013-02-11 07:46:06 +01:00
|
|
|
# insertafter/insertbefore= matched
|
2012-08-30 00:04:23 +02:00
|
|
|
else:
|
|
|
|
lines.insert(index[1], line + os.linesep)
|
|
|
|
msg = 'line added'
|
|
|
|
changed = True
|
|
|
|
|
2013-02-18 08:41:31 +01:00
|
|
|
if changed and not module.check_mode:
|
2012-11-07 16:33:43 +01:00
|
|
|
if backup and os.path.exists(dest):
|
2012-10-05 11:03:14 +02:00
|
|
|
module.backup_local(dest)
|
|
|
|
f = open(dest, 'wb')
|
2012-08-30 00:04:23 +02:00
|
|
|
f.writelines(lines)
|
|
|
|
f.close()
|
|
|
|
|
2013-02-22 17:30:19 +01:00
|
|
|
[ msg, changed ] = check_file_attrs(module, changed, msg)
|
2012-08-30 00:04:23 +02:00
|
|
|
module.exit_json(changed=changed, msg=msg)
|
|
|
|
|
2012-10-05 11:03:14 +02:00
|
|
|
def absent(module, dest, regexp, backup):
|
2013-02-22 09:33:21 +01:00
|
|
|
|
|
|
|
if os.path.isdir(dest):
|
|
|
|
module.fail_json(rc=256, msg='Destination %s is a directory !' % dest)
|
|
|
|
elif not os.path.exists(dest):
|
|
|
|
module.exit_json(changed=False, msg="file not present")
|
|
|
|
|
|
|
|
msg = ""
|
2013-02-22 17:30:19 +01:00
|
|
|
|
2012-10-05 11:03:14 +02:00
|
|
|
f = open(dest, 'rb')
|
2012-08-30 00:04:23 +02:00
|
|
|
lines = f.readlines()
|
|
|
|
f.close()
|
|
|
|
cre = re.compile(regexp)
|
|
|
|
found = []
|
2012-10-31 01:42:07 +01:00
|
|
|
|
2012-08-30 00:04:23 +02:00
|
|
|
def matcher(line):
|
2012-09-08 20:53:34 +02:00
|
|
|
if cre.search(line):
|
2012-08-30 00:04:23 +02:00
|
|
|
found.append(line)
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
2012-10-31 01:42:07 +01:00
|
|
|
|
2012-08-30 00:04:23 +02:00
|
|
|
lines = filter(matcher, lines)
|
|
|
|
changed = len(found) > 0
|
2013-02-18 08:41:31 +01:00
|
|
|
if changed and not module.check_mode:
|
2012-09-04 14:20:52 +02:00
|
|
|
if backup:
|
2012-10-05 11:03:14 +02:00
|
|
|
module.backup_local(dest)
|
|
|
|
f = open(dest, 'wb')
|
2012-08-30 00:04:23 +02:00
|
|
|
f.writelines(lines)
|
|
|
|
f.close()
|
2013-02-22 09:33:21 +01:00
|
|
|
|
|
|
|
if changed:
|
|
|
|
msg = "%s line(s) removed" % len(found)
|
|
|
|
|
2013-02-22 17:30:19 +01:00
|
|
|
[ msg, changed ] = check_file_attrs(module, changed, msg)
|
2013-02-22 09:33:21 +01:00
|
|
|
module.exit_json(changed=changed, found=len(found), msg=msg)
|
2012-08-30 00:04:23 +02:00
|
|
|
|
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec = dict(
|
2012-10-05 11:03:14 +02:00
|
|
|
dest=dict(required=True, aliases=['name', 'destfile']),
|
2012-08-30 00:04:23 +02:00
|
|
|
state=dict(default='present', choices=['absent', 'present']),
|
|
|
|
regexp=dict(required=True),
|
|
|
|
line=dict(aliases=['value']),
|
2013-02-13 04:55:40 +01:00
|
|
|
insertafter=dict(default=None),
|
|
|
|
insertbefore=dict(default=None),
|
2013-02-23 22:56:45 +01:00
|
|
|
create=dict(default=False, type='bool'),
|
|
|
|
backup=dict(default=False, type='bool'),
|
2012-08-30 00:04:23 +02:00
|
|
|
),
|
2013-02-23 12:03:59 +01:00
|
|
|
mutually_exclusive = [['insertbefore', 'insertafter']],
|
2013-02-23 19:59:52 +01:00
|
|
|
add_file_common_args = True,
|
2013-02-23 12:03:59 +01:00
|
|
|
supports_check_mode = True
|
2012-08-30 00:04:23 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
params = module.params
|
2013-02-23 19:59:52 +01:00
|
|
|
create = module.params['create']
|
|
|
|
backup = module.params['backup']
|
2013-02-22 09:33:21 +01:00
|
|
|
dest = os.path.expanduser(params['dest'])
|
2012-08-30 00:04:23 +02:00
|
|
|
|
|
|
|
if params['state'] == 'present':
|
|
|
|
if 'line' not in params:
|
|
|
|
module.fail_json(msg='line= is required with state=present')
|
2013-02-22 09:33:21 +01:00
|
|
|
present(module, dest, params['regexp'], params['line'],
|
2013-02-11 07:46:06 +01:00
|
|
|
params['insertafter'], params['insertbefore'], create, backup)
|
2012-08-30 00:04:23 +02:00
|
|
|
else:
|
2013-02-22 09:33:21 +01:00
|
|
|
absent(module, dest, params['regexp'], backup)
|
2012-08-30 00:04:23 +02:00
|
|
|
|
|
|
|
# this is magic, see lib/ansible/module_common.py
|
|
|
|
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
|
|
|
|
|
|
|
main()
|
|
|
|
|