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

92 views
Skip to first unread message

Andy Smith

unread,
Sep 14, 2021, 7:20:59 PM9/14/21
to ansible...@googlegroups.com
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

flowerysong

unread,
Sep 14, 2021, 8:49:44 PM9/14/21
to Ansible Project
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', '<')

Andy Smith

unread,
Sep 15, 2021, 4:11:45 AM9/15/21
to ansible...@googlegroups.com
Hello,

On Tue, Sep 14, 2021 at 05:49:44PM -0700, flowerysong wrote:
> On Tuesday, September 14, 2021 at 7:20:59 PM UTC-4 Andy Smith wrote:
>
> > 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', '<')

> Yes. Compare to the lowest version you want to accept, which is '10'.

Hmm, OK. It's also that 10 won't come out as >= 10.0 so I'll have to
remember everywhere not to compare to versions with .0 on the end.

Cheers,
Andy

Dick Visser

unread,
Sep 15, 2021, 4:52:48 AM9/15/21
to ansible...@googlegroups.com
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.
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/20210915081128.r2l2drg57m7wv4w5%40bitfolk.com.
>


--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

Andy Smith

unread,
Sep 15, 2021, 3:54:56 PM9/15/21
to ansible...@googlegroups.com
Hello,

On Wed, Sep 15, 2021 at 10:52:23AM +0200, Dick Visser wrote:
> 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

Matt Martz

unread,
Sep 15, 2021, 4:07:32 PM9/15/21
to ansible...@googlegroups.com
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.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.

Andy Smith

unread,
Sep 15, 2021, 4:10:44 PM9/15/21
to ansible...@googlegroups.com
Hello,

On Wed, Sep 15, 2021 at 07:54:44PM +0000, Andy Smith wrote:
> On Wed, Sep 15, 2021 at 10:52:23AM +0200, Dick Visser wrote:
> > 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
Reply all
Reply to author
Forward
0 new messages