conditional include of task file

Is there a way to include a task file base on a condition?

Something like:

- include: some_task_file
   when: do_that_work == True

What you have above applies that condition to everything in the file. It does not conditionally include a file, and we will NOT do that for architectural reasons.

The better way to do it is use the ‘group_by’ module and start a new play against that dynamically created subgroup, as that will produce more elegant output.

That's a cool idea. So you're suggesting something like:

- name: "define a subgroup based on a variable"
  hosts: all
  tasks:
    - group_by: key=has_foo
      when: foo

- name: "run subgroup tasks"
  hosts: has_foo
  tasks:
     - shell: echo "I have foo!"

K

Well, the original example was about grouping on a variables value:

tasks:

  • group_by: key=asdf_{{asdf}}

… later in a following play:

hosts: asdf_True

I haven’t tried it with the when but suspect that would work too.

Well, the original example was about grouping on a variables value:

tasks:

  • group_by: key=asdf_{{asdf}}

… later in a following play:

hosts: asdf_True

I haven’t tried it with the when but suspect that would work too.

It doesn’t work with when (error while evaluating conditional: {% if foo %} True {% else %} False {% endif %})
Ran kahlil’s example on 1.3.4

This works:
when: foo is defined

The above error happens with:
when: foo