How to use version compare test in selectattr?

141 views
Skip to first unread message

Kai Stian Olstad

unread,
Mar 6, 2023, 9:56:50 AM3/6/23
to ansible...@googlegroups.com
Hi

I'm trying to use version compare in selectattr but get error message
"Version comparison failed: '<' not supported between instances of 'int'
and 'str'
This is the playboot I'm using

- hosts: localhost
gather_facts: no
vars:
my:
- vers: "5.43"
- vers: "4.34"
tasks:
- debug:
msg: |
{{ my.0.vers is version("5", "<") }}
{{ my.1.vers is version("5", "<") }}
- debug:
msg:
{{ my | selectattr('vers', 'version', '"5", "<"' }}



The output I get is:

ansible-playboot test.yml
PLAY [localhost] *****

TASK [debug] *****
OK: [localhost] => {}

MSG:

False
True

TASK [debug] *****
fatal: [localhost]: FAILED! => {}

MSG:

Version comparison failed: '<' not supported between instances of 'int'
and 'str'



Anyone have any idea how to do this?


--
Kai Stian Olstad

Todd Lewis

unread,
Mar 6, 2023, 10:24:44 AM3/6/23
to Ansible Project
That last expression is too quoty (and missing a close parenthesis). Try

{{ my | selectattr('vers', 'version', '5', '<') }}

Kai Stian Olstad

unread,
Mar 6, 2023, 10:30:31 AM3/6/23
to ansible...@googlegroups.com
On 06.03.2023 16:24, Todd Lewis wrote:
> That last expression is too quoty (and missing a close parenthesis).
> Try
>
> {{ my | selectattr('vers', 'version', '5', '<') }}

The missing parenthesis is just a typo, but removing the quotes work,
thanks Todd.


--
Kai Stian Olstad

Vladimir Botka

unread,
Mar 6, 2023, 10:40:10 AM3/6/23
to 'Kai Stian Olstad' via Ansible Project
On Mon, 06 Mar 2023 15:56:32 +0100
"'Kai Stian Olstad' via Ansible Project"
<ansible...@googlegroups.com> wrote:

> - debug:
> msg:
> {{ my | selectattr('vers', 'version', '"5", "<"' }}

This is the correct syntax

- debug:
msg: |
{{ my | selectattr('vers', 'version', '5', '<') }}


--
Vladimir Botka
Reply all
Reply to author
Forward
0 new messages