Skip to content

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
@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