Ansible in a Minute · Ansible Core ·
Use version to compare version numbers
Short answer
Use the version test in when to compare version strings with operators such as greater-than.
Subscribe for updates
Get an email whenever we publish new guides and blog posts. Unsubscribe anytime.
By subscribing you agree we may email you about Ansible in a Minute and related Level Up Labs updates. We store your address in the same lead sheet as our contact form.
Request
print a message when my_version is higher than 1.0.0
Example
A quick heads-up: Every environment is different. Review and test this example before you run it on systems you care about.
---
- name: Compare a version with the version test
hosts: all
gather_facts: false
vars:
my_version: "1.2.3"
tasks:
- name: Print when my_version is higher than 1.0.0
ansible.builtin.debug:
msg: "my_version is higher than 1.0.0"
when: my_version is version('1.0.0', '>')How it works
The version test compares version strings. Put the expression in when without curly braces. Operators include <, <=, >, >=, ==, and !=.
When to use it
- Branching on package or application versions
- Comparing ansible_facts distribution_version to a minimum release
Requirements
- Ansible: >=2.14
Validation
- Syntax validated
- Not execution-tested
- Last verified 2026-09-16
Notes
For host OS facts use ansible_facts['distribution_version'] is version('8.0', '>='). Optional version_type values include loose, strict, semver, and pep440. The old name version_compare was renamed to version in Ansible 2.5.
Reference
- Comparing versions (Ansible docs)
Built for every Ansible use case in every organization
BacklogZero can generate this kind of automation in your environment, just like it did here.