Before I file a ticket, I just want to make sure I'm not doing
anything wrong. BTW, this is a simplified example to reproduce the
problem, not what I was actually trying to do. I have this simple
playbook test.yml:
What is happening is that you are effectively setting foo to the string “false”
What you likely want to do is:
when: not foo|bool
Is there a way to use set_fact to force it to use a real boolean
instead of the string? In the real playbook other tasks and includes
further down don't care where the var came from and I'd rather not
have to alter every boolean to run things through the bool filter just
in case the var came from set_fact.
I tried
- set_fact: foo={{ false }}
and
- set_fact: foo={{ false | bool }}
But neither of those work.
If you use the pure YAML complex args way of invoking a module, you can make it an actual boolean:
- set_fact:
foo: False
But due to the fact that you cannot do that with jinja2 variable expansion and such, I’d recommend just sticking to using the |bool filter at evaluation time. It’s more reliable and safer that way.
“But due to the fact that you cannot do that with jinja2 variable expansion and such,”
- set_fact:
foo: “{{ x }}”
The system will preserve complex dict/list types, I haven’t tried it with boolean types recently.
Thanks, this works.
I was under the impression that the 2 syntax variants (yaml vs
key-value) worked the same. Is this something that should be fixed or
just documented as a difference?
Also, isn't it weird the with the variable foo set to string "false"
both "when: foo" and "when: not foo" evaluate to false?
“Is this something that should be fixed or
just documented as a difference?”
Nope. It’s fine, because the system has no hint to decide you asked for anything other than a string.
“Also, isn’t it weird the with the variable foo set to string “false”
both “when: foo” and “when: not foo” evaluate to false?”
This is a programming language concept.
No, as the string ‘false’ in Python or any prograis not the same as False.
there is a difference between these two:
vars:
x: “false”
x: false
The above quoted one is a string whose value is the word “false” and it’s important the word doesn’t get auto-converted into a boolean.
Yes, I realize the difference between a string and a boolean. But I've
never seen a system where "foo" and "not foo" evaluate to the same
thing.
“both “when: foo” and “when: not foo” evaluate to false?”
Don’t recall it ever working that way.
Can you share a playbook that reproduces this?
Sure: test.yml
The inconsistent booleanification is definitely not something I’d want to see here.
Can you please file a bug on github for this one?
Submitted bug: https://github.com/ansible/ansible/issues/8629
Thanks