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'))
# 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]

View File

@ -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