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
|
|
|
|
|
2012-09-19 22:29:11 +02:00
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
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-09-28 03:06:31 +02:00
|
|
|
to remove.
|
|
|
|
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-09-28 03:06:31 +02:00
|
|
|
file. Must match the value given to C(regexp).
|
|
|
|
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
|
2012-09-28 03:06:31 +02:00
|
|
|
after the specified regular expression. Two special values are
|
|
|
|
available; C(BOF) for inserting the line at the beginning of the
|
|
|
|
file, and C(EOF) for inserting the line at the end of the file.
|
|
|
|
choices: [ BOF, EOF ]
|
|
|
|
default: EOF
|
|
|
|
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.
|
2012-09-19 22:29:11 +02:00
|
|
|
examples:
|
2012-10-05 11:03:14 +02:00
|
|
|
- code: lineinfile dest=/etc/selinux/config regexp=^SELINUX= line=SELINUX=disabled
|
|
|
|
- code: lineinfile dest=/etc/sudoers state=absent regexp="^%wheel"
|
2012-09-19 22:29:11 +02:00
|
|
|
'''
|
|
|
|
|
2012-10-05 11:03:14 +02:00
|
|
|
def present(module, dest, regexp, line, insertafter, backup):
|
|
|
|
f = open(dest, 'rb')
|
2012-08-30 00:04:23 +02:00
|
|
|
lines = f.readlines()
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
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)
|
2012-08-30 00:04:23 +02:00
|
|
|
|
|
|
|
if insertafter in ('BOF', 'EOF'):
|
|
|
|
iare = None
|
|
|
|
else:
|
|
|
|
iare = re.compile(insertafter)
|
|
|
|
|
|
|
|
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
|
2012-09-08 20:53:34 +02:00
|
|
|
elif iare is not None and iare.search(lines[lineno]):
|
2012-08-30 00:04:23 +02:00
|
|
|
# + 1 for the next line
|
|
|
|
index[1] = lineno + 1
|
|
|
|
|
|
|
|
# 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
|
|
|
|
elif insertafter == 'BOF':
|
|
|
|
lines.insert(0, line + os.linesep)
|
|
|
|
msg = 'line added'
|
|
|
|
changed = True
|
|
|
|
# Add it to the end of the file if requested or if insertafter= didn't match
|
|
|
|
elif insertafter == 'EOF' or index[1] == -1:
|
|
|
|
lines.append(line + os.linesep)
|
|
|
|
msg = 'line added'
|
|
|
|
changed = True
|
|
|
|
# insertafter= matched
|
|
|
|
else:
|
|
|
|
lines.insert(index[1], line + os.linesep)
|
|
|
|
msg = 'line added'
|
|
|
|
changed = True
|
|
|
|
|
|
|
|
if changed:
|
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()
|
|
|
|
|
|
|
|
module.exit_json(changed=changed, msg=msg)
|
|
|
|
|
2012-10-05 11:03:14 +02:00
|
|
|
def absent(module, dest, regexp, backup):
|
|
|
|
f = open(dest, 'rb')
|
2012-08-30 00:04:23 +02:00
|
|
|
lines = f.readlines()
|
|
|
|
f.close()
|
|
|
|
cre = re.compile(regexp)
|
|
|
|
found = []
|
|
|
|
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
|
|
|
|
lines = filter(matcher, lines)
|
|
|
|
changed = len(found) > 0
|
|
|
|
if changed:
|
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()
|
|
|
|
module.exit_json(changed=changed, found=len(found))
|
|
|
|
|
|
|
|
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']),
|
|
|
|
insertafter=dict(default='EOF'),
|
2012-09-04 14:20:52 +02:00
|
|
|
backup=dict(default=False, choices=BOOLEANS),
|
2012-08-30 00:04:23 +02:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
params = module.params
|
2012-09-04 14:20:52 +02:00
|
|
|
backup = module.boolean(module.params.get('backup', False))
|
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')
|
2012-10-05 11:03:14 +02:00
|
|
|
present(module, params['dest'], params['regexp'], params['line'],
|
2012-09-04 14:20:52 +02:00
|
|
|
params['insertafter'], backup)
|
2012-08-30 00:04:23 +02:00
|
|
|
else:
|
2012-10-05 11:03:14 +02:00
|
|
|
absent(module, params['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()
|
|
|
|
|