As Ansible is widely used as an automated provisioning and deployment tool, it is quite often required to compare version numbers of different software components.
For example, you may need to check the current version of an application and compare it with the latest one to take a decision if update is required.
To compare versions in Ansible we can use the version test, and in this note i will show the examples of how to use it.
Cool Tip: How to check if a file exists in Ansible! Read more →
Compare Versions in Ansible
The variables in Ansible can be compared using the version
test as follows:
# roles/compare_versions/vars/main.yml --- current_version: "1.58.2" latest_version: "1.60.2" update_is_required: "{{ current_version is version(latest_version, '<') }}"
The update_is_required
in the example above will be set to True
or False
depending on a versions comparison result.
The version
test accepts the following operators:
<, lt, <=, le, >, gt, >=, ge, ==, =, eq, !=, <>, ne
To compare versions directly in Ansible tasks, the version
test can be used as in the examples below:
- name: "Set version variables" set_fact: current_version: "1.58.2" latest_version: "1.60.2" - debug: msg: "{{ current_version }} is newer than {{ latest_version }}" when: current_version is version(latest_version, '>') - debug: msg: "{{ current_version }} is newer than or equal to {{ latest_version }}" when: current_version is version(latest_version, '>=') - debug: msg: "{{ current_version }} is equal to {{ latest_version }}" when: current_version is version(latest_version, '==') - debug: msg: "{{ current_version }} is not equal to {{ latest_version }}" when: current_version is version(latest_version, '!=') - debug: msg: "{{ current_version }} is older than {{ latest_version }}" when: current_version is version(latest_version, '<') - debug: msg: "{{ current_version }} is older than or equal to {{ latest_version }}" when: current_version is version(latest_version, '<=')
Cool Tip: How to comment out lines in a file using Ansible! Read more →
Sample output:
TASK [compare_versions : Set version variables] ************************** ok: [127.0.0.1] => { "ansible_facts": { "current_version": "1.58.2", "latest_version": "1.60.2" }, "changed": false } TASK [compare_versions : debug] ****************************************** skipping: [127.0.0.1] => {} TASK [compare_versions : debug] ****************************************** skipping: [127.0.0.1] => {} TASK [compare_versions : debug] ****************************************** skipping: [127.0.0.1] => {} TASK [compare_versions : debug] ****************************************** ok: [127.0.0.1] => { "msg": "1.58.2 is not equal to 1.60.2" } TASK [compare_versions : debug] ****************************************** ok: [127.0.0.1] => { "msg": "1.58.2 is older than 1.60.2" } TASK [compare_versions : debug] ****************************************** ok: [127.0.0.1] => { "msg": "1.58.2 is older than or equal to 1.60.2" }
Now try it with:
set_fact:
current_version: “1.58.2”
latest_version: “1.100.2”
I’ve been trying to find a version() fix for this all afternoon. even the most recent ansible 2.12 can’t do this with version() no matter how I format the inputs and what version_type I ask it to use.