Patch: errors in jinja option handling

This commit is contained in:
Robin Pierre Cordier 2022-02-15 16:07:48 -05:00
parent 26374ecb25
commit 1a7cca3dfc
2 changed files with 43 additions and 26 deletions

View File

@ -96,13 +96,6 @@ class LookupModule(LookupBase):
jinja2_native = kwargs.pop('jinja2_native', self.get_option('jinja2_native')) 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 # Prepare Kheops instance
self.config_file = self.get_option('config') self.config_file = self.get_option('config')
@ -115,6 +108,13 @@ class LookupModule(LookupBase):
] ]
kheops = AnsibleKheops(configs=configs, display=self._display) 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 # Create scope
if process_scope == 'vars': if process_scope == 'vars':
scope = kheops.get_scope_from_host_inventory(variables, scope=None) scope = kheops.get_scope_from_host_inventory(variables, scope=None)
@ -124,22 +124,24 @@ class LookupModule(LookupBase):
# Transform dict to list for lookup/queries # Transform dict to list for lookup/queries
ret = [] ret = []
for term in terms: for term in terms:
result = kheops.lookup( result = kheops.super_lookup(
keys=term, keys=term,
scope=scope, scope=scope,
_variables=variables,
_templar=templar,
) )
# Render data with Templar # Render data with Templar
if process_results == 'jinja': #if process_results == 'jinja':
with templar.set_temporary_context(available_variables=variables): # with templar.set_temporary_context(available_variables=variables):
result = templar.template(result, # result = templar.template(result,
preserve_trailing_newlines=True, # preserve_trailing_newlines=True,
convert_data=False, escape_backslashes=False) # convert_data=False, escape_backslashes=False)
if USE_JINJA2_NATIVE and not jinja2_native: # if USE_JINJA2_NATIVE and not jinja2_native:
# jinja2_native is true globally but off for the lookup, we need this text # # jinja2_native is true globally but off for the lookup, we need this text
# not to be processed by literal_eval anywhere in Ansible # # not to be processed by literal_eval anywhere in Ansible
result = NativeJinjaText(result) # result = NativeJinjaText(result)
# Return result # Return result
subkey = list(result.keys())[0] subkey = list(result.keys())[0]

View File

@ -237,7 +237,7 @@ class AnsibleKheops():
# Instanciate Kheops # Instanciate Kheops
if config["mode"] == 'instance': if config["mode"] == 'instance':
# Confiogure logging # Configure logging
logger = logging.getLogger('kheops') logger = logging.getLogger('kheops')
logger.setLevel(config["instance_log_level"]) logger.setLevel(config["instance_log_level"])
@ -263,24 +263,26 @@ class AnsibleKheops():
def get_config(self): def get_config(self):
items = [ """
# We exclude 'config' Processing order:
'mode', - Fetch the value of config or fallback on ANSIBLE_KHEOPS_CONFIG
'instance_config', 'instance_namespace', 'instance_log_level', - Load the config if any
'namespace', 'scope', 'keys'] - Overrides with other options
"""
# Extract default value from doc # Extract default value from doc
data_doc = yaml.safe_load(DOCUMENTATION_OPTION_FRAGMENT) data_doc = yaml.safe_load(DOCUMENTATION_OPTION_FRAGMENT)
default_config = {key: value.get("default", None) for key, value in data_doc.items()} default_config = {key: value.get("default", None) for key, value in data_doc.items()}
#print ("Show configs")
#pprint (self.configs)
merged_configs = {} merged_configs = {}
for config in self.configs: for config in self.configs:
conf_data = None conf_data = None
if isinstance(config, str): if isinstance(config, str):
#print ("Read file", config) self.display.vv("Read Kheops file config", config)
if os.path.isfile(config): if os.path.isfile(config):
data = open(config, "r") data = open(config, "r")
conf_data = yaml.safe_load(data) conf_data = yaml.safe_load(data)
@ -288,7 +290,7 @@ class AnsibleKheops():
raise AnsibleError("Unable to find configuration file %s" % config_file) raise AnsibleError("Unable to find configuration file %s" % config_file)
elif isinstance(config, dict): elif isinstance(config, dict):
#print ("Read Config", config) self.display.vv ("Read Kheops direct config", config)
conf_data = config conf_data = config
else: else:
assert False, f"Bad config for: {config}" assert False, f"Bad config for: {config}"
@ -299,6 +301,11 @@ class AnsibleKheops():
# Get environment config # Get environment config
items = [
# We exclude 'config'
'mode',
'instance_config', 'instance_namespace', 'instance_log_level',
'namespace', 'scope', 'keys']
env_config = {} env_config = {}
for item in items: for item in items:
envvar = "ANSIBLE_KHEOPS_" + item.upper() envvar = "ANSIBLE_KHEOPS_" + item.upper()
@ -468,6 +475,14 @@ class AnsibleKheops():
ret = self.lookup(keys, namespace=namespace, scope=scope) 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 return ret