Salt: Jinja variable containing an empty string leads to a None value

Created on 5 Jul 2018  路  4Comments  路  Source: saltstack/salt

Description of Issue/Question

Setup

From 2018.3.2 (or maybe a little before, didn't test), salt is treating an empty string in a jinja variable as a None value.

Steps to Reproduce Issue

jinjaerror.sls

{% set empty_str = "" %}
{% set str = "mystr" %}

myfile-state-context:
    file.managed:
        - name: /tmp/test.txt
        - template: jinja
        - source: salt://jinjaerror.cfg
        - context:
            empty_str: {{ empty_str }}
            empty_str2: {{ empty_str }}-test
            empty_str3: {{ empty_str }}""
            empty_str4: ""
            empty_str5: ''
            str: {{ str }}

myfile-state-contents:
    file.managed:
        - name: /tmp/test2.txt
        - contents: |
            empty_str  = {{ empty_str }}
            empty_str2 = {{ empty_str }}-test
            empty_str3 = {{ empty_str }}""
            empty_str4 = ""
            empty_str5 = ''
            str        = {{ str }}

jinjaerror.cfg

empty_str   = {{ empty_str }}
empty_str2  = {{ empty_str2 }}
empty_str3  = {{ empty_str3 }}
empty_str4  = {{ empty_str4 }}
empty_str5  = {{ empty_str5 }}
str         = {{ str }}

Execution :

local:
----------
          ID: myfile-state-context
    Function: file.managed
        Name: /tmp/test.txt
      Result: True
     Comment: File /tmp/test.txt updated
     Started: 12:06:11.664085
    Duration: 164.305 ms
     Changes:   
              ----------
              diff:
                  --- 
                  +++ 
                  @@ -0,0 +1,6 @@
                  +empty_str   = None
                  +empty_str2  = -test
                  +empty_str3  = 
                  +empty_str4  = 
                  +empty_str5  = 
                  +str         = mystr
----------
          ID: myfile-state-contents
    Function: file.managed
        Name: /tmp/test2.txt
      Result: True
     Comment: File /tmp/test2.txt updated
     Started: 12:06:11.828707
    Duration: 31.816 ms
     Changes:   
              ----------
              diff:
                  --- 
                  +++ 
                  @@ -0,0 +1,6 @@
                  +empty_str  = 
                  +empty_str2 = -test
                  +empty_str3 = ""
                  +empty_str4 = ""
                  +empty_str5 = ''
                  +str        = mystr

Summary for local
------------
Succeeded: 2 (changed=2)
Failed:    0
------------
Total states run:     2
Total run time: 196.121 ms

Versions Report

Salt Version:
           Salt: 2018.3.2

Dependency Versions:
           cffi: Not Installed
       cherrypy: 3.5.0
       dateutil: 2.5.3
      docker-py: Not Installed
          gitdb: Not Installed
      gitpython: Not Installed
          ioflo: Not Installed
         Jinja2: 2.9.4
        libgit2: Not Installed
        libnacl: Not Installed
       M2Crypto: 0.24.0
           Mako: Not Installed
   msgpack-pure: Not Installed
 msgpack-python: 0.4.8
   mysql-python: Not Installed
      pycparser: Not Installed
       pycrypto: 2.6.1
   pycryptodome: Not Installed
         pygit2: Not Installed
         Python: 2.7.13 (default, Nov 24 2017, 17:33:09)
   python-gnupg: Not Installed
         PyYAML: 3.12
          PyZMQ: 16.0.2
           RAET: Not Installed
          smmap: Not Installed
        timelib: Not Installed
        Tornado: 4.4.3
            ZMQ: 4.2.1

System Versions:
           dist: debian 9.4 
         locale: UTF-8
        machine: x86_64
        release: 4.13.16-2-pve
         system: Linux
        version: debian 9.4 

Pending Discussion expected-behavior

Most helpful comment

this is how yaml works.

so, empty_str, even though you assigned "", it is actually nothing, so when you dump it out, you end up with this.

        - context:
            empty_str: 
            empty_str2: -test
            empty_str3: ""
            empty_str4: ""
            empty_str5: ''
            str: {{ str }}

And pyyaml interprets nothing being assigned to it as None, so this is the expected behavior.

If you want it to be an empty string, assign the variable a string of an empty string.

{% set empty_str = "''" %}

and that should dump out '' when the jinja is rendered.

All 4 comments

looks like i'm able to replicate this and in older versions of salt.

ping @saltstack/team-core any ideas here? Is this just a part of jinja we would need to work around?

this is how yaml works.

so, empty_str, even though you assigned "", it is actually nothing, so when you dump it out, you end up with this.

        - context:
            empty_str: 
            empty_str2: -test
            empty_str3: ""
            empty_str4: ""
            empty_str5: ''
            str: {{ str }}

And pyyaml interprets nothing being assigned to it as None, so this is the expected behavior.

If you want it to be an empty string, assign the variable a string of an empty string.

{% set empty_str = "''" %}

and that should dump out '' when the jinja is rendered.

You could also just do something like:

- context:
    foo: "{{ somevar }}"

The jinja still gets processed before the YAML, so the result would be an empty quoted string.

Closing this issue as this is th expected behaviour and workaround propositions worked fine.

Was this page helpful?
0 / 5 - 0 ratings