Hi Pavel.

I am experiencing issues with json_query() filter plugin. I am hitting
the issue as reported to upstream at:

https://github.com/ansible-collections/community.general/pull/10539

On my end the code fails as follows:

fatal: [testmachine10]: FAILED! => {"changed": false, "msg": "Task failed:
Finalization of task args for 'ansible.builtin.set_fact' failed: Error
while resolving value for 'tokens_to_deploy_transformed': The filter
plugin 'community.general.json_query' failed: JMESPathError in
json_query filter plugin:\nIn function join(), invalid type for value:
vault-test-a, expected one of: ['array-string'], received:
\"_AnsibleTaggedStr\""}

Ansible task definition looks as:

- name: transform target tokens for easier lookup
  ansible.builtin.set_fact:
    tokens_to_deploy_transformed: "{{
      tokens_to_deploy
      | json_query('[].join(`/`, [cluster, prefix])')
      | zip(tokens_to_deploy | map(attribute='name'))
      | map('pathjoin')
      | zip(tokens_to_deploy)
      | items2dict(key_name=0, value_name=1)
    }}"

Diff from above GitHub pull request 10539 fixes the problem for me. See
below diff for OpenBSD port.


Index: Makefile
===================================================================
RCS file: /cvs/ports/sysutils/ansible/Makefile,v
diff -u -p -u -r1.207 Makefile
--- Makefile    17 Jul 2025 20:44:55 -0000      1.207
+++ Makefile    6 Aug 2025 15:23:01 -0000
@@ -2,6 +2,7 @@ COMMENT =               radically simple IT automatio
 
 MODPY_DISTV =          11.8.0
 DISTNAME =             ansible-${MODPY_DISTV}
+REVISION =             0
 
 CATEGORIES =           sysutils
 
@@ -22,7 +23,7 @@ RUN_DEPENDS =         sysutils/ansible-core>=2.
 post-install:
        ${INSTALL_DATA_DIR} ${PREFIX}/share/doc/ansible
        ${INSTALL_DATA} ${WRKSRC}/CHANGELOG-v*.rst ${PREFIX}/share/doc/ansible
-       find ${PREFIX} -type f \( -name '.*' -o -name '*.orig' \) -delete
+       find ${PREFIX} -type f \( -name '.*' -o -name '*.orig' -o -name 
'*.orig.port' \) -delete
        find ${PREFIX} -type d \( -name '.*' -o -name changelogs \
                -o -name docs -o -name tests \) -exec rm -rf {} +
 
Index: 
patches/patch-ansible_collections_community_general_plugins_filter_json_query_py
===================================================================
RCS file: 
patches/patch-ansible_collections_community_general_plugins_filter_json_query_py
diff -N 
patches/patch-ansible_collections_community_general_plugins_filter_json_query_py
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ 
patches/patch-ansible_collections_community_general_plugins_filter_json_query_py
    6 Aug 2025 15:23:01 -0000
@@ -0,0 +1,28 @@
+# json_query: extend list of type aliases for compatibility with ansible-core 
2.19
+- https://github.com/ansible-collections/community.general/pull/10539
+
+Index: ansible_collections/community/general/plugins/filter/json_query.py
+--- ansible_collections/community/general/plugins/filter/json_query.py.orig
++++ ansible_collections/community/general/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:
Index: 
patches/patch-ansible_collections_community_general_tests_integration_targets_filter_json_query_tasks_main_yml
===================================================================
RCS file: 
patches/patch-ansible_collections_community_general_tests_integration_targets_filter_json_query_tasks_main_yml
diff -N 
patches/patch-ansible_collections_community_general_tests_integration_targets_filter_json_query_tasks_main_yml
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ 
patches/patch-ansible_collections_community_general_tests_integration_targets_filter_json_query_tasks_main_yml
      6 Aug 2025 15:23:01 -0000
@@ -0,0 +1,31 @@
+# json_query: extend list of type aliases for compatibility with ansible-core 
2.19
+- https://github.com/ansible-collections/community.general/pull/10539
+
+Index: 
ansible_collections/community/general/tests/integration/targets/filter_json_query/tasks/main.yml
+--- 
ansible_collections/community/general/tests/integration/targets/filter_json_query/tasks/main.yml.orig
++++ 
ansible_collections/community/general/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"


-- 
Regards,
 Mikolaj

Reply via email to