aboutsummaryrefslogtreecommitdiff
path: root/ansible/action_plugins
diff options
context:
space:
mode:
authorSamYaple <sam@yaple.net>2015-12-18 19:40:48 +0000
committerSamYaple <sam@yaple.net>2015-12-22 04:28:53 +0000
commited82afa8e928ac7c9f5ad2162be2c27eaab8e2e6 (patch)
tree386b3e9ac028f5171cb6bf16d36e2051cab08e30 /ansible/action_plugins
parent03fd08d867ec7967d2f0ec8813d865ea25a93a06 (diff)
downloadkolla-ansible-ed82afa8e928ac7c9f5ad2162be2c27eaab8e2e6.tar.gz
kolla-ansible-ed82afa8e928ac7c9f5ad2162be2c27eaab8e2e6.zip
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
Diffstat (limited to 'ansible/action_plugins')
-rw-r--r--ansible/action_plugins/merge_configs.py90
1 files changed, 90 insertions, 0 deletions
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)