Add trim_blocks=False argument to template lookup plugin #75962
Labels
affects_2.13
feature
This issue/PR relates to a feature request.
P3
Priority 3 - Approved, No Time Limitation
support:core
This issue/PR relates to code supported by the Ansible Engineering Team.
Summary
The
template
module has an option fortrim_blocks=False
. I would like to have the same option with thetemplate
lookup plugin.An alternative solution would be to make
trim_blocks
in thetemplate
module recursively apply to any template lookups inside that template.My use case is that I'm generating AWS CloudFormation yaml templates with Jinja. Since some CloudFormation templates require nested json or yaml as an escaped literal string inside the overall yaml, I have recursive templates. For the top-level template I use the
template
module withtrim_blocks=False
. And inside that template I use stuff like{{ lookup('template', x) | from_yaml | to_json }}
plugin to evaluate other templates, then convert them to json to embed as a string inside my overall yaml template. This fails currently because the default (and only) behavior oflookup('template', x)
is to trim blocks. When my jinja template is of something where indentation matters (e.g. yaml), this messes with the indentation in a way that changes the meaning of the file entirely, typically resulting in something that is broken and undeployable. This might sound like an obscure use case, but really this applies to any use of the template plugin to render a template where leading whitespace matters (e.g. yaml, python, markdown)Issue Type
Feature Idea
Component Name
lib/ansible/plugins/lookup/template.py
Additional Information
test.j2
(note the leading spaces)playbook:
Desired behavior: assertion passes
Current behavior: assertion fails.
trim_blocks
in lookup is ignored.Code of Conduct
The text was updated successfully, but these errors were encountered: