diff options
author | Alicia Cozine <cozi@visi.com> | 2018-07-31 09:28:32 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-07-31 09:28:32 -0500 |
commit | 6edc740614f4c28aacd852f0d0ece562dfa19d8b (patch) | |
tree | f49eff8cc969d874b7f85ff3faa24b51a1d404f2 /docs | |
parent | 61901c35e15d6ec637008130abd423e12a4ecb0a (diff) | |
download | ansible-6edc740614f4c28aacd852f0d0ece562dfa19d8b.tar.gz |
Update JSON Query filter examples (#42432) (#43456)
* 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 7b0dea45e9e9ad14d1e3745fdae07b47da234b93)
Diffstat (limited to 'docs')
-rw-r--r-- | docs/docsite/rst/user_guide/playbooks_filters.rst | 35 |
1 files changed, 23 insertions, 12 deletions
diff --git a/docs/docsite/rst/user_guide/playbooks_filters.rst b/docs/docsite/rst/user_guide/playbooks_filters.rst index 7eac02e3f3..e307d08f19 100644 --- a/docs/docsite/rst/user_guide/playbooks_filters.rst +++ b/docs/docsite/rst/user_guide/playbooks_filters.rst @@ -370,40 +370,51 @@ Now, let's take the following data structure:: To extract all clusters from this structure, you can use the following query:: - name: "Display all cluster names" - debug: var=item - loop: "{{domain_definition|json_query('domain.cluster[*].name')}}" + debug: + var: item + loop: "{{ domain_definition | json_query('domain.cluster[*].name') }}" Same thing for all server names:: - name: "Display all server names" - debug: var=item - loop: "{{domain_definition|json_query('domain.server[*].name')}}" + debug: + var: item + loop: "{{ domain_definition | json_query('domain.server[*].name') }}" This example shows ports from cluster1:: - - name: "Display all server names from cluster1" - debug: var=item - loop: "{{domain_definition|json_query(server_name_cluster1_query)}}" + - name: "Display all ports from cluster1" + debug: + var: item + loop: "{{ domain_definition | json_query(server_name_cluster1_query) }}" vars: server_name_cluster1_query: "domain.server[?cluster=='cluster1'].port" .. 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: - var: item - loop: "{{domain_definition|json_query('domain.server[?cluster=`cluster1`].port')}}" + msg: "{{ domain_definition | json_query('domain.server[?cluster==`cluster1`].port') | join(', ') }}" .. 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:: - name: "Display all server ports and names from cluster1" debug: var: item - loop: "{{domain_definition|json_query(server_name_cluster1_query)}}" + loop: "{{ domain_definition | json_query(server_name_cluster1_query) }}" vars: server_name_cluster1_query: "domain.server[?cluster=='cluster2'].{name: name, port: port}" |