2014-09-26 01:01:01 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# (c) 2013, Evan Kaufman <evan@digitalflophouse.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/>.
|
|
|
|
|
2017-03-14 16:07:22 +00:00
|
|
|
ANSIBLE_METADATA = {'metadata_version': '1.0',
|
|
|
|
'status': ['stableinterface'],
|
|
|
|
'supported_by': 'community'}
|
|
|
|
|
2016-12-06 10:35:05 +00:00
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
DOCUMENTATION = """
|
|
|
|
---
|
|
|
|
module: replace
|
2015-06-15 19:53:30 +00:00
|
|
|
author: "Evan Kaufman (@EvanK)"
|
2016-12-08 02:33:38 +00:00
|
|
|
extends_documentation_fragment:
|
|
|
|
- files
|
|
|
|
- validate
|
2014-09-26 01:01:01 +00:00
|
|
|
short_description: Replace all instances of a particular string in a
|
|
|
|
file using a back-referenced regular expression.
|
|
|
|
description:
|
|
|
|
- This module will replace all instances of a pattern within a file.
|
|
|
|
- It is up to the user to maintain idempotence by ensuring that the
|
|
|
|
same pattern would never match any replacements made.
|
|
|
|
version_added: "1.6"
|
|
|
|
options:
|
2017-01-03 12:47:00 +00:00
|
|
|
path:
|
2014-09-26 01:01:01 +00:00
|
|
|
required: true
|
2017-01-03 12:47:00 +00:00
|
|
|
aliases: [ dest, destfile, name ]
|
2014-09-26 01:01:01 +00:00
|
|
|
description:
|
|
|
|
- The file to modify.
|
2017-01-03 12:47:00 +00:00
|
|
|
- Before 2.3 this option was only usable as I(dest), I(destfile) and I(name).
|
2014-09-26 01:01:01 +00:00
|
|
|
regexp:
|
|
|
|
required: true
|
|
|
|
description:
|
|
|
|
- The regular expression to look for in the contents of the file.
|
|
|
|
Uses Python regular expressions; see
|
|
|
|
U(http://docs.python.org/2/library/re.html).
|
|
|
|
Uses multiline mode, which means C(^) and C($) match the beginning
|
|
|
|
and end respectively of I(each line) of the file.
|
2017-03-21 20:03:00 +00:00
|
|
|
- Note that, as of ansible 2, short form tasks should have any escape
|
|
|
|
sequences backslash-escaped in order to prevent them being parsed
|
|
|
|
as string literal escapes. See the examples.
|
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
replace:
|
|
|
|
required: false
|
|
|
|
description:
|
|
|
|
- The string to replace regexp matches. May contain backreferences
|
|
|
|
that will get expanded with the regexp capture groups if the regexp
|
|
|
|
matches. If not set, matches are removed entirely.
|
2017-03-12 00:49:39 +00:00
|
|
|
after:
|
|
|
|
required: false
|
2017-03-16 21:36:03 +00:00
|
|
|
version_added: "2.4"
|
2017-03-12 00:49:39 +00:00
|
|
|
description:
|
|
|
|
- If specified, the line after the replace/remove will start. Can be used
|
|
|
|
in combination with C(before).
|
|
|
|
Uses Python regular expressions; see
|
|
|
|
U(http://docs.python.org/2/library/re.html).
|
|
|
|
before:
|
|
|
|
required: false
|
2017-03-16 21:36:03 +00:00
|
|
|
version_added: "2.4"
|
2017-03-12 00:49:39 +00:00
|
|
|
description:
|
|
|
|
- If specified, the line before the replace/remove will occur. Can be used
|
|
|
|
in combination with C(after).
|
|
|
|
Uses Python regular expressions; see
|
|
|
|
U(http://docs.python.org/2/library/re.html).
|
2014-09-26 01:01:01 +00:00
|
|
|
backup:
|
|
|
|
required: false
|
|
|
|
default: "no"
|
|
|
|
choices: [ "yes", "no" ]
|
|
|
|
description:
|
|
|
|
- Create a backup file including the timestamp information so you can
|
|
|
|
get the original file back if you somehow clobbered it incorrectly.
|
|
|
|
others:
|
|
|
|
description:
|
|
|
|
- All arguments accepted by the M(file) module also work here.
|
|
|
|
required: false
|
2016-02-15 19:13:38 +00:00
|
|
|
follow:
|
|
|
|
required: false
|
|
|
|
default: "no"
|
|
|
|
choices: [ "yes", "no" ]
|
|
|
|
version_added: "1.9"
|
|
|
|
description:
|
|
|
|
- 'This flag indicates that filesystem links, if they exist, should be followed.'
|
2017-06-06 02:37:53 +00:00
|
|
|
encoding:
|
|
|
|
required: false
|
|
|
|
default: "utf-8"
|
|
|
|
version_added: "2.4"
|
|
|
|
description:
|
|
|
|
- "The character encoding for reading and writing the file."
|
2017-01-03 12:47:00 +00:00
|
|
|
notes:
|
|
|
|
- As of Ansible 2.3, the I(dest) option has been changed to I(path) as default, but I(dest) still works as well.
|
2014-09-26 01:01:01 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
EXAMPLES = r"""
|
2017-01-03 12:47:00 +00:00
|
|
|
# Before 2.3, option 'dest', 'destfile' or 'name' was used instead of 'path'
|
2016-10-12 21:36:22 +00:00
|
|
|
- replace:
|
2017-01-03 12:47:00 +00:00
|
|
|
path: /etc/hosts
|
2016-10-12 21:36:22 +00:00
|
|
|
regexp: '(\s+)old\.host\.name(\s+.*)?$'
|
|
|
|
replace: '\1new.host.name\2'
|
|
|
|
backup: yes
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2017-03-16 21:36:03 +00:00
|
|
|
# Replace after the expression till the end of the file (requires >=2.4)
|
2017-03-12 00:49:39 +00:00
|
|
|
- replace:
|
|
|
|
path: /etc/hosts
|
|
|
|
regexp: '(\s+)old\.host\.name(\s+.*)?$'
|
|
|
|
replace: '\1new.host.name\2'
|
|
|
|
after: 'Start after line.*'
|
|
|
|
backup: yes
|
|
|
|
|
2017-03-16 21:36:03 +00:00
|
|
|
# Replace before the expression till the begin of the file (requires >=2.4)
|
2017-03-12 00:49:39 +00:00
|
|
|
- replace:
|
|
|
|
path: /etc/hosts
|
|
|
|
regexp: '(\s+)old\.host\.name(\s+.*)?$'
|
|
|
|
replace: '\1new.host.name\2'
|
|
|
|
before: 'Start before line.*'
|
|
|
|
backup: yes
|
|
|
|
|
2017-03-16 21:36:03 +00:00
|
|
|
# Replace between the expressions (requires >=2.4)
|
2017-03-12 00:49:39 +00:00
|
|
|
- replace:
|
|
|
|
path: /etc/hosts
|
|
|
|
regexp: '(\s+)old\.host\.name(\s+.*)?$'
|
|
|
|
replace: '\1new.host.name\2'
|
|
|
|
after: 'Start after line.*'
|
|
|
|
before: 'Start before line.*'
|
|
|
|
backup: yes
|
|
|
|
|
2016-10-12 21:36:22 +00:00
|
|
|
- replace:
|
2017-01-03 12:47:00 +00:00
|
|
|
path: /home/jdoe/.ssh/known_hosts
|
2016-10-12 21:36:22 +00:00
|
|
|
regexp: '^old\.host\.name[^\n]*\n'
|
|
|
|
owner: jdoe
|
|
|
|
group: jdoe
|
|
|
|
mode: 0644
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2016-10-12 21:36:22 +00:00
|
|
|
- replace:
|
2017-01-03 12:47:00 +00:00
|
|
|
path: /etc/apache/ports
|
2016-10-12 21:36:22 +00:00
|
|
|
regexp: '^(NameVirtualHost|Listen)\s+80\s*$'
|
|
|
|
replace: '\1 127.0.0.1:8080'
|
|
|
|
validate: '/usr/sbin/apache2ctl -f %s -t'
|
2017-03-21 20:03:00 +00:00
|
|
|
|
|
|
|
- name: short form task (in ansible 2+) necessitates backslash-escaped sequences
|
|
|
|
replace: dest=/etc/hosts regexp='\\b(localhost)(\\d*)\\b' replace='\\1\\2.localdomain\\2 \\1\\2'
|
|
|
|
|
|
|
|
- name: long form task does not
|
|
|
|
replace:
|
|
|
|
dest: /etc/hosts
|
|
|
|
regexp: '\b(localhost)(\d*)\b'
|
|
|
|
replace: '\1\2.localdomain\2 \1\2'
|
2014-09-26 01:01:01 +00:00
|
|
|
"""
|
|
|
|
|
2017-01-03 12:47:00 +00:00
|
|
|
import os
|
2017-02-02 19:45:22 +00:00
|
|
|
import re
|
|
|
|
import tempfile
|
2017-01-03 12:47:00 +00:00
|
|
|
|
2017-02-02 19:45:22 +00:00
|
|
|
from ansible.module_utils._text import to_text, to_bytes
|
2017-01-03 12:47:00 +00:00
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
|
2017-02-02 19:45:22 +00:00
|
|
|
|
2017-01-03 12:47:00 +00:00
|
|
|
def write_changes(module, contents, path):
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
tmpfd, tmpfile = tempfile.mkstemp()
|
|
|
|
f = os.fdopen(tmpfd,'wb')
|
2017-06-06 02:35:18 +00:00
|
|
|
f.write(contents)
|
2014-09-26 01:01:01 +00:00
|
|
|
f.close()
|
|
|
|
|
|
|
|
validate = module.params.get('validate', None)
|
|
|
|
valid = not validate
|
|
|
|
if validate:
|
|
|
|
if "%s" not in validate:
|
|
|
|
module.fail_json(msg="validate must contain %%s: %s" % (validate))
|
|
|
|
(rc, out, err) = module.run_command(validate % tmpfile)
|
|
|
|
valid = rc == 0
|
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(msg='failed to validate: '
|
|
|
|
'rc:%s error:%s' % (rc,err))
|
|
|
|
if valid:
|
2017-01-03 12:47:00 +00:00
|
|
|
module.atomic_move(tmpfile, path, unsafe_writes=module.params['unsafe_writes'])
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2017-03-12 00:49:39 +00:00
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
def check_file_attrs(module, changed, message):
|
|
|
|
|
|
|
|
file_args = module.load_file_common_arguments(module.params)
|
|
|
|
if module.set_file_attributes_if_different(file_args, False):
|
|
|
|
|
|
|
|
if changed:
|
|
|
|
message += " and "
|
|
|
|
changed = True
|
|
|
|
message += "ownership, perms or SE linux context changed"
|
|
|
|
|
|
|
|
return message, changed
|
|
|
|
|
2017-03-12 00:49:39 +00:00
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
2017-01-03 12:47:00 +00:00
|
|
|
path=dict(required=True, aliases=['dest', 'destfile', 'name'], type='path'),
|
2014-09-26 01:01:01 +00:00
|
|
|
regexp=dict(required=True),
|
|
|
|
replace=dict(default='', type='str'),
|
2017-03-12 00:49:39 +00:00
|
|
|
after=dict(required=False),
|
|
|
|
before=dict(required=False),
|
2014-09-26 01:01:01 +00:00
|
|
|
backup=dict(default=False, type='bool'),
|
|
|
|
validate=dict(default=None, type='str'),
|
2017-06-06 02:35:18 +00:00
|
|
|
encoding=dict(default='utf-8', type='str'),
|
2014-09-26 01:01:01 +00:00
|
|
|
),
|
|
|
|
add_file_common_args=True,
|
|
|
|
supports_check_mode=True
|
|
|
|
)
|
|
|
|
|
|
|
|
params = module.params
|
2017-02-13 22:52:25 +00:00
|
|
|
path = params['path']
|
2017-06-06 02:35:18 +00:00
|
|
|
encoding = params['encoding']
|
2016-10-24 14:42:01 +00:00
|
|
|
res_args = dict()
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2017-06-06 02:35:18 +00:00
|
|
|
params['after'] = to_text(params['after'], errors='surrogate_or_strict', nonstring='passthru')
|
|
|
|
params['before'] = to_text(params['before'], errors='surrogate_or_strict', nonstring='passthru')
|
|
|
|
params['regexp'] = to_text(params['regexp'], errors='surrogate_or_strict', nonstring='passthru')
|
|
|
|
params['replace'] = to_text(params['replace'], errors='surrogate_or_strict', nonstring='passthru')
|
|
|
|
|
2017-01-03 12:47:00 +00:00
|
|
|
if os.path.isdir(path):
|
|
|
|
module.fail_json(rc=256, msg='Path %s is a directory !' % path)
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2017-01-03 12:47:00 +00:00
|
|
|
if not os.path.exists(path):
|
|
|
|
module.fail_json(rc=257, msg='Path %s does not exist !' % path)
|
2014-09-26 01:01:01 +00:00
|
|
|
else:
|
2017-01-03 12:47:00 +00:00
|
|
|
f = open(path, 'rb')
|
2017-06-06 02:35:18 +00:00
|
|
|
contents = to_text(f.read(), errors='surrogate_or_strict', encoding=encoding)
|
2014-09-26 01:01:01 +00:00
|
|
|
f.close()
|
|
|
|
|
2017-06-06 02:35:18 +00:00
|
|
|
pattern = u''
|
|
|
|
if params['after'] and params['before']:
|
|
|
|
pattern = u'%s(.*?)%s' % (params['before'], params['after'])
|
|
|
|
elif params['after']:
|
|
|
|
pattern = u'%s(.*)' % params['after']
|
2017-03-12 00:49:39 +00:00
|
|
|
elif params['before']:
|
2017-06-06 02:35:18 +00:00
|
|
|
pattern = u'(.*)%s' % params['before']
|
2017-03-12 00:49:39 +00:00
|
|
|
|
|
|
|
if pattern:
|
|
|
|
section_re = re.compile(pattern, re.DOTALL)
|
|
|
|
match = re.search(section_re, contents)
|
|
|
|
if match:
|
|
|
|
section = match.group(0)
|
|
|
|
|
|
|
|
mre = re.compile(params['regexp'], re.MULTILINE)
|
|
|
|
result = re.subn(mre, params['replace'], section, 0)
|
|
|
|
if result[1] > 0 and section != result[0]:
|
|
|
|
result = (contents.replace(section, result[0]), result[1])
|
|
|
|
|
|
|
|
else:
|
|
|
|
mre = re.compile(params['regexp'], re.MULTILINE)
|
|
|
|
result = re.subn(mre, params['replace'], contents, 0)
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
if result[1] > 0 and contents != result[0]:
|
|
|
|
msg = '%s replacements made' % result[1]
|
|
|
|
changed = True
|
2016-08-30 09:56:35 +00:00
|
|
|
if module._diff:
|
2016-10-24 14:42:01 +00:00
|
|
|
res_args['diff'] = {
|
2017-01-03 12:47:00 +00:00
|
|
|
'before_header': path,
|
2016-10-24 14:42:01 +00:00
|
|
|
'before': contents,
|
2017-01-03 12:47:00 +00:00
|
|
|
'after_header': path,
|
2016-10-24 14:42:01 +00:00
|
|
|
'after': result[0],
|
|
|
|
}
|
2014-09-26 01:01:01 +00:00
|
|
|
else:
|
|
|
|
msg = ''
|
|
|
|
changed = False
|
|
|
|
|
|
|
|
if changed and not module.check_mode:
|
2017-01-03 12:47:00 +00:00
|
|
|
if params['backup'] and os.path.exists(path):
|
|
|
|
res_args['backup_file'] = module.backup_local(path)
|
|
|
|
if params['follow'] and os.path.islink(path):
|
|
|
|
path = os.path.realpath(path)
|
2017-06-06 02:35:18 +00:00
|
|
|
write_changes(module, to_bytes(result[0], encoding=encoding), path)
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2016-10-24 14:42:01 +00:00
|
|
|
res_args['msg'], res_args['changed'] = check_file_attrs(module, changed, msg)
|
|
|
|
module.exit_json(**res_args)
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2017-03-12 00:49:39 +00:00
|
|
|
|
2015-05-18 18:59:20 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|