2012-06-12 08:09:54 +02:00
|
|
|
#!/usr/bin/python
|
2012-08-03 03:29:10 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-06-12 08:09:54 +02:00
|
|
|
|
|
|
|
# (c) 2012, Stephen Fromm <sfromm@gmail.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 os
|
|
|
|
import os.path
|
|
|
|
import shutil
|
|
|
|
import tempfile
|
2013-11-14 16:30:12 +01:00
|
|
|
import re
|
2012-07-09 09:52:00 +02:00
|
|
|
|
2012-09-28 21:55:49 +02:00
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: assemble
|
|
|
|
short_description: Assembles a configuration file from fragments
|
|
|
|
description:
|
|
|
|
- Assembles a configuration file from fragments. Often a particular
|
|
|
|
program will take a single configuration file and does not support a
|
|
|
|
C(conf.d) style structure where it is easy to build up the configuration
|
2013-10-31 02:39:43 +01:00
|
|
|
from multiple sources. M(assemble) will take a directory of files that can be
|
|
|
|
local or have already been transferred to the system, and concatenate them
|
|
|
|
together to produce a destination file. Files are assembled in string sorting order.
|
2012-09-28 21:55:49 +02:00
|
|
|
Puppet calls this idea I(fragments).
|
2013-11-28 03:23:03 +01:00
|
|
|
version_added: "0.5"
|
2012-09-28 21:55:49 +02:00
|
|
|
options:
|
|
|
|
src:
|
|
|
|
description:
|
|
|
|
- An already existing directory full of source files.
|
|
|
|
required: true
|
|
|
|
default: null
|
|
|
|
aliases: []
|
|
|
|
dest:
|
|
|
|
description:
|
|
|
|
- A file to create using the concatenation of all of the source files.
|
|
|
|
required: true
|
|
|
|
default: null
|
|
|
|
backup:
|
|
|
|
description:
|
2012-10-03 04:32:17 +02:00
|
|
|
- Create a backup file (if C(yes)), including the timestamp information so
|
2012-09-28 21:55:49 +02:00
|
|
|
you can get the original file back if you somehow clobbered it
|
|
|
|
incorrectly.
|
|
|
|
required: false
|
|
|
|
choices: [ "yes", "no" ]
|
|
|
|
default: "no"
|
2013-09-20 22:50:09 +02:00
|
|
|
delimiter:
|
|
|
|
description:
|
2014-03-08 11:49:55 +01:00
|
|
|
- A delimiter to separate the file contents.
|
2013-09-23 22:32:36 +02:00
|
|
|
version_added: "1.4"
|
2013-09-20 22:50:09 +02:00
|
|
|
required: false
|
|
|
|
default: null
|
2013-10-31 02:39:43 +01:00
|
|
|
remote_src:
|
|
|
|
description:
|
|
|
|
- If False, it will search for src at originating/master machine, if True it will
|
2013-11-12 19:21:49 +01:00
|
|
|
go to the remote/target machine for the src. Default is True.
|
2013-10-31 02:39:43 +01:00
|
|
|
choices: [ "True", "False" ]
|
|
|
|
required: false
|
|
|
|
default: "True"
|
|
|
|
version_added: "1.4"
|
2013-11-14 16:30:12 +01:00
|
|
|
regexp:
|
|
|
|
description:
|
|
|
|
- Assemble files only if C(regex) matches the filename. If not set,
|
2014-06-10 19:38:57 +02:00
|
|
|
all files are assembled. All "\\" (backslash) must be escaped as
|
2013-11-14 16:30:12 +01:00
|
|
|
"\\\\" to comply yaml syntax. Uses Python regular expressions; see
|
|
|
|
U(http://docs.python.org/2/library/re.html).
|
|
|
|
required: false
|
|
|
|
default: null
|
2015-07-09 06:27:44 +02:00
|
|
|
ignore_hidden:
|
|
|
|
description:
|
|
|
|
- A boolean that controls if files that start with a '.' will be included or not.
|
|
|
|
required: false
|
|
|
|
default: false
|
|
|
|
version_added: "2.0"
|
2015-07-15 16:20:01 +02:00
|
|
|
validate:
|
|
|
|
description:
|
|
|
|
- The validation command to run before copying into place. The path to the file to
|
|
|
|
validate is passed in via '%s' which must be present as in the sshd example below.
|
|
|
|
The command is passed securely so shell features like expansion and pipes won't work.
|
|
|
|
required: false
|
|
|
|
default: ""
|
2015-06-15 21:53:30 +02:00
|
|
|
author: "Stephen Fromm (@sfromm)"
|
2014-08-26 16:09:08 +02:00
|
|
|
extends_documentation_fragment: files
|
2012-09-28 21:55:49 +02:00
|
|
|
'''
|
|
|
|
|
2013-06-14 11:53:43 +02:00
|
|
|
EXAMPLES = '''
|
|
|
|
# Example from Ansible Playbooks
|
|
|
|
- assemble: src=/etc/someapp/fragments dest=/etc/someapp/someapp.conf
|
2013-09-23 22:32:36 +02:00
|
|
|
|
|
|
|
# When a delimiter is specified, it will be inserted in between each fragment
|
|
|
|
- assemble: src=/etc/someapp/fragments dest=/etc/someapp/someapp.conf delimiter='### START FRAGMENT ###'
|
2015-07-15 16:20:01 +02:00
|
|
|
|
|
|
|
# Copy a new "sshd_config" file into place, after passing validation with sshd
|
|
|
|
- assemble: src=/etc/ssh/conf.d/ dest=/etc/ssh/sshd_config validate='sshd -t -f %s'
|
2013-06-14 11:53:43 +02:00
|
|
|
'''
|
|
|
|
|
2012-06-12 08:09:54 +02:00
|
|
|
# ===========================================
|
2013-05-23 23:27:07 +02:00
|
|
|
# Support method
|
2012-06-12 08:09:54 +02:00
|
|
|
|
2015-07-09 06:27:44 +02:00
|
|
|
def assemble_from_fragments(src_path, delimiter=None, compiled_regexp=None, ignore_hidden=False):
|
2012-06-12 08:09:54 +02:00
|
|
|
''' assemble a file from a directory of fragments '''
|
2013-05-23 23:27:07 +02:00
|
|
|
tmpfd, temp_path = tempfile.mkstemp()
|
|
|
|
tmp = os.fdopen(tmpfd,'w')
|
2013-09-20 22:50:09 +02:00
|
|
|
delimit_me = False
|
2014-03-17 23:35:03 +01:00
|
|
|
add_newline = False
|
2014-03-09 20:35:45 +01:00
|
|
|
|
2013-05-23 23:27:07 +02:00
|
|
|
for f in sorted(os.listdir(src_path)):
|
2013-11-14 16:30:12 +01:00
|
|
|
if compiled_regexp and not compiled_regexp.search(f):
|
|
|
|
continue
|
2013-05-23 23:27:07 +02:00
|
|
|
fragment = "%s/%s" % (src_path, f)
|
2015-07-09 06:27:44 +02:00
|
|
|
if not os.path.isfile(fragment) or (ignore_hidden and os.path.basename(fragment).startswith('.')):
|
2014-03-17 23:35:03 +01:00
|
|
|
continue
|
|
|
|
fragment_content = file(fragment).read()
|
2014-03-09 20:35:45 +01:00
|
|
|
|
2014-03-17 23:35:03 +01:00
|
|
|
# always put a newline between fragments if the previous fragment didn't end with a newline.
|
|
|
|
if add_newline:
|
2014-03-09 20:35:45 +01:00
|
|
|
tmp.write('\n')
|
|
|
|
|
2014-03-17 23:35:03 +01:00
|
|
|
# delimiters should only appear between fragments
|
|
|
|
if delimit_me:
|
2014-03-09 20:35:45 +01:00
|
|
|
if delimiter:
|
|
|
|
# un-escape anything like newlines
|
|
|
|
delimiter = delimiter.decode('unicode-escape')
|
|
|
|
tmp.write(delimiter)
|
|
|
|
# always make sure there's a newline after the
|
|
|
|
# delimiter, so lines don't run together
|
|
|
|
if delimiter[-1] != '\n':
|
|
|
|
tmp.write('\n')
|
|
|
|
|
2014-03-17 23:35:03 +01:00
|
|
|
tmp.write(fragment_content)
|
2013-09-20 22:50:09 +02:00
|
|
|
delimit_me = True
|
2014-03-17 23:35:03 +01:00
|
|
|
if fragment_content.endswith('\n'):
|
|
|
|
add_newline = False
|
|
|
|
else:
|
|
|
|
add_newline = True
|
2014-03-09 20:35:45 +01:00
|
|
|
|
2013-05-23 23:27:07 +02:00
|
|
|
tmp.close()
|
|
|
|
return temp_path
|
2012-06-12 08:09:54 +02:00
|
|
|
|
2012-07-24 18:35:06 +02:00
|
|
|
# ==============================================================
|
|
|
|
# main
|
|
|
|
|
|
|
|
def main():
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2012-07-24 18:35:06 +02:00
|
|
|
module = AnsibleModule(
|
2012-09-27 18:30:08 +02:00
|
|
|
# not checking because of daisy chain to file module
|
2012-07-24 18:35:06 +02:00
|
|
|
argument_spec = dict(
|
|
|
|
src = dict(required=True),
|
2013-09-20 22:50:09 +02:00
|
|
|
delimiter = dict(required=False),
|
2012-07-24 18:35:06 +02:00
|
|
|
dest = dict(required=True),
|
2013-02-23 22:56:45 +01:00
|
|
|
backup=dict(default=False, type='bool'),
|
2013-10-31 02:39:43 +01:00
|
|
|
remote_src=dict(default=False, type='bool'),
|
2013-11-14 16:30:12 +01:00
|
|
|
regexp = dict(required=False),
|
2015-07-09 06:27:44 +02:00
|
|
|
ignore_hidden = dict(default=False, type='bool'),
|
2015-07-15 16:20:01 +02:00
|
|
|
validate = dict(required=False, type='str'),
|
2013-01-28 06:33:18 +01:00
|
|
|
),
|
|
|
|
add_file_common_args=True
|
2012-07-24 18:35:06 +02:00
|
|
|
)
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2013-09-23 22:29:13 +02:00
|
|
|
changed = False
|
2014-11-07 06:25:55 +01:00
|
|
|
path_md5 = None # Deprecated
|
|
|
|
path_hash = None
|
|
|
|
dest_hash = None
|
2013-09-23 22:29:13 +02:00
|
|
|
src = os.path.expanduser(module.params['src'])
|
|
|
|
dest = os.path.expanduser(module.params['dest'])
|
|
|
|
backup = module.params['backup']
|
|
|
|
delimiter = module.params['delimiter']
|
2013-11-14 16:30:12 +01:00
|
|
|
regexp = module.params['regexp']
|
|
|
|
compiled_regexp = None
|
2015-07-09 06:27:44 +02:00
|
|
|
ignore_hidden = module.params['ignore_hidden']
|
2015-07-15 16:20:01 +02:00
|
|
|
validate = module.params.get('validate', None)
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2012-07-24 18:35:06 +02:00
|
|
|
if not os.path.exists(src):
|
2012-07-25 18:24:10 +02:00
|
|
|
module.fail_json(msg="Source (%s) does not exist" % src)
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2012-07-24 18:35:06 +02:00
|
|
|
if not os.path.isdir(src):
|
2012-07-25 18:24:10 +02:00
|
|
|
module.fail_json(msg="Source (%s) is not a directory" % src)
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2013-11-14 16:30:12 +01:00
|
|
|
if regexp != None:
|
|
|
|
try:
|
|
|
|
compiled_regexp = re.compile(regexp)
|
|
|
|
except re.error, e:
|
|
|
|
module.fail_json(msg="Invalid Regexp (%s) in \"%s\"" % (e, regexp))
|
|
|
|
|
2015-07-09 06:27:44 +02:00
|
|
|
path = assemble_from_fragments(src, delimiter, compiled_regexp, ignore_hidden)
|
2014-11-07 06:25:55 +01:00
|
|
|
path_hash = module.sha1(path)
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2012-07-24 18:35:06 +02:00
|
|
|
if os.path.exists(dest):
|
2014-11-07 06:25:55 +01:00
|
|
|
dest_hash = module.sha1(dest)
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2014-11-07 06:25:55 +01:00
|
|
|
if path_hash != dest_hash:
|
|
|
|
if backup and dest_hash is not None:
|
2012-09-05 01:49:49 +02:00
|
|
|
module.backup_local(dest)
|
2015-07-15 16:20:01 +02:00
|
|
|
if validate:
|
|
|
|
if "%s" not in validate:
|
|
|
|
module.fail_json(msg="validate must contain %%s: %s" % validate)
|
|
|
|
(rc, out, err) = module.run_command(validate % path)
|
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(msg="failed to validate: rc:%s error:%s" % (rc, err))
|
|
|
|
|
2012-07-24 18:35:06 +02:00
|
|
|
shutil.copy(path, dest)
|
|
|
|
changed = True
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2014-11-07 06:25:55 +01:00
|
|
|
# Backwards compat. This won't return data if FIPS mode is active
|
|
|
|
try:
|
|
|
|
pathmd5 = module.md5(path)
|
|
|
|
except ValueError:
|
|
|
|
pathmd5 = None
|
|
|
|
|
2013-10-31 01:24:03 +01:00
|
|
|
os.remove(path)
|
|
|
|
|
2013-01-28 06:33:18 +01:00
|
|
|
file_args = module.load_file_common_arguments(module.params)
|
2014-03-19 03:39:45 +01:00
|
|
|
changed = module.set_fs_attributes_if_different(file_args, changed)
|
2012-07-24 18:35:06 +02:00
|
|
|
# Mission complete
|
2014-11-07 06:25:55 +01:00
|
|
|
module.exit_json(src=src, dest=dest, md5sum=pathmd5, checksum=path_hash, changed=changed, msg="OK")
|
2012-07-24 18:35:06 +02:00
|
|
|
|
2013-12-02 21:13:49 +01:00
|
|
|
# import module snippets
|
2013-12-02 21:11:23 +01:00
|
|
|
from ansible.module_utils.basic import *
|
2012-07-24 18:35:06 +02:00
|
|
|
|
|
|
|
main()
|
2013-11-14 16:30:12 +01:00
|
|
|
|