Skip to content

[BUG] Unable to use random shuffle and sample functions as Jinja filters #62372

@nicholasmhughes

Description

@nicholasmhughes

Description
Unable to use random shuffle and sample functions as Jinja filters despite them being decorated with @jinja_filter

[ERROR   ] Rendering exception occurred
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/salt/utils/templates.py", line 468, in render_jinja_tmpl
    template = jinja_env.from_string(tmplstr)
  File "/usr/local/lib/python3.7/dist-packages/jinja2/environment.py", line 1105, in from_string
    return cls.from_code(self, self.compile(source), gs, None)
  File "/usr/local/lib/python3.7/dist-packages/jinja2/environment.py", line 768, in compile
    self.handle_exception(source=source_hint)
  File "/usr/local/lib/python3.7/dist-packages/jinja2/environment.py", line 936, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<unknown>", line 1, in template
jinja2.exceptions.TemplateAssertionError: No filter named 'random_sample'.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/salt/utils/templates.py", line 216, in render_tmpl
    output = render_str(tmplstr, context, tmplpath)
  File "/usr/local/lib/python3.7/dist-packages/salt/utils/templates.py", line 486, in render_jinja_tmpl
    "Jinja syntax error: {}{}".format(exc, out), line, tmplstr
salt.exceptions.SaltRenderError: Jinja syntax error: No filter named 'random_sample'.; line 1

---
{% do salt.log.error(['one', 'two', 'three', 'four'] | random_sample(2, seed='static')) -%}    <======================
---
[CRITICAL] Rendering SLS 'base:random' failed: Jinja syntax error: No filter named 'random_sample'.; line 1

---
{% do salt.log.error(['one', 'two', 'three', 'four'] | random_sample(2, seed='static')) -%}    <======================
---
local:
    Data failed to compile:
----------
    Rendering SLS 'base:random' failed: Jinja syntax error: No filter named 'random_sample'.; line 1

---
{% do salt.log.error(['one', 'two', 'three', 'four'] | random_sample(2, seed='static')) -%}    <======================
---

Setup

# /srv/salt/random.sls
{% do salt.log.error(['one', 'two', 'three', 'four'] | random_sample(2, seed='static')) -%}

Steps to Reproduce the behavior

salt-call state.apply random

Expected behavior
Filters should be available.

Versions Report

salt --versions-report (Provided by running salt --versions-report. Please also mention any differences in master/minion versions.)
Salt Version:
          Salt: 3005rc1
 
Dependency Versions:
          cffi: 1.14.6
      cherrypy: 18.6.1
      dateutil: 2.8.1
     docker-py: Not Installed
         gitdb: Not Installed
     gitpython: Not Installed
        Jinja2: 3.1.0
       libgit2: Not Installed
      M2Crypto: Not Installed
          Mako: Not Installed
       msgpack: 1.0.2
  msgpack-pure: Not Installed
  mysql-python: Not Installed
     pycparser: 2.17
      pycrypto: Not Installed
  pycryptodome: 3.9.8
        pygit2: Not Installed
        Python: 3.9.12 (main, Jul 20 2022, 19:55:23)
  python-gnupg: 0.4.8
        PyYAML: 5.4.1
         PyZMQ: 23.2.0
         smmap: Not Installed
       timelib: 0.2.4
       Tornado: 4.5.3
           ZMQ: 4.3.4
 
System Versions:
          dist: manjaro 21.3.5 Ruah
        locale: utf-8
       machine: x86_64
       release: 5.10.131-1-MANJARO
        system: Linux
       version: Manjaro Linux 21.3.5 Ruah

Additional context
Feature added in #62227 and tested in 3005.0 RC2

Metadata

Metadata

Assignees

No one assigned

    Labels

    Phosphorus v3005.0Release code name and versionbugbroken, incorrect, or confusing behavior

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions