From eb0037f93c252427ea3df0e5f6e4b21cb2c72c28 Mon Sep 17 00:00:00 2001 From: Eduardo Sousa Date: Fri, 22 Mar 2019 02:40:47 +0000 Subject: [PATCH] Adding extra-vars to playooks Change-Id: I8cf0ddcdc794e102f2af4f9bf8fe5458009f47f1 Signed-off-by: Eduardo Sousa --- .../actions/templates/actions.yaml.j2 | 6 +++--- .../templates/ansible_charm.py.j2 | 9 ++++++++- .../ansible-charm/templates/ansible_lib.py.j2 | 18 +++++++++++++++--- .../tools/charm-generator/setup.py | 8 ++++++++ 4 files changed, 34 insertions(+), 7 deletions(-) diff --git a/descriptor-packages/tools/charm-generator/generator/actions/templates/actions.yaml.j2 b/descriptor-packages/tools/charm-generator/generator/actions/templates/actions.yaml.j2 index e9f29614..994bcde2 100644 --- a/descriptor-packages/tools/charm-generator/generator/actions/templates/actions.yaml.j2 +++ b/descriptor-packages/tools/charm-generator/generator/actions/templates/actions.yaml.j2 @@ -37,8 +37,8 @@ {%- for act in actions %} {{ act.action_name }}: - description: "Insert description for this playbook." - {#- TODO: Insert parameters -#} - {#- TODO: Insert required -#} + description: "Insert description for this playbook." + {#- TODO: Insert parameters -#} + {#- TODO: Insert required -#} {% endfor %} diff --git a/descriptor-packages/tools/charm-generator/generator/ansible-charm/templates/ansible_charm.py.j2 b/descriptor-packages/tools/charm-generator/generator/ansible-charm/templates/ansible_charm.py.j2 index d9e2f551..016659ff 100644 --- a/descriptor-packages/tools/charm-generator/generator/ansible-charm/templates/ansible_charm.py.j2 +++ b/descriptor-packages/tools/charm-generator/generator/ansible-charm/templates/ansible_charm.py.j2 @@ -70,7 +70,14 @@ def config_changed(): @when('actions.{{ pb.action_name }}') def {{ pb.function_name }}(): try: - result = charms.libansible.execute_playbook('{{ pb.file }}') + dict_vars = {} + + # edit the following snippet to add your parameters + # Note: don't forget to edit actions.yaml to add the parameters + # param_variable = action_get('param-name') + # dict_vars['param-name'] = param_variable} + + result = charms.libansible.execute_playbook('{{ pb.file }}', dict_vars) except: exc_type, exc_value, exc_traceback = sys.exc_info() err = traceback.format_exception(exc_type, exc_value, exc_traceback) diff --git a/descriptor-packages/tools/charm-generator/generator/ansible-charm/templates/ansible_lib.py.j2 b/descriptor-packages/tools/charm-generator/generator/ansible-charm/templates/ansible_lib.py.j2 index 109bae05..9da74029 100644 --- a/descriptor-packages/tools/charm-generator/generator/ansible-charm/templates/ansible_lib.py.j2 +++ b/descriptor-packages/tools/charm-generator/generator/ansible-charm/templates/ansible_lib.py.j2 @@ -72,7 +72,7 @@ def find(pattern, path): return result -def execute_playbook(playbook_file, vars=None): +def execute_playbook(playbook_file, vars_dict=None): playbook_path = find(playbook_file, '/var/lib/juju/agents/') cfg = config() @@ -87,7 +87,19 @@ def execute_playbook(playbook_file, vars=None): create_ansible_cfg() create_hosts(cfg, hosts) - call = ['ansible-playbook', playbook_path] - result = subprocess.check_output(call) + call = 'ansible-playbook %s ' % playbook_path + + if vars_dict and isinstance(vars_dict, dict) and len(vars_dict) > 0: + call += '--extra-vars ' + + string_var = '' + for v in vars_dict.items(): + string_var += '%s=%s ' % v + + string_var = string_var.strip() + call += '"%s"' % string_var + + call = call.strip() + result = subprocess.check_output(call, shell=True) return result diff --git a/descriptor-packages/tools/charm-generator/setup.py b/descriptor-packages/tools/charm-generator/setup.py index 5015efae..51df44ce 100644 --- a/descriptor-packages/tools/charm-generator/setup.py +++ b/descriptor-packages/tools/charm-generator/setup.py @@ -19,6 +19,8 @@ import re import setuptools +print(setuptools.find_packages()) + version = re.search( r'^__version__\s*=\s*["\'](.*)["\']', open('generator/generator.py').read(), @@ -43,6 +45,12 @@ setuptools.setup( install_requires=[ 'Jinja2>=2.10' ], + package_data={ + 'generator.actions': ['templates/*.j2'], + 'generator.ansible-charm': ['templates/*.j2'], + 'generator.metadata': ['templates/*.j2'], + }, + include_package_data=True, classifiers=[ 'Programming Language :: Python :: 3 :: Only', 'License :: OSI Approved :: Apache Software License', -- 2.25.1