Score:1

Blocks with when and include_tasks

ru flag

I have come across a bit of a strange behavior that i don't fully understand and i have so far not been able to find something useful in the documentation.

When you have a "block:" with a "when" condition, this condition on the block itself seems to make its way into included tasks, why?

Example:

#playbook.yml
- hosts: localhost
  tasks:
    - block:
      - name: Include stuff
        include_tasks: "set-x.yml"
      when: x is not defined
#set-x.yml
- name: Set a fact
  set_fact:
    x: foo

- name: test
  debug:
    var: x

If you run above example the debug statement wont run at all and this is not what i expected. My understanding was that the when condition on the block would only apply to if the block itself should be executed or not. Cleary the condition gets carried over inside the include_tasks file and to my understanding this is not how include is supposed to work.

What am i missing?

Score:3
br flag

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.

user3973227 avatar
ru flag
Well... this is a bit of a strange behavior and it gets more strange because not only to the when: condition from the block make it to the include_tasks statement itself it ALSO makes it in to each statement inside the included file and further included files too. i only found this out because i had a meta: reset_connection triggering a warning two levels down from the block about not supporting a when condition on meta:
br flag
Exactly, this is the point!
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.