简体   繁体   中英

How to compare version numbers of software using Ansible?

Hey I am trying to check the version on installed software on my VMs with Ansible.

Unfortunately I cant get the conditionals right.

Via the ansible_facts.packages I get the version of the software but I am not sure about the comparing.

- hosts:
    - os_linux

  vars:
    softwareVersion: "4.8.0-49"

  tasks:

  - name: Gather the package facts
    ansible.builtin.package_facts:
      manager: auto

  - name: Check version of SoftwareX
    ansible.builtin.debug:
      msg: "OLD SoftwareX version: {{ ansible_facts.packages['SoftwareX'][0]['version'] }} found"
    when: "ansible_facts.packages['SoftwareX'][0]['version'] <= {{ softwareVersion }}"  

I am not sure about the comparing.

when: "ansible_facts.packages['SoftwareX'][0]['version'] <= {{ softwareVersion }}"

Since according When should I use {{ }} ?

when: ... are always templated and you should avoid adding {{ }}.

you'll need to remove first the curly brackets from softwareVersion .

Then, since you are comparing strings which contains a version number in order, you need to address this in a different way. Means, you can't use plain a mathematical operation but instead a specific filter for.

A minimal test playbook

---
- hosts: localhost
  become: false
  gather_facts: false

  vars:

    installedVersion: "4.7.0-48"
    softwareVersion: "4.8.0-49"

  tasks:

  - name: Show hint
    debug:
      msg: "OLD"
    when: installedVersion is version(softwareVersion, '<=')

will result into an output of

TASK [Show hint] ******
ok: [localhost] =>
  msg: OLD

Further Documentaion

Note: In 2.5 version_compare was renamed to version

To compare a version number, such as checking if the ansible_facts['distribution_version'] version is greater than or equal to '12.04', you can use the version test.

Similar Q&A

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