Interview Prep/Ansible/Jinja2, Conditionals and Loops

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.

All Ansible questions Roadmap: Ansible

More on Jinja2, Conditionals and Loops