Update JSON Query filter examples (#42432) (#43491)

* Update JSON Query filter examples

Correct syntax on one example
Add more examples of escaping
Change example to show joining list to string

(cherry picked from commit 7b0dea45e9)
pull/43518/head
Alicia Cozine 6 years ago committed by GitHub
parent 41a0bcf2fc
commit da043d635e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -288,40 +288,51 @@ Now, let's take the following data structure::
To extract all clusters from this structure, you can use the following query:: To extract all clusters from this structure, you can use the following query::
- name: "Display all cluster names" - name: "Display all cluster names"
debug: var=item debug:
loop: "{{domain_definition|json_query('domain.cluster[*].name')}}" var: item
loop: "{{ domain_definition | json_query('domain.cluster[*].name') }}"
Same thing for all server names:: Same thing for all server names::
- name: "Display all server names" - name: "Display all server names"
debug: var=item debug:
loop: "{{domain_definition|json_query('domain.server[*].name')}}" var: item
loop: "{{ domain_definition | json_query('domain.server[*].name') }}"
This example shows ports from cluster1:: This example shows ports from cluster1::
- name: "Display all server names from cluster1" - name: "Display all ports from cluster1"
debug: var=item debug:
loop: "{{domain_definition|json_query(server_name_cluster1_query)}}" var: item
loop: "{{ domain_definition | json_query(server_name_cluster1_query) }}"
vars: vars:
server_name_cluster1_query: "domain.server[?cluster=='cluster1'].port" server_name_cluster1_query: "domain.server[?cluster=='cluster1'].port"
.. note:: You can use a variable to make the query more readable. .. note:: You can use a variable to make the query more readable.
Or, alternatively:: Or, alternatively print out the ports in a comma separated string::
- name: "Display all server names from cluster1" - name: "Display all ports from cluster1 as a string"
debug: debug:
var: item msg: "{{ domain_definition | json_query('domain.server[?cluster==`cluster1`].port') | join(', ') }}"
loop: "{{domain_definition|json_query('domain.server[?cluster=`cluster1`].port')}}"
.. note:: Here, quoting literals using backticks avoids escaping quotes and maintains readability. .. note:: Here, quoting literals using backticks avoids escaping quotes and maintains readability.
Or, using YAML `single quote escaping <http://yaml.org/spec/current.html#id2534365>`_::
- name: "Display all ports from cluster1"
debug:
var: item
loop: "{{ domain_definition | json_query('domain.server[?cluster==''cluster1''].port') }}"
.. note:: Escaping single quotes within single quotes in YAML is done by doubling the single quote.
In this example, we get a hash map with all ports and names of a cluster:: In this example, we get a hash map with all ports and names of a cluster::
- name: "Display all server ports and names from cluster1" - name: "Display all server ports and names from cluster1"
debug: debug:
var: item var: item
loop: "{{domain_definition|json_query(server_name_cluster1_query)}}" loop: "{{ domain_definition | json_query(server_name_cluster1_query) }}"
vars: vars:
server_name_cluster1_query: "domain.server[?cluster=='cluster2'].{name: name, port: port}" server_name_cluster1_query: "domain.server[?cluster=='cluster2'].{name: name, port: port}"

Loading…
Cancel
Save