import_tasks: vs include: when using when: conditional

Simple tasks, where I want the “Remote git configuration” task to only be run when apache2_configuration == ‘git’

  • debug:
    msg: “DEBUG: {{ apache2_configuration }}”
    tags: apache2-servers

  • name: Remote git configuration
    import_tasks: remote-git.yml
    when: apache2_configuration == ‘git’
    tags: apache2-servers

Running ansible, apache2_configuration is “role” but for some reason the import_tasks: remote-git-yml is being imported?

TASK [apache2-servers : debug] *************************************************
ok: [www.aws.comap.com] => {
“msg”: “DEBUG: role”
}

TASK [apache2-servers : XXXXXXXXX inside remote-git.yml] ***********************

Trying with include:

  • debug:
    msg: “DEBUG: {{ apache2_configuration }}”
    tags: apache2-servers

  • name: Remote git configuration
    include: remote-git.yml
    when: apache2_configuration == “git”
    tags: apache2-servers

TASK [apache2-servers : debug] *************************************************
ok: [www.aws.comap.com] => {
“msg”: “DEBUG: role”
}

TASK [apache2-servers : XXXXXXXXX inside remote-git.yml] ***********************

I do not understand why import_tasks: or include: is importing/including remote-git.yml when apache2_configuration is not equal to git.

Any help?

Hi Bob,

Simple tasks, where I want the "Remote git configuration" task to only be
run when apache2_configuration == 'git'

I do not understand why import_tasks: or include: is importing/including
remote-git.yml when apache2_configuration is not equal to git.

Any help?

So for me the following happens:

When the when condition is false import_tasks still imports the tasks
file, but skips all tasks in it. include_tasks on the other hand is
skipped as a task itself.

include without the _tasks (which you shouldn't use anymore) behaves a
lot like import_tasks in this case. If you want it to behave like
include_tasks, you'll have to set the static: no option to the include task.

If you look at the documentation, it's in the examples:

https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/utilities/logic/import_tasks.py#L58
https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/utilities/logic/include_tasks.py#L70
https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/utilities/logic/include.py#L79

Playbook:

To start, I’ll say, don’t use include any more. It has been replaced by import_tasks and include_tasks. The behavior of include was hard to reason about, and as such we are in the process of deprecating it.

Now as for the differences:

import_tasks is a pre-processor, that is parsed and imported during playbook parsing time. Effectively the result, is that the import is replaced at playbook parsing time with the list of tasks within. Any keyword/argument applied to the import_tasks is effectively merged down or inherited by the individual tasks within.

include_tasks is JIT (Just in Time) processed during task execution. As such, when encountered during normal task execution, any when statement (or other keyword/argument) will affect whether or not it is included at that point. If included, the tasks within do not inherit the keywords/arguments on the include_tasks, to get that behavior you would use apply. If the keywords or arguments, such as when or tags, prevent the include_tasks from running, none of the included tasks wihin will be seen in the playbook execution.