I have two variables: a
, b
.
I want to assign a value to a variable c
based on which: a
or b
contains greater numerical value.
This what I tried:
- set_fact:
c: "test1"
when: a <= b
- set_fact:
c: "test2"
when: b <= a
Look like it always sets c
to test1
not test2
.
Using
if-else
expression:Using ternary operator:
Using your own code which is correct (see the notice below)
Either of the above methods requires both variables used in comparison to be of the same type to give sensible results. And for a numerical comparison, they require both values to be integers.
The code from the question:
works properly in two cases:
a
andb
are integersa
andb
are strings containing numerical value with the same number of digitsHowever it produces unexpected result is when: