Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add trim_blocks=False argument to template lookup plugin #75962

Open
1 task done
mdavis-xyz opened this issue Oct 8, 2021 · 3 comments
Open
1 task done

Add trim_blocks=False argument to template lookup plugin #75962

mdavis-xyz opened this issue Oct 8, 2021 · 3 comments
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.

Comments

@mdavis-xyz
Copy link
Contributor

mdavis-xyz commented Oct 8, 2021

Summary

The template module has an option for trim_blocks=False. I would like to have the same option with the template lookup plugin.

An alternative solution would be to make trim_blocks in the template 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 with trim_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 of lookup('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)

  First Line
  {# comment #}
  Second Line
  {#-comment #}
  third line

playbook:

---
- hosts: localhost
  connection: local
  vars:
    path: 'test.j2'
    module_output: 'module-output.txt'
    plugin_output: 'plugin-output.txt'
  tasks:
    - name: use template module
      template:
        src: "{{ path }}"
        dest: "{{ module_output }}"
        trim_blocks: False

    - name: lookup plugin
      copy:
        content: "{{ lookup('template', path, trim_blocks=False) }}"
        dest: "{{ plugin_output }}"
        
    - name: compare
      assert:
        that:
        - lookup('file', plugin_output) == lookup('file', module_output)

Desired behavior: assertion passes

Current behavior: assertion fails. trim_blocks in lookup is ignored.

Code of Conduct

  • I agree to follow the Ansible Code of Conduct
@ansibot
Copy link
Contributor

ansibot commented Oct 8, 2021

Files identified in the description:

If these files are incorrect, please update the component name section of the description or use the !component bot command.

click here for bot help

@ansibot ansibot added affects_2.13 feature This issue/PR relates to a feature request. needs_triage Needs a first human triage before being processed. support:core This issue/PR relates to code supported by the Ansible Engineering Team. labels Oct 8, 2021
@mdavis-xyz
Copy link
Contributor Author

I'm struggling to find the right part of the codebase for this change.

I think this is the relevant part of the template module, and this is the relevant part of the plugin module. So I just need to add another argument to that second link?

@sivel
Copy link
Member

sivel commented Oct 8, 2021

lib/ansible/plugins/lookup/template.py is where it needs to go

@bcoca bcoca added P3 Priority 3 - Approved, No Time Limitation and removed needs_triage Needs a first human triage before being processed. labels Oct 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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.
Projects
None yet
Development

No branches or pull requests

4 participants