saltstack / saltstack/salt

render_jinja_tmpl renders an extra newline

Open
#56,568 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug Confirmed severity-low
Dominant language
Python
Stars
15.7k
Forks
5.6k
Avg merge
2d 44m
Merged PRs (30d)
80

Description

Description of Issue

An extra line is rendered if file template end with jinja template

Setup

Bug definitely wasn't there until we updated from 2017.7.8 on python2.7 to 2019.2.3 on python3.6.
It was probably introduced with #2773.

Steps to Reproduce Issue
$ srv/salt/st.sls
/tmp/keys.txt:
  file.managed:
    - defaults:
        keys:
          - key1
          - key2
    - source: salt:///keys.txt.tpl
    - template: jinja

$ echo ----- && cat srv/salt/keys.txt.tpl && echo -----
-----
{% for key in keys %}
{{ key }}
{% endfor -%}
-----

Execute:

$ salt-call state.sls st --local 
local:
----------
          ID: /tmp/keys.txt
    Function: file.managed
      Result: True
     Comment: File /tmp/keys.txt is in the correct state
     Started: 18:24:46.681642
    Duration: 16.955 ms
     Changes:   

Summary for local
------------
Succeeded: 1
Failed:    0
------------
Total states run:     1
Total run time:  16.955 ms

Expected result:

$ echo ---- && cat /tmp/keys.txt && echo ----
----
key1
key2
----

Actual result:

$ echo ---- && cat /tmp/keys.txt && echo ----
----
key1
key2

----

There's a blank line at the end of file. If you add non-jinja text to the template file you won't get an empty line:

$ echo ----- && cat srv/salt/keys.txt.tpl && echo -----
-----
{% for key in keys %}
{{ key }}
{% endfor -%}
non-jinja text
-----
$ salt-call state.sls st --local 
...
$ echo ---- && cat /tmp/keys.txt && echo ----
----
key1
key2
non-jinja text
----

Config file has all the necessary options:

...
jinja_env:
  lstrip_blocks: true
  trim_blocks: true
jinja_sls_env:
  lstrip_blocks: true
  trim_blocks: true
...
Versions Report

Lab environment

Salt Version:
           Salt: 2019.2.3
 
Dependency Versions:
           cffi: Not Installed
       cherrypy: Not Installed
       dateutil: Not Installed
      docker-py: Not Installed
          gitdb: Not Installed
      gitpython: Not Installed
          ioflo: Not Installed
         Jinja2: 2.11.1
        libgit2: Not Installed
        libnacl: Not Installed
       M2Crypto: Not Installed
           Mako: Not Installed
   msgpack-pure: Not Installed
 msgpack-python: 1.0.0
   mysql-python: Not Installed
      pycparser: Not Installed
       pycrypto: 2.6.1
   pycryptodome: Not Installed
         pygit2: Not Installed
         Python: 3.6.9 (default, Apr  7 2020, 14:58:55)
   python-gnupg: Not Installed
         PyYAML: 3.13
          PyZMQ: 19.0.0
           RAET: Not Installed
          smmap: Not Installed
        timelib: Not Installed
        Tornado: 4.5.3
            ZMQ: 4.3.2
 
System Versions:
           dist: debian buster/sid 
         locale: UTF-8
        machine: x86_64
        release: 5.3.0-18-generic
         system: Linux
        version: debian buster/sid 

Prod environment

Salt Version:
           Salt: 2019.2.3
 
Dependency Versions:
           cffi: 1.14.0
       cherrypy: Not Installed
       dateutil: 2.8.1
      docker-py: Not Installed
          gitdb: Not Installed
      gitpython: Not Installed
          ioflo: Not Installed
         Jinja2: 2.11.1
        libgit2: 0.99.0
        libnacl: 1.6.1
       M2Crypto: Not Installed
           Mako: 1.0.6
   msgpack-pure: Not Installed
 msgpack-python: 0.6.2
   mysql-python: Not Installed
      pycparser: 2.19
       pycrypto: 3.9.4
   pycryptodome: Not Installed
         pygit2: 1.1.1
         Python: 3.6.10 (default, Mar 25 2020, 21:33:24)
   python-gnupg: 0.4.5
         PyYAML: 5.3
          PyZMQ: 17.1.0
           RAET: Not Installed
          smmap: Not Installed
        timelib: Not Installed
        Tornado: 4.5.3
            ZMQ: 4.3.2
 
System Versions:
           dist: gentoo 2.6 
         locale: UTF-8
        machine: x86_64
        release: 4.19.44-gentoo-xen-guest-cgroups
         system: Linux
        version: Gentoo Base System 2.6 

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the provided st.sls and keys.txt.tpl inputs using salt-call state.sls st --local, then trace the render_jinja_tmpl entry point. Done means the Jinja-only template produces key1 and key2 without an extra blank line at the end while preserving the shown non-Jinja case.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devops, infrastructure
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.