Which comparison operators can a when condition use, and why does ansible_facts['distribution_version'] == "9.0" compare against a quoted value?
==, !=, <, >, <=, >=, is defined / is not defined, a bare boolean or not, and in for list membership; you quote the value when the fact is a string, because a string never equals a number.
| Operation | Example |
|---|---|
| Equal (string) | ansible_facts['machine'] == "x86_64" |
| Equal (numeric) | max_memory == 512 |
| Less / greater than | min_memory < 128, min_memory > 256 |
| Less / greater or equal | min_memory <= 256, min_memory >= 512 |
| Not equal | min_memory != 512 |
| Variable exists / doesn't | min_memory is defined, min_memory is not defined |
| Boolean true / false | memory_available, not memory_available |
| Value is in a list | ansible_facts['distribution'] in supported_distros |
Types matter. Version facts such as distribution_version and distribution_major_version are strings ("9.0", "9"), so they must be compared with quoted strings. ansible_facts['distribution_major_version'] == 9 compares the string "9" with the number 9 and is always false. Either quote the value or convert the fact (ansible_facts['distribution_major_version'] | int >= 9), which is also the way to do a numeric "at least version 9".
Go deeper:
Conditionals based on Ansible facts (official docs) — examples with distribution and version facts.
Tests: comparing versions (official docs) — the
versiontest for "9.2 is newer than 9.10"-style comparisons.