I have a Ansible PHP role which uses json_query 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 to demonstrate this.
In the test case there is a roles/test/defaults/main.yml which defines a list of PHP packages like this:
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 which contains:
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 to print the test_php_ver_present
variable:
- name: PHP versions that are set to be present
ansible.builtin.debug:
var: test_php_ver_present
The error is:
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 and jp:
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?
- Replace the use of JMESPath via
community.general.json_query
with Jinja2 in/vars/main.yml
? - Move all the setting of variables in
/vars/main.yml
to tasks? - Something else?