简体   繁体   中英

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

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, '>')"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM