From 1e68d980a0587bb1afea3685d0a46fce86135cb9 Mon Sep 17 00:00:00 2001 From: Guilhem Moulin Date: Sun, 6 Jul 2014 19:55:58 +0200 Subject: Enable zero-copy updates to the LDAP directory. --- lib/action_plugins/openldap.py | 83 ++++++++++++++++++++++++++++++++++++++++++ lib/modules/openldap | 5 ++- 2 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 lib/action_plugins/openldap.py (limited to 'lib') diff --git a/lib/action_plugins/openldap.py b/lib/action_plugins/openldap.py new file mode 100644 index 0000000..ee8a991 --- /dev/null +++ b/lib/action_plugins/openldap.py @@ -0,0 +1,83 @@ +# Manage OpenLDAP databases +# Copyright (c) 2014 Guilhem Moulin +# +# This program 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. +# +# This program 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 this program. If not, see . + +import os +import pipes +import tempfile + +from ansible.utils import template +from ansible import utils +from ansible.runner.return_data import ReturnData + +class ActionModule(object): + TRANSFERS_FILES = True + + def __init__(self, runner): + self.runner = runner + + def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs): + ''' handler for file transfer operations ''' + + # load up options + options = {} + if complex_args: + options.update(complex_args) + options.update(utils.parse_kv(module_args)) + + target = options.get('target', None) + local = options.get('local', 'no') + + if local not in [ 'no', 'file', 'template' ]: + result = dict(failed=True, msg="local must be in ['no','file','template']") + return ReturnData(conn=conn, comm_ok=False, result=result) + + if local != 'no' and target is None: + result = dict(failed=True, msg="target is required in local mode") + return ReturnData(conn=conn, comm_ok=False, result=result) + + if local == 'no': + # run the module remotely + return self.runner._execute_module(conn, tmp, 'openldap', module_args, inject=inject, complex_args=complex_args) + elif '_original_file' in inject: + target = utils.path_dwim_relative(inject['_original_file'], local+'s', target, self.runner.basedir) + else: + # the source is local, so expand it here + target = os.path.expanduser(target) + + options['local'] = 'no' + options['target'] = os.path.join(tmp, os.path.basename(target)) + if local == 'template': + # template the source data locally and transfer it + try: + s = template.template_from_file(self.runner.basedir, target, inject, vault_password=self.runner.vault_pass) + tmpfile = tempfile.NamedTemporaryFile(delete=False) + tmpfile.write(s) + tmpfile.close() + target = tmpfile.name + except Exception, e: + result = dict(failed=True, msg=str(e)) + return ReturnData(conn=conn, comm_ok=False, result=result) + conn.put_file(tmpfile.name, options['target']) + os.unlink(tmpfile.name) + + elif local == 'file': + conn.put_file(target, options['target']) + + # run the script remotely with the new (temporary) filename + module_args = "" + for o in options: + module_args = "%s %s=%s" % (module_args, o, pipes.quote(options[o])) + return self.runner._execute_module(conn, tmp, 'openldap', module_args, inject=inject) diff --git a/lib/modules/openldap b/lib/modules/openldap index 7293b23..3f6ea39 100644 --- a/lib/modules/openldap +++ b/lib/modules/openldap @@ -351,6 +351,7 @@ def main(): suffix = dict( default=None ), format = dict( default="ldif", choices=["ldif","slapd.conf"] ), name = dict( default=None ), + local = dict( default="no", choices=["no","file","template"] ), ), supports_check_mode=True ) @@ -428,7 +429,7 @@ def main(): module.exit_json(changed=changed) +# import module snippets +from ansible.module_utils.basic import * -# this is magic, see lib/ansible/module_common.py -#<> main() -- cgit v1.2.3