when condition based on software version

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

would anyone have any suggestions on this

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