Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port json_query Jinja filter from Ansible #50428

Merged
merged 2 commits into from
Nov 8, 2018

Conversation

max-arnold
Copy link
Contributor

What does this PR do?

This is a port of Ansible json_query Jinja filter to make complex queries against json data structures. Query language http://jmespath.org/ parser depends on jmespath python library (optional).

It could be used to filter pillar data, yaml maps, and in combination withhttp_query. Can replace lots of ugly Jinja loops and in some cases help to avoid writing trivial custom modules https://docs.saltstack.com/en/latest/topics/tutorials/jinja_to_execution_module.html

Jinja template:

  Example 1: {{ [1, 2, 3, 4, [5, 6]] | json_query('[]') }}

  Example 2: {{
  {"machines": [
    {"name": "a", "state": "running"},
    {"name": "b", "state": "stopped"},
    {"name": "b", "state": "running"}
  ]} | json_query("machines[?state=='running'].name") }}

  Example 3: {{
  {"services": [
    {"name": "http", "host": "1.2.3.4", "port": 80},
    {"name": "smtp", "host": "1.2.3.5", "port": 25},
    {"name": "ssh",  "host": "1.2.3.6", "port": 22},
  ]} | json_query("services[].port") }}

Rendered output:

  Example 1: [1, 2, 3, 4, 5, 6]

  Example 2: ['a', 'b']

  Example 3: [80, 25, 22]

Tests written?

Yes

Commits signed with GPG?

No

P.S. The feature is not intrusive, how about including it into the Fluorine branch?

@max-arnold max-arnold requested a review from a team as a code owner November 8, 2018 08:52
@ghost ghost self-requested a review November 8, 2018 08:52
salt/utils/data.py Outdated Show resolved Hide resolved
salt/utils/data.py Show resolved Hide resolved
salt/utils/data.py Outdated Show resolved Hide resolved
@max-arnold
Copy link
Contributor Author

Are jinja filters accessible in other salt modules as simple functions?

Copy link
Contributor

@cachedout cachedout left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like this. Excellent idea.

@cachedout cachedout merged commit 30e09a6 into saltstack:develop Nov 8, 2018
@max-arnold max-arnold deleted the json-query branch December 15, 2018 10:10
@mchugh19
Copy link
Contributor

mchugh19 commented Jul 15, 2019

Great! Probably needs a mention in the release notes as well. @max-arnold

@max-arnold
Copy link
Contributor Author

@mchugh19 Oops, I completely forgot about that. Thanks for the reminder! Fixed in #53866

@waynew waynew added the has master-port port to master has been created label Oct 17, 2019
s0undt3ch added a commit to garethgreenaway/salt that referenced this pull request Oct 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
has master-port port to master has been created
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants