# import\_tasks: vs include: when using when: conditional

**URL:** <https://forum.ansible.com/t/import-tasks-vs-include-when-using-when-conditional/30958>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [August 5, 2019, 9:21pm UTC](https://forum.ansible.com/t/import-tasks-vs-include-when-using-when-conditional/30958 "2019-08-05T21:21:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bob](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bob/32/9260_2.png) [@bob](https://forum.ansible.com/u/bob)\
**Post date:** [August 5, 2019, 9:21pm UTC](https://forum.ansible.com/t/import-tasks-vs-include-when-using-when-conditional/30958/1 "2019-08-05T21:21:09Z")

</div>

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](http://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](http://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?

---

<div class="post-metadata">

**Author:** ![Sebastian\_Meyer](https://avatars.discourse-cdn.com/v4/letter/s/779978/32.png) [@Sebastian\_Meyer](https://forum.ansible.com/u/Sebastian_Meyer)\
**Post date:** [August 5, 2019, 10:50pm UTC](https://forum.ansible.com/t/import-tasks-vs-include-when-using-when-conditional/30958/2 "2019-08-05T22:50:49Z")

</div>

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/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_tasks.py#L70)  
[https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/utilities/logic/include.py#L79](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/utilities/logic/include.py#L79)

Playbook:

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [August 6, 2019, 2:25pm UTC](https://forum.ansible.com/t/import-tasks-vs-include-when-using-when-conditional/30958/3 "2019-08-06T14:25:31Z")

</div>

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.
