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

@max-arnold max-arnold commented Nov 8, 2018

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 as a code owner Nov 8, 2018
@ghost ghost self-requested a review Nov 8, 2018
dwoz
dwoz approved these changes Nov 8, 2018
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
isbm
isbm approved these changes Nov 8, 2018
@max-arnold
Copy link
Contributor Author

@max-arnold max-arnold commented Nov 8, 2018

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

Copy link
Contributor

@cachedout cachedout left a comment

I really like this. Excellent idea.

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

@mchugh19 mchugh19 commented Jul 15, 2019

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

@max-arnold
Copy link
Contributor Author

@max-arnold max-arnold commented Jul 16, 2019

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

garethgreenaway added a commit to garethgreenaway/salt that referenced this issue Sep 18, 2019
@waynew waynew added this to PR needs port to master in PRs to port to master Oct 17, 2019
@waynew waynew added the has master-port label Oct 17, 2019
@waynew waynew moved this from PR needs port to master to PR has port to master in PRs to port to master Oct 17, 2019
s0undt3ch added a commit to garethgreenaway/salt that referenced this issue 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
Projects
PRs to port to master
  
PR has port to master
Development

Successfully merging this pull request may close these issues.

None yet

6 participants