Quoting from Conditionals with includes:
When you use a conditional on an include_* statement, the condition is applied only to the include task itself and not to any other tasks within the included file(s).
This works as expected
- include_tasks: set-x.yml
when: x is not defined
No task from the included file will be used
TASK [include_tasks] ************************************************
included: /export/scratch/tmp8/set-x.yml for localhost
The situation is different with the blocks. Quoting from Grouping tasks with blocks:
All tasks in a block inherit directives applied at the block level. ... The directive does not affect the block itself, it is only inherited by the tasks enclosed by a block. For example, a when statement is applied to the tasks within a block, not to the block itself.
If you put include_tasks into a block
- block:
- include_tasks: set-x.yml
when: x is not defined
the condition is applied to all tasks. Also to the included tasks, thus overriding the previous rule that a condition is applied only to the include task itself.
TASK [include_tasks] *************************************************
included: /export/scratch/tmp8/set-x.yml for localhost
TASK [set_fact] ******************************************************
ok: [localhost]
TASK [debug] *********************************************************
skipping: [localhost]
Open an issue if you think this is a bug.