saltstack / saltstack/salt

Add a new jinja filter `items`

Open
#57,970 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Feature
Dominant language
Python
Stars
15.7k
Forks
5.6k
Avg merge
2d 44m
Merged PRs (30d)
80

Description

Is your feature request related to a problem? Please describe.

When using jinja filters where the result is a dictionary, we can't process the result directly.
We need to pass by an intermediate variable.

Here is an example inspired by my last openssh-formula contribution:

{%- set public_ssh_host_keys = known_hosts | traverse('salt_ssh:public_ssh_host_keys', {}) %}
{%- for minion_id, minion_host_keys in public_ssh_host_keys.items() -%}

Describe the solution you'd like

I would like to be able to do:

{%- for minion_id, minion_host_keys in known_hosts | traverse('salt_ssh:public_ssh_host_keys', {}) | items -%}                                                                       

Describe alternatives you've considered

I first used dictsort which does the job:

{%- for minion_id, minion_host_keys in known_hosts | traverse('salt_ssh:public_ssh_host_keys', {}) | dictsort -%}                                                                       

But there is an issue with dictsort.

Additional context

Salt Version:
           Salt: 3001
 
Dependency Versions:
           cffi: Not Installed
       cherrypy: Not Installed
       dateutil: 2.7.3
      docker-py: Not Installed
          gitdb: Not Installed
      gitpython: Not Installed
         Jinja2: 2.10
        libgit2: 0.27.7
       M2Crypto: Not Installed
           Mako: Not Installed
   msgpack-pure: Not Installed
 msgpack-python: 0.5.6
   mysql-python: Not Installed
      pycparser: 2.19
       pycrypto: Not Installed
   pycryptodome: 3.6.1
         pygit2: 0.27.4
         Python: 3.7.3 (default, Dec 20 2019, 18:57:59)
   python-gnupg: Not Installed
         PyYAML: 3.13
          PyZMQ: 17.1.2
          smmap: Not Installed
        timelib: Not Installed
        Tornado: 4.5.3
            ZMQ: 4.3.1
 
System Versions:
           dist: debian 10 buster
         locale: UTF-8
        machine: x86_64
        release: 4.19.0-9-amd64
         system: Linux
        version: Debian GNU/Linux 10 buster

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

The issue names no implementation files or tests. Start by locating Salt's Jinja filter registration and existing filter tests, then compare the requested chained items example with the dictsort alternative. Done means the documented loop works directly on the traversed dictionary result and the behavior is covered by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.