2012-08-30 00:04:23 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# (c) 2012, Daniel Hokka Zakrisson <daniel@hozac.com>
|
2014-06-09 18:58:45 +02:00
|
|
|
# (c) 2014, Ahti Kitsik <ak@ahtik.com>
|
2012-08-30 00:04:23 +02:00
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
2013-02-09 20:34:03 +01:00
|
|
|
DOCUMENTATION = """
|
2012-09-19 22:29:11 +02:00
|
|
|
---
|
|
|
|
module: lineinfile
|
2015-07-24 23:57:13 +02:00
|
|
|
author:
|
2015-06-15 21:53:30 +02:00
|
|
|
- "Daniel Hokka Zakrissoni (@dhozac)"
|
|
|
|
- "Ahti Kitsik (@ahtik)"
|
2015-07-24 23:57:13 +02:00
|
|
|
extends_documentation_fragment:
|
|
|
|
- files
|
|
|
|
- validate
|
2013-03-26 22:00:34 +01: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 22:29:11 +02:00
|
|
|
description:
|
|
|
|
- This module will search a file for a line, and ensure that it is present or absent.
|
2014-09-30 19:52:03 +02:00
|
|
|
- This is primarily useful when you want to change a single line in
|
|
|
|
a file only. See the M(replace) module if you want to change
|
2016-05-24 14:49:09 +02:00
|
|
|
multiple, similar lines or check M(blockinfile) if you want to insert/update/remove a block of lines in a file.
|
|
|
|
For other cases, see the M(copy) or M(template) modules.
|
2012-09-19 22:29:11 +02:00
|
|
|
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:
|
2013-03-26 22:00:34 +01:00
|
|
|
- The file to modify.
|
2012-09-28 03:06:31 +02:00
|
|
|
regexp:
|
2013-10-24 03:04:18 +02:00
|
|
|
required: false
|
2014-08-04 13:51:29 +02:00
|
|
|
version_added: 1.7
|
2012-09-28 03:06:31 +02:00
|
|
|
description:
|
2013-03-26 22:00:34 +01: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 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
|
2014-02-16 05:26:21 +01:00
|
|
|
file. If C(backrefs) is set, may contain backreferences that will get
|
2015-05-15 23:13:27 +02:00
|
|
|
expanded with the C(regexp) capture groups if the regexp matches.
|
2013-03-26 22:00:34 +01:00
|
|
|
backrefs:
|
|
|
|
required: false
|
|
|
|
default: "no"
|
|
|
|
choices: [ "yes", "no" ]
|
2013-03-30 20:44:34 +01:00
|
|
|
version_added: "1.1"
|
2013-03-26 22:00:34 +01:00
|
|
|
description:
|
|
|
|
- Used with C(state=present). If set, line can contain backreferences
|
2014-02-16 05:26:21 +01:00
|
|
|
(both positional and named) that will get populated if the C(regexp)
|
2013-03-26 22:00:34 +01:00
|
|
|
matches. This flag changes the operation of the module slightly;
|
2014-02-16 05:26:21 +01:00
|
|
|
C(insertbefore) and C(insertafter) will be ignored, and if the C(regexp)
|
2013-03-26 22:00:34 +01:00
|
|
|
doesn't match anywhere in the file, the file will be left unchanged.
|
2014-02-16 05:26:21 +01:00
|
|
|
If the C(regexp) does match, the last matching line will be replaced by
|
2013-03-26 22:00:34 +01:00
|
|
|
the expanded line parameter.
|
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
|
2014-12-04 17:07:03 +01:00
|
|
|
after the last match of specified regular expression. A special value is
|
2016-05-24 14:49:09 +02:00
|
|
|
available; C(EOF) for inserting the line at the end of the file.
|
2016-02-15 16:03:40 +01:00
|
|
|
If specified regular expression has no matches, EOF will be used instead.
|
2014-02-16 05:26:21 +01:00
|
|
|
May not be used with C(backrefs).
|
2013-02-16 21:40:31 +01:00
|
|
|
choices: [ 'EOF', '*regex*' ]
|
2013-02-11 07:46:06 +01:00
|
|
|
insertbefore:
|
|
|
|
required: false
|
2013-03-30 20:44:34 +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
|
2016-05-24 14:49:09 +02:00
|
|
|
before the last match of specified regular expression. A value is
|
2014-12-04 17:07:03 +01:00
|
|
|
available; C(BOF) for inserting the line at the beginning of the file.
|
2016-02-15 16:03:40 +01:00
|
|
|
If specified regular expression has no matches, the line will be
|
2015-03-03 20:38:08 +01:00
|
|
|
inserted at the end of the file. May not be used with C(backrefs).
|
2013-02-16 21:40:31 +01:00
|
|
|
choices: [ 'BOF', '*regex*' ]
|
2012-10-30 16:57:45 +01:00
|
|
|
create:
|
|
|
|
required: false
|
2013-03-12 13:18:12 +01:00
|
|
|
choices: [ "yes", "no" ]
|
|
|
|
default: "no"
|
2012-10-30 16:57:45 +01: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 03:06:31 +02:00
|
|
|
backup:
|
|
|
|
required: false
|
2013-03-12 13:18:12 +01:00
|
|
|
default: "no"
|
|
|
|
choices: [ "yes", "no" ]
|
2012-09-28 03:06:31 +02: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 09:33:21 +01:00
|
|
|
others:
|
|
|
|
description:
|
2013-03-26 00:22:33 +01:00
|
|
|
- All arguments accepted by the M(file) module also work here.
|
2013-02-22 09:33:21 +01:00
|
|
|
required: false
|
2013-02-23 21:08:33 +01:00
|
|
|
"""
|
|
|
|
|
2013-03-26 22:00:34 +01:00
|
|
|
EXAMPLES = r"""
|
2014-09-30 14:12:23 +02:00
|
|
|
- lineinfile: dest=/etc/selinux/config regexp=^SELINUX= line=SELINUX=enforcing
|
2013-02-23 21:08:33 +01:00
|
|
|
|
2013-06-14 11:53:43 +02:00
|
|
|
- lineinfile: dest=/etc/sudoers state=absent regexp="^%wheel"
|
2013-02-23 21:08:33 +01:00
|
|
|
|
2013-06-14 11:53:43 +02: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 21:08:33 +01:00
|
|
|
|
2013-06-14 11:53:43 +02:00
|
|
|
- lineinfile: dest=/etc/httpd/conf/httpd.conf regexp="^Listen " insertafter="^#Listen " line="Listen 8080"
|
2013-02-23 21:08:33 +01:00
|
|
|
|
2013-06-14 11:53:43 +02:00
|
|
|
- lineinfile: dest=/etc/services regexp="^# port for http" insertbefore="^www.*80/tcp" line="# port for http by default"
|
2013-02-23 21:08:33 +01:00
|
|
|
|
2013-11-07 00:51:31 +01:00
|
|
|
# Add a line to a file if it does not exist, without passing regexp
|
|
|
|
- lineinfile: dest=/tmp/testfile line="192.168.1.99 foo.lab.net foo"
|
|
|
|
|
2013-09-06 03:37:54 +02: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 08:19:40 +01:00
|
|
|
|
2014-04-27 16:36:13 +02:00
|
|
|
- lineinfile: dest=/opt/jboss-as/bin/standalone.conf regexp='^(.*)Xms(\d+)m(.*)$' line='\1Xms${xms}m\3' backrefs=yes
|
2013-10-06 19:43:16 +02:00
|
|
|
|
2014-11-30 05:41:24 +01:00
|
|
|
# Validate the sudoers file before saving
|
2013-10-06 19:43:16 +02:00
|
|
|
- lineinfile: dest=/etc/sudoers state=present regexp='^%ADMIN ALL\=' line='%ADMIN ALL=(ALL) NOPASSWD:ALL' validate='visudo -cf %s'
|
2013-02-09 20:34:03 +01:00
|
|
|
"""
|
2013-03-26 00:22:33 +01:00
|
|
|
|
2016-08-26 10:05:53 +02:00
|
|
|
import re
|
|
|
|
import os
|
|
|
|
import tempfile
|
|
|
|
|
|
|
|
# import module snippets
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
from ansible.module_utils.six import b
|
|
|
|
from ansible.module_utils._text import to_bytes, to_native
|
|
|
|
|
|
|
|
|
|
|
|
def write_changes(module, b_lines, dest):
|
2013-05-08 04:08:29 +02:00
|
|
|
|
|
|
|
tmpfd, tmpfile = tempfile.mkstemp()
|
2016-08-26 10:05:53 +02:00
|
|
|
f = os.fdopen(tmpfd, 'wb')
|
|
|
|
f.writelines(b_lines)
|
2013-05-08 04:08:29 +02:00
|
|
|
f.close()
|
|
|
|
|
2013-10-06 19:43:16 +02:00
|
|
|
validate = module.params.get('validate', None)
|
|
|
|
valid = not validate
|
|
|
|
if validate:
|
2014-04-19 04:39:10 +02:00
|
|
|
if "%s" not in validate:
|
|
|
|
module.fail_json(msg="validate must contain %%s: %s" % (validate))
|
2016-09-01 13:19:15 +02:00
|
|
|
(rc, out, err) = module.run_command(to_bytes(validate % tmpfile, errors='surrogate_or_strict'))
|
2013-10-06 19:43:16 +02:00
|
|
|
valid = rc == 0
|
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(msg='failed to validate: '
|
2016-08-26 10:05:53 +02:00
|
|
|
'rc:%s error:%s' % (rc, err))
|
2013-10-06 19:43:16 +02:00
|
|
|
if valid:
|
2016-09-01 13:19:15 +02:00
|
|
|
module.atomic_move(tmpfile,
|
|
|
|
to_native(os.path.realpath(to_bytes(dest, errors='surrogate_or_strict')), errors='surrogate_or_strict'),
|
|
|
|
unsafe_writes=module.params['unsafe_writes'])
|
2012-09-19 22:29:11 +02:00
|
|
|
|
2016-08-26 10:05:53 +02:00
|
|
|
|
2016-01-27 12:33:24 +01:00
|
|
|
def check_file_attrs(module, changed, message, diff):
|
2013-02-22 09:33:21 +01:00
|
|
|
|
|
|
|
file_args = module.load_file_common_arguments(module.params)
|
2016-01-27 12:33:24 +01:00
|
|
|
if module.set_fs_attributes_if_different(file_args, False, diff=diff):
|
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"
|
|
|
|
|
2013-03-26 00:22:33 +01:00
|
|
|
return message, changed
|
|
|
|
|
2013-02-22 09:33:21 +01:00
|
|
|
|
2013-03-26 00:22:33 +01:00
|
|
|
def present(module, dest, regexp, line, insertafter, insertbefore, create,
|
|
|
|
backup, backrefs):
|
2012-10-30 16:57:45 +01:00
|
|
|
|
2016-01-27 12:33:24 +01:00
|
|
|
diff = {'before': '',
|
|
|
|
'after': '',
|
|
|
|
'before_header': '%s (content)' % dest,
|
|
|
|
'after_header': '%s (content)' % dest}
|
|
|
|
|
2016-09-01 13:19:15 +02:00
|
|
|
b_dest = to_bytes(dest, errors='surrogate_or_strict')
|
2016-08-26 10:05:53 +02:00
|
|
|
if not os.path.exists(b_dest):
|
2012-10-30 16:57:45 +01:00
|
|
|
if not create:
|
|
|
|
module.fail_json(rc=257, msg='Destination %s does not exist !' % dest)
|
2016-08-26 10:05:53 +02:00
|
|
|
b_destpath = os.path.dirname(b_dest)
|
|
|
|
if not os.path.exists(b_destpath) and not module.check_mode:
|
|
|
|
os.makedirs(b_destpath)
|
|
|
|
b_lines = []
|
2012-10-30 16:57:45 +01:00
|
|
|
else:
|
2016-08-26 10:05:53 +02:00
|
|
|
f = open(b_dest, 'rb')
|
|
|
|
b_lines = f.readlines()
|
2012-10-30 16:57:45 +01:00
|
|
|
f.close()
|
2012-08-30 00:04:23 +02:00
|
|
|
|
2016-01-27 12:33:24 +01:00
|
|
|
if module._diff:
|
2016-08-26 10:05:53 +02:00
|
|
|
diff['before'] = to_native(b('').join(b_lines))
|
2013-02-22 17:30:19 +01:00
|
|
|
|
2013-10-24 03:04:18 +02:00
|
|
|
if regexp is not None:
|
2016-09-01 13:19:15 +02:00
|
|
|
bre_m = re.compile(to_bytes(regexp, errors='surrogate_or_strict'))
|
2013-03-26 00:22:33 +01:00
|
|
|
|
|
|
|
if insertafter not in (None, 'BOF', 'EOF'):
|
2016-09-01 13:19:15 +02:00
|
|
|
bre_ins = re.compile(to_bytes(insertafter, errors='surrogate_or_strict'))
|
2013-03-26 00:22:33 +01:00
|
|
|
elif insertbefore not in (None, 'BOF'):
|
2016-09-01 13:19:15 +02:00
|
|
|
bre_ins = re.compile(to_bytes(insertbefore, errors='surrogate_or_strict'))
|
2013-02-13 04:55:40 +01:00
|
|
|
else:
|
2016-08-26 10:05:53 +02:00
|
|
|
bre_ins = 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]
|
2013-03-07 08:19:40 +01:00
|
|
|
m = None
|
2016-09-01 13:19:15 +02:00
|
|
|
b_line = to_bytes(line, errors='surrogate_or_strict')
|
2016-08-26 10:05:53 +02:00
|
|
|
for lineno, b_cur_line in enumerate(b_lines):
|
2013-10-24 03:04:18 +02:00
|
|
|
if regexp is not None:
|
2016-08-26 10:05:53 +02:00
|
|
|
match_found = bre_m.search(b_cur_line)
|
2013-10-24 03:04:18 +02:00
|
|
|
else:
|
2016-08-26 10:05:53 +02:00
|
|
|
match_found = b_line == b_cur_line.rstrip(b('\r\n'))
|
2013-03-07 08:19:40 +01:00
|
|
|
if match_found:
|
2012-08-30 00:04:23 +02:00
|
|
|
index[0] = lineno
|
2013-03-07 08:19:40 +01:00
|
|
|
m = match_found
|
2016-08-26 10:05:53 +02:00
|
|
|
elif bre_ins is not None and bre_ins.search(b_cur_line):
|
2013-02-11 07:46:06 +01:00
|
|
|
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
|
|
|
|
2013-03-26 00:22:33 +01:00
|
|
|
msg = ''
|
|
|
|
changed = False
|
2012-08-30 00:04:23 +02:00
|
|
|
# Regexp matched a line in the file
|
2016-09-01 13:19:15 +02:00
|
|
|
b_linesep = to_bytes(os.linesep, errors='surrogate_or_strict')
|
2012-08-30 00:04:23 +02:00
|
|
|
if index[0] != -1:
|
2013-03-26 00:22:33 +01:00
|
|
|
if backrefs:
|
2016-08-26 10:05:53 +02:00
|
|
|
b_new_line = m.expand(b_line)
|
2012-08-30 00:04:23 +02:00
|
|
|
else:
|
2013-03-26 00:22:33 +01:00
|
|
|
# Don't do backref expansion if not asked.
|
2016-08-26 10:05:53 +02:00
|
|
|
b_new_line = b_line
|
2013-03-26 00:22:33 +01:00
|
|
|
|
2016-08-26 10:05:53 +02:00
|
|
|
if not b_new_line.endswith(b_linesep):
|
|
|
|
b_new_line += b_linesep
|
2015-02-23 15:14:00 +01:00
|
|
|
|
2016-08-26 10:05:53 +02:00
|
|
|
if b_lines[index[0]] != b_new_line:
|
|
|
|
b_lines[index[0]] = b_new_line
|
2012-08-30 00:04:23 +02:00
|
|
|
msg = 'line replaced'
|
|
|
|
changed = True
|
2013-03-26 00:22:33 +01: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-30 00:04:23 +02:00
|
|
|
# Add it to the beginning of the file
|
2013-02-11 07:46:06 +01:00
|
|
|
elif insertbefore == 'BOF' or insertafter == 'BOF':
|
2016-08-26 10:05:53 +02:00
|
|
|
b_lines.insert(0, b_line + b_linesep)
|
2012-08-30 00:04:23 +02:00
|
|
|
msg = 'line added'
|
|
|
|
changed = True
|
2013-02-13 04:55:40 +01:00
|
|
|
# Add it to the end of the file if requested or
|
2014-11-30 12:32:08 +01:00
|
|
|
# if insertafter/insertbefore didn't match anything
|
2013-02-13 04:55:40 +01:00
|
|
|
# (so default behaviour is to add at the end)
|
2014-11-30 12:32:08 +01:00
|
|
|
elif insertafter == 'EOF' or index[1] == -1:
|
2014-06-09 18:58:45 +02:00
|
|
|
|
|
|
|
# If the file is not empty then ensure there's a newline before the added line
|
2016-08-26 10:05:53 +02:00
|
|
|
if len(b_lines) > 0 and not b_lines[-1][-1:] in (b('\n'), b('\r')):
|
|
|
|
b_lines.append(b_linesep)
|
2014-06-09 18:58:45 +02:00
|
|
|
|
2016-08-26 10:05:53 +02:00
|
|
|
b_lines.append(b_line + b_linesep)
|
2012-08-30 00:04:23 +02:00
|
|
|
msg = 'line added'
|
|
|
|
changed = True
|
2013-03-26 00:22:33 +01:00
|
|
|
# insert* matched, but not the regexp
|
2012-08-30 00:04:23 +02:00
|
|
|
else:
|
2016-08-26 10:05:53 +02:00
|
|
|
b_lines.insert(index[1], b_line + b_linesep)
|
2012-08-30 00:04:23 +02:00
|
|
|
msg = 'line added'
|
|
|
|
changed = True
|
|
|
|
|
2016-01-27 12:33:24 +01:00
|
|
|
if module._diff:
|
2016-08-26 10:05:53 +02:00
|
|
|
diff['after'] = to_native(b('').join(b_lines))
|
2016-01-27 12:33:24 +01:00
|
|
|
|
2014-02-20 19:30:28 +01:00
|
|
|
backupdest = ""
|
2013-02-18 08:41:31 +01:00
|
|
|
if changed and not module.check_mode:
|
2016-08-26 10:05:53 +02:00
|
|
|
if backup and os.path.exists(b_dest):
|
2014-02-20 19:30:28 +01:00
|
|
|
backupdest = module.backup_local(dest)
|
2016-08-26 10:05:53 +02:00
|
|
|
write_changes(module, b_lines, dest)
|
2012-08-30 00:04:23 +02:00
|
|
|
|
2016-08-26 10:05:53 +02:00
|
|
|
if module.check_mode and not os.path.exists(b_dest):
|
2016-01-27 12:33:24 +01:00
|
|
|
module.exit_json(changed=changed, msg=msg, backup=backupdest, diff=diff)
|
|
|
|
|
|
|
|
attr_diff = {}
|
|
|
|
msg, changed = check_file_attrs(module, changed, msg, attr_diff)
|
2014-12-15 02:48:36 +01:00
|
|
|
|
2016-01-27 12:33:24 +01:00
|
|
|
attr_diff['before_header'] = '%s (file attributes)' % dest
|
|
|
|
attr_diff['after_header'] = '%s (file attributes)' % dest
|
|
|
|
|
|
|
|
difflist = [diff, attr_diff]
|
|
|
|
module.exit_json(changed=changed, msg=msg, backup=backupdest, diff=difflist)
|
2012-08-30 00:04:23 +02:00
|
|
|
|
2013-03-26 00:22:33 +01:00
|
|
|
|
2013-10-24 03:04:18 +02:00
|
|
|
def absent(module, dest, regexp, line, backup):
|
2013-02-22 09:33:21 +01:00
|
|
|
|
2016-09-01 13:19:15 +02:00
|
|
|
b_dest = to_bytes(dest, errors='surrogate_or_strict')
|
2016-08-26 10:05:53 +02:00
|
|
|
if not os.path.exists(b_dest):
|
2013-02-22 09:33:21 +01:00
|
|
|
module.exit_json(changed=False, msg="file not present")
|
|
|
|
|
2016-01-27 12:33:24 +01:00
|
|
|
msg = ''
|
|
|
|
diff = {'before': '',
|
|
|
|
'after': '',
|
|
|
|
'before_header': '%s (content)' % dest,
|
|
|
|
'after_header': '%s (content)' % dest}
|
2013-02-22 17:30:19 +01:00
|
|
|
|
2016-08-26 10:05:53 +02:00
|
|
|
f = open(b_dest, 'rb')
|
|
|
|
b_lines = f.readlines()
|
2012-08-30 00:04:23 +02:00
|
|
|
f.close()
|
2016-01-27 12:33:24 +01:00
|
|
|
|
|
|
|
if module._diff:
|
2016-08-26 10:05:53 +02:00
|
|
|
diff['before'] = to_native(b('').join(b_lines))
|
2016-01-27 12:33:24 +01:00
|
|
|
|
2013-10-24 03:04:18 +02:00
|
|
|
if regexp is not None:
|
2016-09-01 13:19:15 +02:00
|
|
|
bre_c = re.compile(to_bytes(regexp, errors='surrogate_or_strict'))
|
2012-08-30 00:04:23 +02:00
|
|
|
found = []
|
2012-10-31 01:42:07 +01:00
|
|
|
|
2016-09-01 13:19:15 +02:00
|
|
|
b_line = to_bytes(line, errors='surrogate_or_strict')
|
2016-08-26 10:05:53 +02:00
|
|
|
def matcher(b_cur_line):
|
2013-10-24 03:04:18 +02:00
|
|
|
if regexp is not None:
|
2016-08-26 10:05:53 +02:00
|
|
|
match_found = bre_c.search(b_cur_line)
|
2012-08-30 00:04:23 +02:00
|
|
|
else:
|
2016-08-26 10:05:53 +02:00
|
|
|
match_found = b_line == b_cur_line.rstrip(b('\r\n'))
|
2013-10-24 03:04:18 +02:00
|
|
|
if match_found:
|
2016-08-26 10:05:53 +02:00
|
|
|
found.append(b_cur_line)
|
2013-10-24 03:04:18 +02:00
|
|
|
return not match_found
|
2012-10-31 01:42:07 +01:00
|
|
|
|
2016-08-26 10:05:53 +02:00
|
|
|
b_lines = [l for l in b_lines if matcher(l)]
|
2012-08-30 00:04:23 +02:00
|
|
|
changed = len(found) > 0
|
2016-01-27 12:33:24 +01:00
|
|
|
|
|
|
|
if module._diff:
|
2016-08-26 10:05:53 +02:00
|
|
|
diff['after'] = to_native(b('').join(b_lines))
|
2016-01-27 12:33:24 +01:00
|
|
|
|
2014-02-20 19:30:28 +01:00
|
|
|
backupdest = ""
|
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:
|
2014-02-20 19:30:28 +01:00
|
|
|
backupdest = module.backup_local(dest)
|
2016-08-26 10:05:53 +02:00
|
|
|
write_changes(module, b_lines, dest)
|
2013-02-22 09:33:21 +01:00
|
|
|
|
|
|
|
if changed:
|
|
|
|
msg = "%s line(s) removed" % len(found)
|
|
|
|
|
2016-01-27 12:33:24 +01:00
|
|
|
attr_diff = {}
|
|
|
|
msg, changed = check_file_attrs(module, changed, msg, attr_diff)
|
|
|
|
|
|
|
|
attr_diff['before_header'] = '%s (file attributes)' % dest
|
|
|
|
attr_diff['after_header'] = '%s (file attributes)' % dest
|
|
|
|
|
|
|
|
difflist = [diff, attr_diff]
|
|
|
|
|
|
|
|
module.exit_json(changed=changed, found=len(found), msg=msg, backup=backupdest, diff=difflist)
|
2012-08-30 00:04:23 +02:00
|
|
|
|
2013-03-26 00:22:33 +01:00
|
|
|
|
2012-08-30 00:04:23 +02:00
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
2013-03-26 00:22:33 +01:00
|
|
|
argument_spec=dict(
|
2016-08-26 10:05:53 +02:00
|
|
|
dest=dict(required=True, aliases=['name', 'destfile'], type='path'),
|
2012-08-30 00:04:23 +02:00
|
|
|
state=dict(default='present', choices=['absent', 'present']),
|
2013-10-24 03:04:18 +02:00
|
|
|
regexp=dict(default=None),
|
2012-08-30 00:04:23 +02:00
|
|
|
line=dict(aliases=['value']),
|
2013-02-13 04:55:40 +01:00
|
|
|
insertafter=dict(default=None),
|
|
|
|
insertbefore=dict(default=None),
|
2013-03-26 00:22:33 +01:00
|
|
|
backrefs=dict(default=False, type='bool'),
|
2013-02-23 22:56:45 +01:00
|
|
|
create=dict(default=False, type='bool'),
|
|
|
|
backup=dict(default=False, type='bool'),
|
2013-10-06 19:43:16 +02:00
|
|
|
validate=dict(default=None, type='str'),
|
2012-08-30 00:04:23 +02:00
|
|
|
),
|
2013-03-26 00:22:33 +01:00
|
|
|
mutually_exclusive=[['insertbefore', 'insertafter']],
|
|
|
|
add_file_common_args=True,
|
|
|
|
supports_check_mode=True
|
2012-08-30 00:04:23 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
params = module.params
|
2016-08-26 10:05:53 +02:00
|
|
|
create = params['create']
|
|
|
|
backup = params['backup']
|
|
|
|
backrefs = params['backrefs']
|
|
|
|
dest = params['dest']
|
2013-05-08 04:08:29 +02:00
|
|
|
|
2016-09-01 13:19:15 +02:00
|
|
|
b_dest = to_bytes(dest, errors='surrogate_or_strict')
|
2016-08-26 10:05:53 +02:00
|
|
|
if os.path.isdir(b_dest):
|
2013-05-08 04:08:29 +02:00
|
|
|
module.fail_json(rc=256, msg='Destination %s is a directory !' % dest)
|
|
|
|
|
2012-08-30 00:04:23 +02:00
|
|
|
if params['state'] == 'present':
|
2013-10-24 03:04:18 +02:00
|
|
|
if backrefs and params['regexp'] is None:
|
|
|
|
module.fail_json(msg='regexp= is required with backrefs=true')
|
|
|
|
|
2013-09-02 22:16:45 +02:00
|
|
|
if params.get('line', None) is None:
|
2012-08-30 00:04:23 +02:00
|
|
|
module.fail_json(msg='line= is required with state=present')
|
2013-03-26 00:22:33 +01: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'
|
|
|
|
|
2014-08-11 18:22:59 +02:00
|
|
|
line = params['line']
|
|
|
|
|
2014-01-19 02:24:26 +01:00
|
|
|
present(module, dest, params['regexp'], line,
|
2013-03-26 00:22:33 +01:00
|
|
|
ins_aft, ins_bef, create, backup, backrefs)
|
2012-08-30 00:04:23 +02:00
|
|
|
else:
|
2013-10-24 03:04:18 +02:00
|
|
|
if params['regexp'] is None and params.get('line', None) is None:
|
|
|
|
module.fail_json(msg='one of line= or regexp= is required with state=absent')
|
|
|
|
|
|
|
|
absent(module, dest, params['regexp'], params.get('line', None), backup)
|
2012-08-30 00:04:23 +02:00
|
|
|
|
2015-05-17 18:15:57 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|