Does this Ansible feature make sense to anyone at all?

I’m really puzzled by how a certain Ansible feature appears to work, and I can’t figure out why anyone in the world would ever want it to work that way. I hope that I’m just densely mis-interpreting how it works.

It seems that the when: attribute for the include: task doesn’t actually constrain when the specified playbook is included. Rather, the specified playbook is always included, and every task within the playbook is executed, with the original when: clause applied to the task.

This doesn’t make any sense whatsoever. I cannot think of a single use case for this.

On the one hand, if when: evaluates to false, then every task within the playbook will be skipped. If it evaluates to true, every task within the playbook will be executed. That sounds like a really stupid alternative to only including the playbook when when: evaluates to true. In fact, this implementation actually prevents us from conditionally including a playbook only if it hasn’t been included before, which seems like a really useful feature in general, and one of the strategies I was hoping to use in order to compensate for Ansible’s really terrible lack of composable language constructs.

What am I missing?

This is because includes before 2.0 were ‘preprocessing macros’ and did not evaluate the ‘when:’ condition at all, most of the time they did not have the proper data to do so, that is why tasks just inherited the clause. I know this is confusing as they appear to be ‘normal tasks’.

Starting at 2.0, ‘task includes’ are now dynamic and will work as you expected, the one exception is when the ‘when:’ clause fails to ‘execute’, Ansible assumes this is because of missing data so it then appends the clause to the included tasks, which might have the required data in scope.

‘Play includes’ still work as in 1.9. They use the same keyword include but include full plays instead of just task lists.