Skip to content

Commit

Permalink
json_query: Add examples for starts_with and contains (ansible#72821)
Browse files Browse the repository at this point in the history
* Add a note about - data structure returned from register variables
needs to be parsed using ``to_json | from_json`` in order to
get correct result.
* Add examples for starts_with and contains

Fixes: ansible-collections/community.general#320

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
  • Loading branch information
Akasurde committed Dec 3, 2020
1 parent 0a7fcd1 commit 38f2155
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions docs/docsite/rst/user_guide/playbooks_filters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ If you are reading in some already formatted data::
for example::

tasks:
- name: Register JSON output as a variable
- name: Register JSON output as a variable
ansible.builtin.shell: cat /some/path/to/file.json
register: result

Expand Down Expand Up @@ -749,8 +749,8 @@ To extract all server names::

To extract ports from cluster1::

- ansible.builtin.name: Display all ports from cluster1
debug:
- name: Display all ports from cluster1
ansible.builtin.debug:
var: item
loop: "{{ domain_definition | community.general.json_query(server_name_cluster1_query) }}"
vars:
Expand Down Expand Up @@ -784,6 +784,24 @@ To get a hash map with all ports and names of a cluster::
vars:
server_name_cluster1_query: "domain.server[?cluster=='cluster2'].{name: name, port: port}"

To extract ports from all clusters with name starting with 'server1'::

- name: Display all ports from cluster1
ansible.builtin.debug:
msg: "{{ domain_definition | to_json | from_json | community.general.json_query(server_name_query) }}"
vars:
server_name_query: "domain.server[?starts_with(name,'server1')].port"

To extract ports from all clusters with name containing 'server1'::

- name: Display all ports from cluster1
ansible.builtin.debug:
msg: "{{ domain_definition | to_json | from_json | community.general.json_query(server_name_query) }}"
vars:
server_name_query: "domain.server[?contains(name,'server1')].port"

.. note:: while using ``starts_with`` and ``contains``, you have to use `` to_json | from_json `` filter for correct parsing of data structure.


Randomizing data
================
Expand Down

0 comments on commit 38f2155

Please sign in to comment.