New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unexpected behavior in when: conditions #33306
Comments
Maybe I'm misunderstanding something and it's supposed to work this way, but it is counterintuitive and difficult to debug. |
reproducer: ---
- hosts: localhost
gather_facts: false
vars:
bar: []
tasks:
- set_fact:
foo: []
- debug:
msg: "foo (empty list fact)"
when: foo
- debug:
msg: "bar (empty list var)"
when: bar
- debug:
msg: "(empty list inline)"
when: []
- debug:
msg: "baz (empty list in a tuple inline)"
when: ([]) result:
|
I wonder what Ansible's specification says about these cases. Is the behaviour wel-defined or unspecified? |
I believe this is a side affect of the fact that a
is equivalent to:
So effectively In
I'm not sure what should be done here. As far as I know this logic has always been present. |
A list with one element evaluates to that element. |
I'm not necessarily saying it makes sense, but it evaluates to the same thing as no condition, because a list of no conditions was provided. And historically, no condition is treated as |
An empty list should trigger an error, or at least a warning, if an explicit cast to bool is not used. As a bool, it's false; as a list of conditions, it makes no sense and the current behavior is to return true, when it's a literal (unless it's wrapped in How many users, do you think, understand this behavior ? |
In this major version, the warning seems like the only viable option. |
Let's take a step back and talk about the use case. What use case do you have that you need to explicitly specify The more I think about it, this seems like an extreme edge case, as I can't think of a time when a person would explicitly use needs_info |
Well, I wasn't fuzzing ansible. I ran into this in the course of regular usage. I was debugging the conditions in a few tasks and figured I needed to simplify them to the point where I could be absolutely certain I understood what was going on and then build them back up towards what I wanted. That's how I ended up with a I don't see how adding a warning could be anything but good in this situation, especially that there's already a warning, in the same function, about using jinja templates (even though it's a legitimate use case). And the new warning could be trivially muted by an explicit cast to bool, while the jinja warning is impossible to get rid of (afaik). The jinja warning is there because the user might be doing something that will not work the way they expect, which is exactly what this issue is about. But I think this discussion is moving away from the more significant point, which is that this implementation is simply wrong. It makes no mathematical sense. But hey, if you want to sweep the problem under the carpet and pretend it doesn't exist - go ahead. I've exhausted the energy I could/would devote to this. |
We decided in todays core meeting, to change the default to prevent @bcoca will be providing a pull request to address. |
After further discussion and attempting to fix this, we have decided to not fix this. This is only triggered when setting For further help with the issue, please reach out on IRC or the mailing list: * IRC: #ansible on irc.freenode.net |
ISSUE TYPE
Bug Report
COMPONENT NAME
core
ANSIBLE VERSION
2.5.0.34173.3d63ecb6f3-1
CONFIGURATION
DEFAULT_STDOUT_CALLBACK(/etc/ansible/ansible.cfg) = debug
OS / ENVIRONMENT
N/A
SUMMARY
Unexpected behavior in
when
conditions.STEPS TO REPRODUCE
EXPECTED RESULTS
I expected the same behavior in each case - skip/skip/skip.
ACTUAL RESULTS
Got different behaviors - skip/run/skip.
The text was updated successfully, but these errors were encountered: