aboutsummaryrefslogtreecommitdiff
path: root/ansible/action_plugins
diff options
context:
space:
mode:
authorJenkins <jenkins@review.openstack.org>2015-12-23 13:19:08 +0000
committerGerrit Code Review <review@openstack.org>2015-12-23 13:19:08 +0000
commit027e70f9a270b54f2e41208956262ee89875bd4a (patch)
tree60d97c1f3e9072c16358986288f8c9c5d609e775 /ansible/action_plugins
parentbd1fdf3b4ca7f15ccb4fb4ada57ee35a107ad3be (diff)
parented82afa8e928ac7c9f5ad2162be2c27eaab8e2e6 (diff)
downloadkolla-027e70f9a270b54f2e41208956262ee89875bd4a.tar.gz
kolla-027e70f9a270b54f2e41208956262ee89875bd4a.zip
Merge "Simplify config creation"
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)