From 1a7cca3dfc5b66b8c274d2848bc2fb49b61e22be Mon Sep 17 00:00:00 2001 From: Robin Pierre Cordier Date: Tue, 15 Feb 2022 16:07:48 -0500 Subject: [PATCH] Patch: errors in jinja option handling --- plugins/lookup/kheops.py | 36 ++++++++++++++++++---------------- plugins/plugin_utils/common.py | 33 ++++++++++++++++++++++--------- 2 files changed, 43 insertions(+), 26 deletions(-) diff --git a/plugins/lookup/kheops.py b/plugins/lookup/kheops.py index da8ee04..3909d7a 100644 --- a/plugins/lookup/kheops.py +++ b/plugins/lookup/kheops.py @@ -96,13 +96,6 @@ class LookupModule(LookupBase): jinja2_native = kwargs.pop('jinja2_native', self.get_option('jinja2_native')) - # Start jinja template engine - if process_scope == 'jinja' or process_results == 'jinja': - if USE_JINJA2_NATIVE and not jinja2_native: - templar = self._templar.copy_with_new_env(environment_class=AnsibleEnvironment) - else: - templar = self._templar - # Prepare Kheops instance self.config_file = self.get_option('config') @@ -115,6 +108,13 @@ class LookupModule(LookupBase): ] kheops = AnsibleKheops(configs=configs, display=self._display) + + # Start jinja template engine + if USE_JINJA2_NATIVE and not jinja2_native: + templar = self._templar.copy_with_new_env(environment_class=AnsibleEnvironment) + else: + templar = self._templar + # Create scope if process_scope == 'vars': scope = kheops.get_scope_from_host_inventory(variables, scope=None) @@ -124,22 +124,24 @@ class LookupModule(LookupBase): # Transform dict to list for lookup/queries ret = [] for term in terms: - result = kheops.lookup( + result = kheops.super_lookup( keys=term, scope=scope, + _variables=variables, + _templar=templar, ) # Render data with Templar - if process_results == 'jinja': - with templar.set_temporary_context(available_variables=variables): - result = templar.template(result, - preserve_trailing_newlines=True, - convert_data=False, escape_backslashes=False) + #if process_results == 'jinja': + # with templar.set_temporary_context(available_variables=variables): + # result = templar.template(result, + # preserve_trailing_newlines=True, + # convert_data=False, escape_backslashes=False) - if USE_JINJA2_NATIVE and not jinja2_native: - # jinja2_native is true globally but off for the lookup, we need this text - # not to be processed by literal_eval anywhere in Ansible - result = NativeJinjaText(result) + # if USE_JINJA2_NATIVE and not jinja2_native: + # # jinja2_native is true globally but off for the lookup, we need this text + # # not to be processed by literal_eval anywhere in Ansible + # result = NativeJinjaText(result) # Return result subkey = list(result.keys())[0] diff --git a/plugins/plugin_utils/common.py b/plugins/plugin_utils/common.py index e9d0fd9..126f48f 100644 --- a/plugins/plugin_utils/common.py +++ b/plugins/plugin_utils/common.py @@ -237,7 +237,7 @@ class AnsibleKheops(): # Instanciate Kheops if config["mode"] == 'instance': - # Confiogure logging + # Configure logging logger = logging.getLogger('kheops') logger.setLevel(config["instance_log_level"]) @@ -263,24 +263,26 @@ class AnsibleKheops(): def get_config(self): - items = [ - # We exclude 'config' - 'mode', - 'instance_config', 'instance_namespace', 'instance_log_level', - 'namespace', 'scope', 'keys'] - + """ + Processing order: + - Fetch the value of config or fallback on ANSIBLE_KHEOPS_CONFIG + - Load the config if any + - Overrides with other options + """ # Extract default value from doc data_doc = yaml.safe_load(DOCUMENTATION_OPTION_FRAGMENT) default_config = {key: value.get("default", None) for key, value in data_doc.items()} + #print ("Show configs") + #pprint (self.configs) merged_configs = {} for config in self.configs: conf_data = None if isinstance(config, str): - #print ("Read file", config) + self.display.vv("Read Kheops file config", config) if os.path.isfile(config): data = open(config, "r") conf_data = yaml.safe_load(data) @@ -288,7 +290,7 @@ class AnsibleKheops(): raise AnsibleError("Unable to find configuration file %s" % config_file) elif isinstance(config, dict): - #print ("Read Config", config) + self.display.vv ("Read Kheops direct config", config) conf_data = config else: assert False, f"Bad config for: {config}" @@ -299,6 +301,11 @@ class AnsibleKheops(): # Get environment config + items = [ + # We exclude 'config' + 'mode', + 'instance_config', 'instance_namespace', 'instance_log_level', + 'namespace', 'scope', 'keys'] env_config = {} for item in items: envvar = "ANSIBLE_KHEOPS_" + item.upper() @@ -468,6 +475,14 @@ class AnsibleKheops(): ret = self.lookup(keys, namespace=namespace, scope=scope) + if _process_results == 'jinja': + with _templar.set_temporary_context(available_variables=_variables): + ret = _templar.template(ret, + preserve_trailing_newlines=True, + convert_data=False, escape_backslashes=False) + if USE_JINJA2_NATIVE and not jinja2_native: + ret = NativeJinjaText(ret) + return ret