mirror of https://github.com/ansible/ansible.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
137 lines
4.1 KiB
Plaintext
137 lines
4.1 KiB
Plaintext
13 years ago
|
#!/usr/bin/python
|
||
12 years ago
|
# -*- coding: utf-8 -*-
|
||
13 years ago
|
|
||
|
# (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
|
||
13 years ago
|
|
||
12 years ago
|
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
|
||
12 years ago
|
from multiple sources. M(assemble) will take a directory of files that have
|
||
12 years ago
|
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:
|
||
12 years ago
|
- Create a backup file (if C(yes)), including the timestamp information so
|
||
12 years ago
|
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
|
||
|
examples:
|
||
12 years ago
|
- code: "assemble: src=/etc/someapp/fragments dest=/etc/someapp/someapp.conf"
|
||
12 years ago
|
description: "Example from Ansible Playbooks"
|
||
|
author: Stephen Fromm
|
||
|
'''
|
||
|
|
||
13 years ago
|
# ===========================================
|
||
|
# Support methods
|
||
|
|
||
|
def assemble_from_fragments(path):
|
||
|
''' assemble a file from a directory of fragments '''
|
||
|
assembled = []
|
||
|
for f in sorted(os.listdir(path)):
|
||
|
fragment = "%s/%s" % (path, f)
|
||
|
if os.path.isfile(fragment):
|
||
|
assembled.append(file(fragment).read())
|
||
|
return "".join(assembled)
|
||
|
|
||
|
def write_temp_file(data):
|
||
|
fd, path = tempfile.mkstemp()
|
||
|
os.write(fd, data)
|
||
|
os.close(fd)
|
||
|
return path
|
||
|
|
||
13 years ago
|
# ==============================================================
|
||
|
# main
|
||
|
|
||
|
def main():
|
||
12 years ago
|
|
||
13 years ago
|
module = AnsibleModule(
|
||
12 years ago
|
# not checking because of daisy chain to file module
|
||
13 years ago
|
argument_spec = dict(
|
||
|
src = dict(required=True),
|
||
|
dest = dict(required=True),
|
||
12 years ago
|
backup=dict(default=False, type='bool'),
|
||
12 years ago
|
),
|
||
|
add_file_common_args=True
|
||
13 years ago
|
)
|
||
12 years ago
|
|
||
13 years ago
|
changed=False
|
||
|
pathmd5 = None
|
||
|
destmd5 = None
|
||
|
src = os.path.expanduser(module.params['src'])
|
||
|
dest = os.path.expanduser(module.params['dest'])
|
||
12 years ago
|
backup = module.params['backup']
|
||
12 years ago
|
|
||
13 years ago
|
if not os.path.exists(src):
|
||
13 years ago
|
module.fail_json(msg="Source (%s) does not exist" % src)
|
||
12 years ago
|
|
||
13 years ago
|
if not os.path.isdir(src):
|
||
13 years ago
|
module.fail_json(msg="Source (%s) is not a directory" % src)
|
||
12 years ago
|
|
||
13 years ago
|
path = write_temp_file(assemble_from_fragments(src))
|
||
|
pathmd5 = module.md5(path)
|
||
12 years ago
|
|
||
13 years ago
|
if os.path.exists(dest):
|
||
|
destmd5 = module.md5(dest)
|
||
12 years ago
|
|
||
13 years ago
|
if pathmd5 != destmd5:
|
||
12 years ago
|
if backup and destmd5 is not None:
|
||
12 years ago
|
module.backup_local(dest)
|
||
13 years ago
|
shutil.copy(path, dest)
|
||
|
changed = True
|
||
12 years ago
|
|
||
12 years ago
|
file_args = module.load_file_common_arguments(module.params)
|
||
|
changed = module.set_file_attributes_if_different(file_args, changed)
|
||
13 years ago
|
# Mission complete
|
||
12 years ago
|
module.exit_json(src=src, dest=dest, md5sum=destmd5,
|
||
12 years ago
|
changed=changed, msg="OK")
|
||
13 years ago
|
|
||
|
# this is magic, see lib/ansible/module_common.py
|
||
|
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
||
|
|
||
|
main()
|