# Using community.general.json\_query in vars/main.yml with an Ansible role and ansible-core 2.19.x

**URL:** <https://forum.ansible.com/t/using-community-general-json-query-in-vars-main-yml-with-an-ansible-role-and-ansible-core-2-19-x/44202>\
**Category:** Get Help\
**Tags:** jmespath\
**Created:** [August 4, 2025, 6:36am UTC](https://forum.ansible.com/t/using-community-general-json-query-in-vars-main-yml-with-an-ansible-role-and-ansible-core-2-19-x/44202 "2025-08-04T06:36:13Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [August 4, 2025, 6:36am UTC](https://forum.ansible.com/t/using-community-general-json-query-in-vars-main-yml-with-an-ansible-role-and-ansible-core-2-19-x/44202/1 "2025-08-04T06:36:13Z")

</div>

I have a [Ansible PHP role](https://git.coop/roles/php) which uses [json\_query](https://docs.ansible.com/ansible/latest/collections/community/general/docsite/filter_guide_selecting_json_data.html) in the `vars/main.yml` file.

With `ansible-core` prior to 2.19.x this has been fine but with 2.19.x it doesn’t work, I have created a [minimal test case](https://git.coop/webarch/test) to demonstrate this.

In the test case there is a [roles/test/defaults/main.yml](https://git.coop/webarch/test/-/blob/main/roles/test/defaults/main.yml) which defines a list of PHP packages like this:

```yaml
test_php_versions:
  - name: PHP 8.4 packages
    version: "8.4"
    state: present
    pkg_absent:
      - libapache2-mod-php8.4
      - php8.4-phpdbg
    pkg_present:
      - php8.4-apcu
      - php8.4-bcmath
      - php8.4-bz2
      - php8.4-cli
      - php8.4-common
      - php8.4-curl
      - php8.4-gd
      - php8.4-gmp
      - php8.4-fpm
      - php8.4-imagick
      - php8.4-imap
      - php8.4-intl
      - php8.4-ldap
      - php8.4-mbstring
      - php8.4-mysql
      - php8.4-opcache
      - php8.4-readline
      - php8.4-redis
      - php8.4-sqlite3
      - php8.4-soap
      - php8.4-uploadprogress
      - php8.4-xml
      - php8.4-xmlrpc
      - php8.4-xsl
      - php8.4-zip
      - php-pear
  - name: PHP 8.3 packages
    version: "8.3"
    state: present
    pkg_absent:
      - libapache2-mod-php8.3
      - php8.3-phpdbg
    pkg_present:
      - php8.3-apcu
      - php8.3-bcmath
      - php8.3-bz2
      - php8.3-cli
      - php8.3-common
      - php8.3-curl
      - php8.3-gd
      - php8.3-gmp
      - php8.3-fpm
      - php8.3-imagick
      - php8.3-imap
      - php8.3-intl
      - php8.3-ldap
      - php8.3-mbstring
      - php8.3-mysql
      - php8.3-opcache
      - php8.3-readline
      - php8.3-redis
      - php8.3-sqlite3
      - php8.3-soap
      - php8.3-uploadprogress
      - php8.3-xml
      - php8.3-xmlrpc
      - php8.3-xsl
      - php8.3-zip
      - php-pear

```

And a [roles/test/vars/main.yml](https://git.coop/webarch/test/-/blob/main/roles/test/vars/main.yml) which contains:

```yaml
test_php_ver_present: "{{ test_php_versions | community.general.json_query('sort([?state==`present`].version)') }}"

```

Running the role fails with the task in [roles/test/tasks/main.yml](https://git.coop/webarch/test/-/blob/main/roles/test/tasks/main.yml) to print the `test_php_ver_present` variable:

```yaml
- name: PHP versions that are set to be present
  ansible.builtin.debug:
    var: test_php_ver_present

```

The error is:

```auto
TASK [test : PHP versions that are set to be present] ********************************************************************************
[ERROR]: Task failed: Error while resolving `var` expression: The filter plugin 'community.general.json_query' failed: JMESPathError in json_query filter plugin:
In function sort(), invalid type for value: 8.4, expected one of: ['array-string', 'array-number'], received: "_AnsibleTaggedStr"

Task failed.
Origin: /test/roles/test/tasks/main.yml:5:7

3 block:
4
5 - name: PHP versions that are set to be present
        ^ column 7

<<< caused by >>>

Error while resolving `var` expression: The filter plugin 'community.general.json_query' failed.
Origin: /test/roles/test/tasks/main.yml:7:14

5 - name: PHP versions that are set to be present
6 ansible.builtin.debug:
7 var: test_php_ver_present
               ^ column 14

<<< caused by >>>

JMESPathError in json_query filter plugin:
In function sort(), invalid type for value: 8.4, expected one of: ['array-string', 'array-number'], received: "_AnsibleTaggedStr"

fatal: [localhost]: FAILED! => 
    msg: |-
        Task failed: Error while resolving `var` expression: The filter plugin 'community.general.json_query' failed: JMESPathError in json_query filter plugin:
        In function sort(), invalid type for value: 8.4, expected one of: ['array-string', 'array-number'], received: "_AnsibleTaggedStr"

```

This can also be tested using [yq](https://github.com/mikefarah/yq) and [jp](https://github.com/jmespath/jp):

```bash
cat roles/test/defaults/main.yml | yq -o=json -P | jp "sort(test_php_versions[?state=='present'].version)"
[
  "7.4",
  "8.0",
  "8.1",
  "8.2",
  "8.3",
  "8.4"
]

```

Is this error behaviour correct or is this a bug?

If it is correct how could I update the role to work with `ansible-core` 2.19.x?

1. Replace the use of JMESPath via `community.general.json_query` with Jinja2 in `/vars/main.yml`?
2. Move all the setting of variables in `/vars/main.yml` to tasks?
3. Something else?

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [August 4, 2025, 9:04am UTC](https://forum.ansible.com/t/using-community-general-json-query-in-vars-main-yml-with-an-ansible-role-and-ansible-core-2-19-x/44202/2 "2025-08-04T09:04:35Z")

</div>

FWIW.

- Do not use the `json_query` sort

```yaml
test_php_ver_presen3: "{{ test_php_versions |
                          community.general.json_query('[?state==`present`].version') |
                          sort }}"

```

- You don’t need `json_query`

```yaml
test_php_ver_presen2: "{{ test_php_versions |
                          selectattr('state', '==', 'present') |
                          map(attribute='version') |
                          sort }}"

```

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [August 4, 2025, 9:18am UTC](https://forum.ansible.com/t/using-community-general-json-query-in-vars-main-yml-with-an-ansible-role-and-ansible-core-2-19-x/44202/3 "2025-08-04T09:18:18Z")

</div>

Thanks @vbotka you are right, the JMESPath `sort` does appear to be the cause, is this a bug or a feature?

You are also right that it can be done using Jinja2, I find JMESPath easier so tend to use it a lot…

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [August 4, 2025, 9:27am UTC](https://forum.ansible.com/t/using-community-general-json-query-in-vars-main-yml-with-an-ansible-role-and-ansible-core-2-19-x/44202/4 "2025-08-04T09:27:52Z")

</div>

I don’t know. I also use JMESPath a lot. It works for me in 2.18 too. (I don’t have 2.19). You might want to look at the result of `[?state==`present`].version` in 2.19. How could this be `_AnsibleTaggedStr` ?

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [August 4, 2025, 9:32am UTC](https://forum.ansible.com/t/using-community-general-json-query-in-vars-main-yml-with-an-ansible-role-and-ansible-core-2-19-x/44202/5 "2025-08-04T09:32:30Z")

</div>

Thanks @vbotka I don’t understand why 2.19 thinks a list is a `_AnsibleTaggedStr`.

I have also just come across another issue with a JMESPath query that doesn’t use sort that also generates an error, I’ll distil it to a minimal test case and post it, perhaps there is a general issue with the [json\_query filter](https://docs.ansible.com/ansible/latest/collections/community/general/json_query_filter.html) and `ansible-core` 2.19.x?

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [August 4, 2025, 11:08am UTC](https://forum.ansible.com/t/using-community-general-json-query-in-vars-main-yml-with-an-ansible-role-and-ansible-core-2-19-x/44202/6 "2025-08-04T11:08:43Z")

</div>

I’ve another case where `json_query` fails with Ansible 2.19.0 but is fine with earlier versions, given a list like this:

```yaml
test_php_fpm_pool_file_contents:
  - www74:
      group: www-data
      listen: /run/php/php7.4-fpm.sock
      listen.group: www-data
      listen.mode: '0666'
      listen.owner: www-data
      pm: dynamic
      pm.max_children: '4'
      pm.max_requests: '5000'
      pm.max_spare_servers: '2'
      pm.min_spare_servers: '2'
      pm.start_servers: '2'
      pm.status_path: /fpm-www74-status
      user: www-data
  - www81:
      group: www-data
      listen: /run/php/php8.1-fpm.sock
      listen.group: www-data
      listen.mode: '0666'
      listen.owner: www-data
      pm: dynamic
      pm.max_children: '4'
      pm.max_requests: '5000'
      pm.max_spare_servers: '2'
      pm.min_spare_servers: '2'
      pm.start_servers: '2'
      pm.status_path: /fpm-www81-status
      user: www-data

```

The following task to set a fact:

```yaml
    - name: Set a fact based on a list of PHP-FPM socket paths and pool names
      ansible.builtin.set_fact:
        test_php_fpm_pool_names_and_socket_paths: "{{ test_php_fpm_pool_file_contents | community.general.json_query('[].{name: keys(@)|[0],socket: *.listen|[0]}') }}"

```

Fails with:

```auto
TASK [test : Set a fact based on a list of PHP-FPM socket paths and pool names] ******************************************************
[ERROR]: Task failed: Finalization of task args for 'ansible.builtin.set_fact' failed: Error while resolving value for 'test_php_fpm_pool_names_and_socket_paths': The filter plugin 'community.general.json_query' failed: JMESPathError in json_query filter plugin:
In function keys(), invalid type for value: {'www74': {'group': 'www-data', 'listen': '/run/php/php7.4-fpm.sock', 'listen.group': 'www-data', 'listen.mode': '0666', 'listen.owner': 'www-data', 'pm': 'dynamic', 'pm.max_children': '4', 'pm.max_requests': '5000', 'pm.max_spare_servers': '2', 'pm.min_spare_servers': '2', 'pm.start_servers': '2', 'pm.status_path': '/fpm-www74-status', 'user': 'www-data'}}, expected one of: ['object'], received: "unknown"

Task failed.
Origin: /test/roles/test/tasks/main.yml:29:7

27 var: test_php_fpm_pool_file_contents
28
29 - name: Set a fact based on a list of PHP-FPM socket paths and pool names
        ¦^ column 7

<<< caused by >>>

Finalization of task args for 'ansible.builtin.set_fact' failed.
Origin: /test/roles/test/tasks/main.yml:30:7

28
29 - name: Set a fact based on a list of PHP-FPM socket paths and pool names
30 ansible.builtin.set_fact:
        ¦^ column 7

<<< caused by >>>

Error while resolving value for 'test_php_fpm_pool_names_and_socket_paths': The filter plugin 'community.general.json_query' failed.
Origin: /test/roles/test/tasks/main.yml:31:51

29 - name: Set a fact based on a list of PHP-FPM socket paths and pool names
30 ansible.builtin.set_fact:
31 test_php_fpm_pool_names_and_socket_paths: "{{ test_php_fpm_pool_file_contents | community.general.json_que...
        ¦ ¦ ¦ ¦ ¦ ¦ ^ column 51

<<< caused by >>>

JMESPathError in json_query filter plugin:
In function keys(), invalid type for value: {'www74': {'group': 'www-data', 'listen': '/run/php/php7.4-fpm.sock', 'listen.group': 'www-data', 'listen.mode': '0666', 'listen.owner': 'www-data', 'pm': 'dynamic', 'pm.max_children': '4', 'pm.max_requests': '5000', 'pm.max_spare_servers': '2', 'pm.min_spare_servers': '2', 'pm.start_servers': '2', 'pm.status_path': '/fpm-www74-status', 'user': 'www-data'}}, expected one of: ['object'], received: "unknown"

fatal: [localhost]: FAILED! =>
    changed: false
    msg: |-
        Task failed: Finalization of task args for 'ansible.builtin.set_fact' failed: Error while resolving value for 'test_php_fpm_pool_names_and_socket_paths': The filter plugin 'community.general.json_query' failed: JMESPathError in json_query filter plugin:
        In function keys(), invalid type for value: {'www74': {'group': 'www-data', 'listen': '/run/php/php7.4-fpm.sock', 'listen.group': 'www-data', 'listen.mode': '0666', 'listen.owner': 'www-data', 'pm': 'dynamic', 'pm.max_children': '4', 'pm.max_requests': '5000', 'pm.max_spare_servers': '2', 'pm.min_spare_servers': '2', 'pm.start_servers': '2', 'pm.status_path': '/fpm-www74-status', 'user': 'www-data'}}, expected one of: ['object'], received: "unknown"

```

With an older version of `ansible-core` the task works:

```auto
TASK [test : Set a fact based on a list of PHP-FPM socket paths and pool names] ******************************************************
ok: [localhost] => 
    ansible_facts:
        test_php_fpm_pool_names_and_socket_paths:
        - name: www74
            socket: /run/php/php7.4-fpm.sock
        - name: www81
            socket: /run/php/php8.1-fpm.sock
    changed: false

```

I have added a task for the above to the demo repo and a couple of others, it does looks to me that there is an issue with `community.general.json_query` and `ansible-core` 2.19.0?

**EDIT:** I have [opened an issue at GitHub](https://github.com/ansible-collections/community.general/issues/10575) for this problem.

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [August 4, 2025, 1:58pm UTC](https://forum.ansible.com/t/using-community-general-json-query-in-vars-main-yml-with-an-ansible-role-and-ansible-core-2-19-x/44202/7 "2025-08-04T13:58:57Z")

</div>

> <https://github.com/ansible-collections/community.general/pull/10539>
>
> \##### SUMMARY
> Fixes https://github.com/ansible/ansible/issues/85600.
> 
> \##### I…SSUE TYPE
> \- Bugfix Pull Request
> 
> \##### COMPONENT NAME
> json\_query filter

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [August 4, 2025, 2:04pm UTC](https://forum.ansible.com/t/using-community-general-json-query-in-vars-main-yml-with-an-ansible-role-and-ansible-core-2-19-x/44202/8 "2025-08-04T14:04:08Z")

</div>

Thanks @bcoca I’m now looking forward to the next release of `community.general` so I can easily test this fix — after manually editing `json_query.py` locally to apply [this fix](https://github.com/ansible-collections/community.general/commit/3de073fb6f3c0558b57b6c8d2d58713987ebba27) I still get the same error ☹ , I also tried clobbering the local file like this:

```bash
wget https://raw.githubusercontent.com/felixfontein/community.general/a52c88a6bb72e3d06701e69bd842ced8f66ccba4/plugins/filter/json_query.py -O ~/.local./pipx/venvs/ansible/lib/python3.11/site-packages/ansible_collections/community/general/plugins/filter/json_query.py

```

And I still have the same error, @felixfontein any suggestions?

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [August 4, 2025, 6:26pm UTC](https://forum.ansible.com/t/using-community-general-json-query-in-vars-main-yml-with-an-ansible-role-and-ansible-core-2-19-x/44202/9 "2025-08-04T18:26:05Z")

</div>

I wasn’t able to reproduce this with the fix (only without it). community.general 11.1.2 just got released with the fix, can you try it from there? Maybe that works better.

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [August 5, 2025, 2:14pm UTC](https://forum.ansible.com/t/using-community-general-json-query-in-vars-main-yml-with-an-ansible-role-and-ansible-core-2-19-x/44202/10 "2025-08-05T14:14:28Z")

</div>

Thanks @felixfontein `community.general` `11.1.2` does fix it for me.
