When ... "ansible_facts['distribution_major_version'] <= '9')" evaluates not as expected

Hi,

RedHat 10 was released in May, and to address subtle differences between RedHat 9 and 10 I thought I could simply use the ‘lte’ construct for RedHat 9:-

     when: (ansible_facts['distribution'] == 'RedHat' and ansible_facts['distribution_major_version'] <= '9') or
           (ansible_facts['distribution'] == 'OracleLinux' and ansible_facts['distribution_major_version'] <= '9')


and for RedHat 10 use the following to differentiate:-
 when: (ansible_facts['distribution'] == 'RedHat' and ansible_facts['distribution_major_version'] == '10') or
       (ansible_facts['distribution'] == 'OracleLinux' and ansible_facts['distribution_major_version'] == '10')

however, the less than or equal to 9 is evaluating to true for my RedHat 10 deployments, so still tries to run the wrong code and consequently fails.

I wonder is this because the first character ‘1’ of ‘10’ is being evaluated as less than 9, whereas I expect this to evaluate to false. Probably wrong on that, but I’ve tried int without improving things.

Any ideas?

What the current code does is lexicographical comparison. You want to operate on integers, not strings:

ansible_facts['distribution_major_version'] | int <= 9
2 Likes

thanks @mkrizek [Martin]

I thought I tried to int already, but evidently didn’t have it quite right.

i can confirm your solution worked,

thanks for the quick response,

best regards,

In fact versions should not be checked like that, but better with the version filter

when: ansible_facts['distribution']  | version('9', ' <=')

OK thanks , Ton, I’ll give that a go, ta

version is a test (plugin), not a filter so the correct form would be:

ansible_facts['distribution_version'] is version('9', '<=')

In general when using the version test, make sure you have the correct versioning scheme (if available) set for your use case: ansible.builtin.version test – compare version strings — Ansible Community Documentation.

Though I don’t see a problem with checking against distribution_major_version directly if the major version is the only relevant part.

1 Like

Yes, you are right.

Just checked in a tiny playbook

thanks people, yes this later amendment also works, so also marked as solved, thanks again.