Clean: Jinja templating options for lookups

This commit is contained in:
Robin Pierre Cordier 2022-02-15 13:47:11 -05:00
parent b731d507c7
commit 26374ecb25
2 changed files with 26 additions and 32 deletions

View File

@ -51,26 +51,6 @@ DOCUMENTATION = """
description: One or more string terms prefixed by a namespace. Format is `<namespace>/<key>`. description: One or more string terms prefixed by a namespace. Format is `<namespace>/<key>`.
required: True required: True
enable_jinja:
description:
- Enable or not Jinja rendering
default: True
type: bool
jinja2_native:
description:
- Controls whether to use Jinja2 native types.
- It is off by default even if global jinja2_native is True.
- Has no effect if global jinja2_native is False.
- This offers more flexibility than the template module which does not use Jinja2 native types at all.
- Mutually exclusive with the convert_data option.
default: False
type: bool
env:
- name: ANSIBLE_JINJA2_NATIVE
notes:
- Kheops documentation is available on http://kheops.io/
- You can add more parameters as documented in http://kheops.io/server/api
""" + DOCUMENTATION_OPTION_FRAGMENT """ + DOCUMENTATION_OPTION_FRAGMENT
EXAMPLES = """ EXAMPLES = """
@ -113,13 +93,11 @@ class LookupModule(LookupBase):
process_scope = self.get_option('process_scope') process_scope = self.get_option('process_scope')
process_results = self.get_option('process_results') process_results = self.get_option('process_results')
enable_jinja = kwargs.pop('enable_jinja', self.get_option('enable_jinja'))
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 # Start jinja template engine
if enable_jinja: if process_scope == 'jinja' or process_results == 'jinja':
if USE_JINJA2_NATIVE and not jinja2_native: if USE_JINJA2_NATIVE and not jinja2_native:
templar = self._templar.copy_with_new_env(environment_class=AnsibleEnvironment) templar = self._templar.copy_with_new_env(environment_class=AnsibleEnvironment)
else: else:
@ -152,6 +130,7 @@ class LookupModule(LookupBase):
) )
# Render data with Templar # Render data with Templar
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,

View File

@ -143,6 +143,21 @@ DOCUMENTATION_OPTION_FRAGMENT = '''
default: 'none' default: 'none'
choices: ['none', 'jinja'] choices: ['none', 'jinja']
jinja2_native:
description:
- Controls whether to use Jinja2 native types.
- It is off by default even if global jinja2_native is True.
- Has no effect if global jinja2_native is False.
- This offers more flexibility than the template module which does not use Jinja2 native types at all.
- Mutually exclusive with the convert_data option.
default: False
type: bool
env:
- name: ANSIBLE_JINJA2_NATIVE
notes:
- Kheops documentation is available on http://kheops.io/
- You can add more parameters as documented in http://kheops.io/server/api
# Uneeded # Misc # Uneeded # Misc
# Uneeded version: # Uneeded version: