"10" compares as older than "10.0" with version()

Hi,

My ansible_distribution_version returns "10". This debug fires:

- name: version 10 < 10.0
  ansible.builtin.debug:
    msg:
      - "I think 10 is older than 10.0"
  when: {{ ansible_distribution_version }} is version('10.0', '<')

python 3.9.7, ansible 2.10.14.

Any way around this without processing ansible_distribution_version
to have a trailing ".0" when it has no dots?

Thanks!
Andy

Yes. Compare to the lowest version you want to accept, which is ‘10’. (Also, don’t nest moustaches.)

when: ansible_facts.distribution_version is version(‘10’, ‘<’)

Hello,

I'm not even sure if '10' and '10.0' are actually different in terms
of one being higher or lower than the other.
I'd be interested to know what the rationale is for declaring 10
higher or lower than 10.0 (or the other way around).

BTW in our environment, the OSes where ansible_distribution_version is
defined, it always has one or more 'decimals', for example:

9.4 (debian 9)
10.10 (debian 10)
16.04 (ubuntu)
18.04 (ubuntu)
6.9 (centos6)
6.10 (rhel6)
7.5.1804 (centos7)
7.6 (rhel7)

Instead of trying to make (to me rather arbitrary) comparisons between
10 and 10.0, I'd investigate why ansible_distribution_version for your
OS has no 'decimals' to begin with.

Hello,

Instead of trying to make (to me rather arbitrary) comparisons between
10 and 10.0, I'd investigate why ansible_distribution_version for your
OS has no 'decimals' to begin with.

I think it's because none of the point releases for Debian 10 have
been installed on that host, so it's not received any update to the
base-files package (for /etc/os-release) that would show the point
release (e.g. 10.10).

$ head -3 /etc/os-release
PRETTY_NAME="Debian GNU/Linux 10 (buster)"
NAME="Debian GNU/Linux"
VERSION_ID="10"

But this is besides the point as it is normal for a Debian release
to show as not having decimals for some period of time. So we do have
to be aware what testing ansible_distribution_version against x.0
will do, apparently. Whether that test is newer, older or equality.

Cheers,
Andy

FWIW, the version test is implemented via distutils.version.LooseVersion (by default)

And based on the documentation:

there is no such thing as an invalid version number under this scheme;
the rules for comparison are simple and predictable, but may not always give the results you want (for some definition of “want”).

Due to the implementation details of basically comparing a python tuple, 10 is less than 10.0

LooseVersion(‘10’) < LooseVersion(‘10.0’)
True
(10,) < (10, 0)
True

To get equality, you’ll have to perform some normalization on your versions first, and while there are some ways you can use existing jinja operations to do so, it may require assumptions on your data that could just as easily cause problems in the future.

Hello,

> Instead of trying to make (to me rather arbitrary) comparisons between
> 10 and 10.0, I'd investigate why ansible_distribution_version for your
> OS has no 'decimals' to begin with.

I think it's because none of the point releases for Debian 10 have
been installed on that host, so it's not received any update to the
base-files package (for /etc/os-release) that would show the point
release (e.g. 10.10).

Having now actually checked, this is not the case! That host does
have version 10.3+deb10u10 of base-files (the latest), so I don't
know why its /etc/os-files shows VERSION_ID="10" or even if that is
normal. Does yours have that? The only lsb-related packages
installed are lsb-base and lsb-release.

Sorry, this is going off at a bit of a tangent now, but I am
interested in why your ansible_distribution_version comes back as
10.10 here!

By the way, on a freshly installed Debian 11 (bullseye) host, I also
see:

$ head -3 /etc/os-release
PRETTY_NAME="Debian GNU/Linux 11 (bullseye)"
NAME="Debian GNU/Linux"
VERSION_ID="11"

and that one also returns just "11" for
ansible_distribution_version. So as I say, in certain configurations
on Debian it seems normal to not have a decimal point in
ansible_distribution_version.

Thanks,
Andy