Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare two version numbers with version in ansible

I am writing a playbook that only should run when a new package version of a software is available. Both version numbers are custom facts I set with "set_fact"

Ansible let's you compare version numbers, so I tried it like this:

- name: compare versions
  debug:
    msg: "The version {{ new_version }} is newer than the old version {{ old_version }}"
  when: "{{ new_version is version('{{ old_version }}', '>', strict=True }}"

Ansible throws an error that the version number is invalid. When I set "old_version" to a fixed version number it works as expected. Is it possible to compare two facts with "version"? I already tried different approaches with double-quotes etc which leads to syntax errors in ansible.

Any ideas?

Thanks

like image 724
diggnsaegg Avatar asked May 15 '26 19:05

diggnsaegg


1 Answers

It could be use of Jinja delimiters {{ in a conditional. In a when: condition you don't need these delimiters to interpolate.

Example below works:

vars:
  new_version: 14
  old_version: 12

tasks:
- debug:
    msg:  'The version {{ new_version }} is newer than the old version {{ old_version }}'
  when: "new_version is version(old_version, '>')"
like image 53
seshadri_c Avatar answered May 18 '26 09:05

seshadri_c