mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-25 11:51:26 -07:00
template: Add option to lstrip_blocks' and fix setting
trim_blocks` inline (#37478)
* template: Add integration tests for `lstrip_blocks' Signed-off-by: Alex Tsitsimpis <alextsi@arrikto.com> * template: Fix passing `trim_blocks' inline Fix passing `trim_blocks' option to the template module as inline argument. Previously passing the `trim_blocks' option inline instead of using the YAML dictionary format resulted in it always being set to `True', even if `trim_blocks=False' was used. Signed-off-by: Alex Tsitsimpis <alextsi@arrikto.com> * template: Add option to `lstrip_blocks' Add option to set `lstrip_blocks' when using the template module to render Jinja templates. The Jinja documentation suggests that `trim_blocks' and `lstrip_blocks' is a great combination and the template module already provides an option for `trim_blocks'. Note that although `trim_blocks' in Ansible is enabled by default since version 2.4, in order to avoid breaking things keep `lstrip_blocks' disabled by default. Maybe in a future version it could be enabled by default. This seems to address issue #10725 in a more appropriate way than the suggested. Signed-off-by: Alex Tsitsimpis <alextsi@arrikto.com> * template: Add integration tests for `trim_blocks' Signed-off-by: Alex Tsitsimpis <alextsi@arrikto.com> * template: Check Jinja2 support for `lstrip_blocks' Since the `lstrip_blocks' option was added in Jinja2 version 2.7, raise an exception when `lstrip_blocks' is set but Jinja2 does not support it. Check support for `lstrip_blocks' option by checking `jinja2.defaults' for `LSTRIP_BLOCKS' and do not use `jinja2.__version__' because the latter is set to `unknown' in some cases, perhaps due to bug in `pkg_resources' in Python 2.6.6. Also update option description to state that Jinja2 version >=2.7 is required. Signed-off-by: Alex Tsitsimpis <alextsi@arrikto.com>
This commit is contained in:
parent
687f3bbef3
commit
c3ab6cb9b1
9 changed files with 136 additions and 3 deletions
|
@ -53,7 +53,20 @@ class ActionModule(ActionBase):
|
|||
variable_end_string = self._task.args.get('variable_end_string', None)
|
||||
block_start_string = self._task.args.get('block_start_string', None)
|
||||
block_end_string = self._task.args.get('block_end_string', None)
|
||||
trim_blocks = self._task.args.get('trim_blocks', None)
|
||||
trim_blocks = boolean(self._task.args.get('trim_blocks', True), strict=False)
|
||||
lstrip_blocks = boolean(self._task.args.get('lstrip_blocks', False), strict=False)
|
||||
|
||||
# Option `lstrip_blocks' was added in Jinja2 version 2.7.
|
||||
if lstrip_blocks:
|
||||
try:
|
||||
import jinja2.defaults
|
||||
except ImportError:
|
||||
raise AnsibleError('Unable to import Jinja2 defaults for determing Jinja2 features.')
|
||||
|
||||
try:
|
||||
jinja2.defaults.LSTRIP_BLOCKS
|
||||
except AttributeError:
|
||||
raise AnsibleError("Option `lstrip_blocks' is only available in Jinja2 versions >=2.7")
|
||||
|
||||
wrong_sequences = ["\\n", "\\r", "\\r\\n"]
|
||||
allowed_sequences = ["\n", "\r", "\r\n"]
|
||||
|
@ -108,8 +121,8 @@ class ActionModule(ActionBase):
|
|||
self._templar.environment.variable_start_string = variable_start_string
|
||||
if variable_end_string is not None:
|
||||
self._templar.environment.variable_end_string = variable_end_string
|
||||
if trim_blocks is not None:
|
||||
self._templar.environment.trim_blocks = bool(trim_blocks)
|
||||
self._templar.environment.trim_blocks = trim_blocks
|
||||
self._templar.environment.lstrip_blocks = lstrip_blocks
|
||||
|
||||
# add ansible 'template' vars
|
||||
temp_vars = task_vars.copy()
|
||||
|
@ -133,6 +146,7 @@ class ActionModule(ActionBase):
|
|||
new_task.args.pop('variable_start_string', None)
|
||||
new_task.args.pop('variable_end_string', None)
|
||||
new_task.args.pop('trim_blocks', None)
|
||||
new_task.args.pop('lstrip_blocks', None)
|
||||
|
||||
local_tempdir = tempfile.mkdtemp(dir=C.DEFAULT_LOCAL_TMP)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue