diff --git a/changelogs/fragments/10539-json_query.yml b/changelogs/fragments/10539-json_query.yml new file mode 100644 index 0000000000..7e84b7ecb0 --- /dev/null +++ b/changelogs/fragments/10539-json_query.yml @@ -0,0 +1,2 @@ +bugfixes: + - "json_query filter plugin - make compatible with lazy evaluation list and dictionary types of ansible-core 2.19 (https://github.com/ansible-collections/community.general/pull/10539)." diff --git a/plugins/filter/json_query.py b/plugins/filter/json_query.py index 8976694a94..9c0a83a481 100644 --- a/plugins/filter/json_query.py +++ b/plugins/filter/json_query.py @@ -124,10 +124,17 @@ def json_query(data, expr): 'json_query filter') # Hack to handle Ansible Unsafe text, AnsibleMapping and AnsibleSequence - # See issue: https://github.com/ansible-collections/community.general/issues/320 - jmespath.functions.REVERSE_TYPES_MAP['string'] = jmespath.functions.REVERSE_TYPES_MAP['string'] + ('AnsibleUnicode', 'AnsibleUnsafeText', ) - jmespath.functions.REVERSE_TYPES_MAP['array'] = jmespath.functions.REVERSE_TYPES_MAP['array'] + ('AnsibleSequence', ) - jmespath.functions.REVERSE_TYPES_MAP['object'] = jmespath.functions.REVERSE_TYPES_MAP['object'] + ('AnsibleMapping', ) + # See issues https://github.com/ansible-collections/community.general/issues/320 + # and https://github.com/ansible/ansible/issues/85600. + jmespath.functions.REVERSE_TYPES_MAP['string'] = jmespath.functions.REVERSE_TYPES_MAP['string'] + ( + 'AnsibleUnicode', 'AnsibleUnsafeText', '_AnsibleTaggedStr', + ) + jmespath.functions.REVERSE_TYPES_MAP['array'] = jmespath.functions.REVERSE_TYPES_MAP['array'] + ( + 'AnsibleSequence', '_AnsibleLazyTemplateList', + ) + jmespath.functions.REVERSE_TYPES_MAP['object'] = jmespath.functions.REVERSE_TYPES_MAP['object'] + ( + 'AnsibleMapping', '_AnsibleLazyTemplateDict', + ) try: return jmespath.search(expr, data) except jmespath.exceptions.JMESPathError as e: diff --git a/tests/integration/targets/filter_json_query/tasks/main.yml b/tests/integration/targets/filter_json_query/tasks/main.yml index 92db6d876a..0195ddb5dd 100644 --- a/tests/integration/targets/filter_json_query/tasks/main.yml +++ b/tests/integration/targets/filter_json_query/tasks/main.yml @@ -11,4 +11,23 @@ - name: Test json_query filter assert: that: - - "users | community.general.json_query('[*].hosts[].host') == ['host_a', 'host_b', 'host_c', 'host_d']" + - >- + users | community.general.json_query('[*].hosts[].host') == ['host_a', 'host_b', 'host_c', 'host_d'] + - >- + ports | json_query("[?contains(ports, `22`)]") == [ports[0]] + - >- + ports | json_query("[?contains(rule_desc, `ssh`)]") == [ports[0]] + - >- + my_complex_data | json_query('users[?id==`1`]') == [my_complex_data['users'][0]] + vars: + my_complex_data: + users: + - id: 1 + name: Alice + roles: ["admin", "dev"] + status: active + ports: + - ports: [22] + rule_desc: "ssh" + - ports: [80] + rule_desc: "http"