From ed82afa8e928ac7c9f5ad2162be2c27eaab8e2e6 Mon Sep 17 00:00:00 2001 From: SamYaple Date: Fri, 18 Dec 2015 19:40:48 +0000 Subject: Simplify config creation Convert config creation from a playbook to an action_plugin. This reduces the complexity and confusion while retaining the same augment structure and flexibility. This allows us to remove the 0-byte files as requirements. They will still be used if they are present (this means we require additional documentation around them). DocImpact Closes-Bug: #1528430 Change-Id: I2c789f6be9f195c7771ca093a6d59499564b4740 --- ansible/action_plugins/merge_configs.py | 90 +++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 ansible/action_plugins/merge_configs.py (limited to 'ansible/action_plugins') diff --git a/ansible/action_plugins/merge_configs.py b/ansible/action_plugins/merge_configs.py new file mode 100644 index 000000000..622f6f91d --- /dev/null +++ b/ansible/action_plugins/merge_configs.py @@ -0,0 +1,90 @@ +#!/usr/bin/python + +# Copyright 2015 Sam Yaple +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from ConfigParser import ConfigParser +from cStringIO import StringIO +import os + +from ansible import utils +from ansible.utils import template +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): + args = {} + if complex_args: + args.update(complex_args) + args.update(utils.parse_kv(module_args)) + + dest = args.get('dest') + extra_vars = args.get('vars') + sources = args.get('sources') + + if extra_vars: + # Extend 'inject' args used in templating + if isinstance(extra_vars, dict): + inject.update(extra_vars) + else: + inject.update(utils.parse_kv(extra_vars)) + + + # Catch the case where sources is a str() + if not isinstance(sources, list): + sources = [sources] + + config = ConfigParser() + + for source in sources: + # template the source string + source = template.template(self.runner.basedir, source, inject) + + # Only use config if present + if os.access(source, os.R_OK): + # template the source data locally & get ready to transfer + try: + resultant = template.template_from_file(self.runner.basedir, source, inject) + except Exception as e: + return ReturnData(conn=conn, comm_ok=False, result={'failed': True, 'msg': str(e)}) + + # Read in new results and merge this with the existing config + fakefile = StringIO(resultant) + config.readfp(fakefile) + fakefile.close() + + # Dump configparser to string via an emulated file + fakefile = StringIO() + config.write(fakefile) + # Template the file to fill out any variables + content = template.template(self.runner.basedir, fakefile.getvalue(), inject) + fakefile.close() + + # Ship this content over to a new file for use with the copy module + xfered = self.runner._transfer_str(conn, tmp, 'source', content) + + copy_module_args = dict( + src=xfered, + dest=dest, + original_basename=os.path.basename(source), + follow=True, + ) + return self.runner._execute_module(conn, tmp, 'copy', '', inject=inject, complex_args=copy_module_args) -- cgit v1.2.3