Adding extra-vars to playooks 55/7355/4
authorEduardo Sousa <esousa@whitestack.com>
Fri, 22 Mar 2019 02:40:47 +0000 (02:40 +0000)
committerEduardo Sousa <esousa@whitestack.com>
Fri, 12 Apr 2019 03:19:15 +0000 (04:19 +0100)
Change-Id: I8cf0ddcdc794e102f2af4f9bf8fe5458009f47f1
Signed-off-by: Eduardo Sousa <esousa@whitestack.com>
descriptor-packages/tools/charm-generator/generator/actions/templates/actions.yaml.j2
descriptor-packages/tools/charm-generator/generator/ansible-charm/templates/ansible_charm.py.j2
descriptor-packages/tools/charm-generator/generator/ansible-charm/templates/ansible_lib.py.j2
descriptor-packages/tools/charm-generator/setup.py

index e9f2961..994bcde 100644 (file)
@@ -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 %}
 
index d9e2f55..016659f 100644 (file)
@@ -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)
index 109bae0..9da7402 100644 (file)
@@ -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
index 5015efa..51df44c 100644 (file)
@@ -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',