Skip to content

[Bug]: file.managed not rendering jinja variables in template properly #68754

@ramboman

Description

@ramboman

What happened?

When I use the jinja renderer directly on a template, it does not recognize the SLS specific variables. To get around that issue, I am making a .sls file which creates the SLS specific variables and then transfer them to the renderer through file.managed.

When I try to render a jinja template, its variables are not rendered properly.

Here is how to reproduce the problem:

  • Start from a fresh OS install. Mine is Fedora 42.
  • Bootstrap salt following these instructions
  • Create the following files:
    • /srv/salt/check_jinja.sls (the renderer):
{% load_yaml as args %}
output: /tmp/output.sls
isdir: false
{% endload %}
{% set _ = args.update(pillar.get(sls, {})) %}

{% set cache_dir = tplpath[:-((tplfile|length) + 1)] %}
{% set _sls = args.dotname %}
{% if args.isdir %}
{%   set _slsdotpath = _sls %}
{% else %}
{%   set _slsdotpath = _sls.split('.')[:-1] | join('.') %}
{% endif %}
{% set _slspath = _slsdotpath | replace('.', '/') %}
{% set _sls_path = _slsdotpath | replace('.', '_') %}
{% set _slscolonpath = _slsdotpath | replace('.', ':') %}
{% set _tplfile = (_sls | replace('.', '/')) + '.sls' %}
{% set _tplpath = cache_dir + '/' + _tplfile %}
{% set _tpldir = _sls.split('.')[:-1] | join('/') %}
{% if _tpldir == '' %}
{%   set _tpldir = '.' %}
{% endif %}
{% set _tpldot = _tpldir.replace('/', '.') %}

Check variables:
  cmd.run:
    - name: cat
    - stdin: |
        {{ args | yaml }}

        sls: {{ _sls }}
        slspath: {{ _slspath }}
        sls_path: {{ _sls_path }}
        slsdotpath: {{ _slsdotpath }}
        slscolonpath: {{ _slscolonpath }}
        tplpath: {{ _tplpath }}
        tplfile: {{ _tplfile }}
        tpldir: {{ _tpldir }}
        tpldot: {{ _tpldot }}

Check jinja rendering:
  file.managed:
    - name: {{ args.output }}
    - source: salt://{{ args.dotname | replace('.', '/') }}.sls
    - template: jinja
    - replace: True
    - defaults:
        sls: {{ _sls }}
        slspath: {{ _slspath }}
        sls_path: {{ _sls_path }}
        slsdotpath: {{ _slsdotpath }}
        slscolonpath: {{ _slscolonpath }}
        tplpath: {{ _tplpath }}
        tplfile: {{ _tplfile }}
        tpldir: {{ _tpldir }}
        tpldot: {{ _tpldot }}
  • /srv/salt/test/basic/output.sls (a basic jinja template to render):
sls: {{ sls }}
slspath: {{ slspath }}
sls_path: {{ sls_path }}
slsdotpath: {{ slsdotpath }}
slscolonpath: {{ slscolonpath }}
tplpath: {{ tplpath }}
tplfile: {{ tplfile }}
tpldir: {{ tpldir }}
tpldot: {{ tpldot }}
  • Execute the renderer on the basic jinja template:
$ sudo salt-call --local state.sls check_jinja pillar="{'check_jinja': {'dotname': 'test.basic.output'}}"
local:
----------
          ID: Check variables
    Function: cmd.run
        Name: cat
      Result: True
     Comment: Command "cat" run
     Started: 05:40:43.105144
    Duration: 3.879 ms
     Changes:   
              ----------
              pid:
                  8457
              retcode:
                  0
              stderr:
              stdout:
                  {dotname: test.basic.output, isdir: false, output: /tmp/output.sls}
                  
                  sls: test.basic.output
                  slspath: test/basic
                  sls_path: test_basic
                  slsdotpath: test.basic
                  slscolonpath: test:basic
                  tplpath: /var/cache/salt/minion/files/base/test/basic/output.sls
                  tplfile: test/basic/output.sls
                  tpldir: test/basic
                  tpldot: test.basic
----------
          ID: Check jinja rendering
    Function: file.managed
        Name: /tmp/output.sls
      Result: True
     Comment: File /tmp/output.sls updated
     Started: 05:40:43.110057
    Duration: 11.958 ms
     Changes:   
              ----------
              diff:
                  New file
              mode:
                  0644

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

Expected results:

  • The content of /tmp/output.sls:
sls: test.basic.output
slspath: test/basic
sls_path: test_basic
slsdotpath: test.basic
slscolonpath: test:basic
tplpath: /var/cache/salt/minion/files/base/test/basic/output.sls
tplfile: test/basic/output.sls
tpldir: test/basic
tpldot: test.basic

Actual results:

  • The content of /tmp/output.sls:
sls: test.basic.output
slspath: test/basic/output
sls_path: test_basic_output
slsdotpath: test.basic.output
slscolonpath: test:basic:output
tplpath: /var/cache/salt/minion/files/base/test/basic/output.sls
tplfile: test/basic/output.sls
tpldir: test/basic
tpldot: test.basic

Type of salt install

other (please specify in bug details)

Major version

3006.x

What supported OS are you seeing the problem on? Can select multiple. (If bug appears on an unsupported OS, please open a GitHub Discussion instead)

fedora-42

salt --versions-report output

Salt Version:
          Salt: 3006.21
 
Python Version:
        Python: 3.10.19 (main, Feb  5 2026, 07:05:38) [GCC 11.2.0]
 
Dependency Versions:
          cffi: 2.0.0
      cherrypy: 18.10.0
  cryptography: 42.0.5
      dateutil: 2.8.1
     docker-py: Not Installed
         gitdb: Not Installed
     gitpython: Not Installed
        Jinja2: 3.1.6
       libgit2: Not Installed
  looseversion: 1.0.2
      M2Crypto: Not Installed
          Mako: Not Installed
       msgpack: 1.0.2
  msgpack-pure: Not Installed
  mysql-python: Not Installed
     packaging: 24.0
     pycparser: 2.21
      pycrypto: Not Installed
  pycryptodome: 3.19.1
        pygit2: Not Installed
  python-gnupg: 0.4.8
        PyYAML: 6.0.1
         PyZMQ: 23.2.0
        relenv: 0.22.3
         smmap: Not Installed
       timelib: 0.3.0
       Tornado: 4.5.3
           ZMQ: 4.3.4
 
System Versions:
          dist: fedora 42 
        locale: utf-8
       machine: x86_64
       release: 6.12.64-1.qubes.fc41.x86_64
        system: Linux
       version: Fedora Linux 42

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugbroken, incorrect, or confusing behaviorneeds-triage

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions