when condition based on software version

39 views
Skip to first unread message

John Veliss

unread,
Jan 29, 2022, 8:05:39 AM1/29/22
to Ansible Project
I would like to include some ansible "when" conditions based on if a software package is greater than a value, ie when a package is greater than 8.2, perform a task such as stop a service, uninstall the package, copy a file, install a new version of the package

I have the below which shows the version of a package called "Double" if found

The results appear as this -->  ansible_facts.packages['Double'][0].version: 8.2.2

   - name: Gather the Package facts
      package_facts:
        manager: auto
      tags:
        - dt-check

    - name: "Double Found result"
      debug: var=ansible_facts.packages['Double'][0].version
      when: "'Double' in ansible_facts.packages"
      register: dtversion
      tags:
        - dt-check

    - debug:
        var: dtversion
      tags:
        - dt-check

    - name: "Double Not-found result"
      debug:
        msg: "Double NOT found"
      when: "'Double' not in ansible_facts.packages"
      register: notfound
      tags:
        - dt-check

    - debug:
        var: notfound
      tags:
        - dt-check


Thanks in advance for any ideas that you may suggest to me

John Veliss

unread,
Jan 30, 2022, 7:25:33 PM1/30/22
to Ansible Project
would anyone have any suggestions on this

Vladimir Botka

unread,
Jan 30, 2022, 11:19:32 PM1/30/22
to John Veliss, ansible...@googlegroups.com
On Sat, 29 Jan 2022 05:05:39 -0800 (PST)
John Veliss <vel...@gmail.com> wrote:

> I would like to include some ansible *"when"* conditions based on if a
> software package is greater than a value ...

Set default to 'undef' if the package is missing, e.g.

- package_facts:
- set_fact:
_version: "{{ ansible_facts.packages[my_pkg].0.version|
default('undef') }}"
- set_fact:
found: "{{ my_pkg in ansible_facts.packages and
_version is version(my_version, '>') }}"

- debug:
msg: "{{ my_pkg }}: {{ _version }}"
when: found

- debug:
msg: "{{ my_pkg }} NOT found"
when: not found

Then if the package is installed you get, e.g.
> ansible-playbook test.yml -e my_pkg=yamllint -e my_version=1.20

msg: 'yamllint: 1.20.0-1'

If the package is not installed you get, e.g.
> ansible-playbook test.yml -e my_pkg=yamllintXY -e my_version=1.20

msg: yamllintXY NOT found

If the package is installed but the version is lower you get, e.g.
> ansible-playbook test.yml -e my_pkg=yamllint -e my_version=1.21

msg: yamllint NOT found


--
Vladimir Botka
Reply all
Reply to author
Forward
0 new messages