I need the simple thing - if variable is false or empty string, then evaluate to false. Otherwise evaluate to true.
I tried bool(var) but I'm getting:
UndefinedError: 'bool' is undefined
Then I tried var | bool but even though var is non-empty, that evaluates to false. How to make that condition work??
I've found a possible solution in ruby style:
when: not not var
But it's rather ugly. Forgot to say that without not not the var evaluates to a string so ansible errors out. I hope for a better answer so please add another answer if you have.
Your main problem is that anything other than "true" is going to get evaluated as false when using var | bool.
If you are always providing it as a string (eg. var: '' or var: 'false') then you can just check for string equality:
when: condition == 'false' or condition == ''
Optionally adding the boolean check as well if you have that possibility:
when: not condition or condition == 'false' or condition == ''
Alternatively you could default to a boolean and optionally override. For example you might have a role that has a conditional task:
- name: echo foobar
shell: echo 'foobar'
when: echo_foo
echo_foo: false
But then we might override this at a group or hosts vars level:
echo_foo: true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With