How do you compare software versions correctly in a condition?
IntermediateConceptJinja2, Conditionals and Loops
Answer
Use the version test, not string comparison. Strings compare character by character, so '9.10' < '9.9'.
when: ansible_facts['distribution_version'] is version('22.04', '>=')
when: app_version is version('2.0.0', 'lt', strict=true)
Operators include <, <=, ==, >=, >, !=. strict=true enforces semantic versioning and fails on malformed input rather than guessing.
More on Jinja2, Conditionals and Loops
What role does Jinja2 play in Ansible?BeginnerWhat are the three Jinja2 delimiters?BeginnerShow a template with a loop and a conditional, and how it is deployed.BeginnerWhich filters do you use every day?BeginnerHow do you transform lists and dicts with filters (`map`, `select`, `selectattr`, `unique`, `flatten`)?IntermediateHow does `when` work and which operators can you use?Beginner