# when condition based on software version

**URL:** <https://forum.ansible.com/t/when-condition-based-on-software-version/36262>\
**Category:** Ansible Project\
**Created:** [January 29, 2022, 1:05pm UTC](https://forum.ansible.com/t/when-condition-based-on-software-version/36262 "2022-01-29T13:05:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Veliss](https://avatars.discourse-cdn.com/v4/letter/j/8e8cbc/32.png) [@John\_Veliss](https://forum.ansible.com/u/John_Veliss)\
**Post date:** [January 29, 2022, 1:05pm UTC](https://forum.ansible.com/t/when-condition-based-on-software-version/36262/1 "2022-01-29T13:05:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![John\_Veliss](https://avatars.discourse-cdn.com/v4/letter/j/8e8cbc/32.png) [@John\_Veliss](https://forum.ansible.com/u/John_Veliss)\
**Post date:** [January 31, 2022, 12:25am UTC](https://forum.ansible.com/t/when-condition-based-on-software-version/36262/2 "2022-01-31T00:25:32Z")

</div>

would anyone have any suggestions on this

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [January 31, 2022, 4:19am UTC](https://forum.ansible.com/t/when-condition-based-on-software-version/36262/3 "2022-01-31T04:19:18Z")

</div>

> 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.

&nbsp;&nbsp;&nbsp;&nbsp;- package\_facts:  
&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_version: "{{ ansible\_facts.packages[my\_pkg].0.version|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;default('undef') }}"  
&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;found: "{{ my\_pkg in ansible\_facts.packages and  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_version is version(my\_version, '\>') }}"

&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ my\_pkg }}: {{ \_version }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: found

&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ my\_pkg }} NOT found"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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

&nbsp;&nbsp;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

&nbsp;&nbsp;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

&nbsp;&nbsp;msg: yamllint NOT found
