I’m using a conditional include statement to call a task file which has a task that uses a “with_dict” loop. When the include statement conditional fails, I’d like the task in “option.yml” to be skipped as the task would result in an error anyway.
When this is run, I get an error “with_dict expects a dict” since there is no dictionary “groups[{{ grps[2] }}]”. Why does Ansible try to evaluate the task at all since it should be skipped because of the include conditional?
Ideally, there would be a way to use a “with_items” loop with an include statement, but I am aware that that is intentionally not allowed.
(Note: I know this is a terrible example of how/when to use an include statement, but it shows the gist of how the error occurs. My actual usage is much more complicated.)
I just found https://github.com/ansible/ansible/issues/8546, which explains clearly that when is for each item in the loop. (Which I now recall reading in the docs). So this is clearly not a bug.
And the workaround in that issues works just fine for my case.
But related, and where this started for me, was running into this via a conditional include:
main.yml
{ include: service_def.yml, when: consul_services is defined }
service_def.yml
name: Create service definitions
template: > …
with_dict: consul_services
I did not expect service_def.yml to even be loaded in this case, but it clearly does. What exactly is happening with task includes with a ‘when: false’? Does each task in the include get a hidden ‘when: false’ added to it? That would explain the behavior, but its definately not the behavior of least surprise to me.