2012-06-12 06:09:54 +00:00
|
|
|
#!/usr/bin/python
|
2012-08-03 01:29:10 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-06-12 06:09:54 +00: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
|
2012-07-09 07:52:00 +00:00
|
|
|
|
2012-09-28 19:55:49 +00: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
|
2012-11-21 17:49:30 +00:00
|
|
|
from multiple sources. M(assemble) will take a directory of files that have
|
2012-09-28 19:55:49 +00:00
|
|
|
already been transferred to the system, and concatenate them together to
|
|
|
|
produce a destination file. Files are assembled in string sorting order.
|
|
|
|
Puppet calls this idea I(fragments).
|
|
|
|
version_added: "0.5"
|
|
|
|
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 02:32:17 +00:00
|
|
|
- Create a backup file (if C(yes)), including the timestamp information so
|
2012-09-28 19:55:49 +00:00
|
|
|
you can get the original file back if you somehow clobbered it
|
|
|
|
incorrectly.
|
|
|
|
required: false
|
|
|
|
choices: [ "yes", "no" ]
|
|
|
|
default: "no"
|
|
|
|
others:
|
|
|
|
description:
|
|
|
|
- all arguments accepted by the M(file) module also work here
|
|
|
|
required: false
|
|
|
|
author: Stephen Fromm
|
|
|
|
'''
|
|
|
|
|
2013-06-14 09:53:43 +00:00
|
|
|
EXAMPLES = '''
|
|
|
|
# Example from Ansible Playbooks
|
|
|
|
- assemble: src=/etc/someapp/fragments dest=/etc/someapp/someapp.conf
|
|
|
|
'''
|
|
|
|
|
2012-06-12 06:09:54 +00:00
|
|
|
# ===========================================
|
2013-05-23 21:27:07 +00:00
|
|
|
# Support method
|
2012-06-12 06:09:54 +00:00
|
|
|
|
2013-05-23 21:27:07 +00:00
|
|
|
def assemble_from_fragments(src_path):
|
2012-06-12 06:09:54 +00:00
|
|
|
''' assemble a file from a directory of fragments '''
|
2013-05-23 21:27:07 +00:00
|
|
|
tmpfd, temp_path = tempfile.mkstemp()
|
|
|
|
tmp = os.fdopen(tmpfd,'w')
|
|
|
|
for f in sorted(os.listdir(src_path)):
|
|
|
|
fragment = "%s/%s" % (src_path, f)
|
2012-06-12 06:09:54 +00:00
|
|
|
if os.path.isfile(fragment):
|
2013-05-23 21:27:07 +00:00
|
|
|
tmp.write(file(fragment).read())
|
|
|
|
tmp.close()
|
|
|
|
return temp_path
|
2012-06-12 06:09:54 +00:00
|
|
|
|
2012-07-24 16:35:06 +00:00
|
|
|
# ==============================================================
|
|
|
|
# main
|
|
|
|
|
|
|
|
def main():
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2012-07-24 16:35:06 +00:00
|
|
|
module = AnsibleModule(
|
2012-09-27 16:30:08 +00:00
|
|
|
# not checking because of daisy chain to file module
|
2012-07-24 16:35:06 +00:00
|
|
|
argument_spec = dict(
|
|
|
|
src = dict(required=True),
|
|
|
|
dest = dict(required=True),
|
2013-02-23 21:56:45 +00:00
|
|
|
backup=dict(default=False, type='bool'),
|
2013-01-28 05:33:18 +00:00
|
|
|
),
|
|
|
|
add_file_common_args=True
|
2012-07-24 16:35:06 +00:00
|
|
|
)
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2013-05-29 14:07:11 +00:00
|
|
|
changed = False
|
2012-07-24 16:35:06 +00:00
|
|
|
pathmd5 = None
|
|
|
|
destmd5 = None
|
2013-05-29 14:07:11 +00:00
|
|
|
src = os.path.expanduser(module.params['src'])
|
|
|
|
dest = os.path.expanduser(module.params['dest'])
|
|
|
|
backup = module.params['backup']
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2012-07-24 16:35:06 +00:00
|
|
|
if not os.path.exists(src):
|
2012-07-25 16:24:10 +00:00
|
|
|
module.fail_json(msg="Source (%s) does not exist" % src)
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2012-07-24 16:35:06 +00:00
|
|
|
if not os.path.isdir(src):
|
2012-07-25 16:24:10 +00:00
|
|
|
module.fail_json(msg="Source (%s) is not a directory" % src)
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2013-05-23 21:27:07 +00:00
|
|
|
path = assemble_from_fragments(src)
|
2012-07-24 16:35:06 +00:00
|
|
|
pathmd5 = module.md5(path)
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2012-07-24 16:35:06 +00:00
|
|
|
if os.path.exists(dest):
|
|
|
|
destmd5 = module.md5(dest)
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2012-07-24 16:35:06 +00:00
|
|
|
if pathmd5 != destmd5:
|
2012-09-04 14:00:55 +00:00
|
|
|
if backup and destmd5 is not None:
|
2012-09-04 23:49:49 +00:00
|
|
|
module.backup_local(dest)
|
2012-07-24 16:35:06 +00:00
|
|
|
shutil.copy(path, dest)
|
|
|
|
changed = True
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2013-01-28 05:33:18 +00:00
|
|
|
file_args = module.load_file_common_arguments(module.params)
|
|
|
|
changed = module.set_file_attributes_if_different(file_args, changed)
|
2012-07-24 16:35:06 +00:00
|
|
|
# Mission complete
|
2013-05-29 14:07:11 +00:00
|
|
|
module.exit_json(src=src, dest=dest, md5sum=destmd5, changed=changed, msg="OK")
|
2012-07-24 16:35:06 +00:00
|
|
|
|
|
|
|
# this is magic, see lib/ansible/module_common.py
|
|
|
|
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
|
|
|
|
|
|
|
main()
|