import_tasks: vs include: when using when: conditional

33 views
Skip to first unread message

Bob Tanner

unread,
Aug 5, 2019, 5:21:09 PM8/5/19
to Ansible Project
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] *************************************************
    "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] *************************************************
    "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?

Sebastian Meyer

unread,
Aug 5, 2019, 6:51:00 PM8/5/19
to ansible...@googlegroups.com
Hi Bob,

On 05.08.19 23:21, Bob Tanner wrote:
> 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:

---
- hosts: localhost
roles:
- foobar

Role:

---
# foobar/tasks/main.yml
- debug:
var: config

- import_tasks: foo.yml
when: config == 'git'

- include_tasks: foo.yml
when: config == 'nogit'

- debug:
msg: 'End'

---
# foobar/tasks/foo.yml
- debug:
msg: ABCDE


------------------------

ansible-playbook test.yml -e config=abc

PLAY [localhost] *****************************

TASK [Gathering Facts] ***********************
ok: [localhost]

TASK [foobar : debug] ************************
ok: [localhost] => {
"config": "abc"
}

TASK [foobar : debug] ************************
skipping: [localhost]

TASK [foobar : include_tasks] ****************
skipping: [localhost]

TASK [foobar : debug] ************************
ok: [localhost] => {
"msg": "End"
}

PLAY RECAP [...]

Sebastian

--
Sebastian Meyer
Linux Consultant & Trainer
Mail: me...@b1-systems.de

B1 Systems GmbH
Osterfeldstraße 7 / 85088 Vohburg / http://www.b1-systems.de
GF: Ralph Dehner / Unternehmenssitz: Vohburg / AG: Ingolstadt,HRB 3537


-- Deutsche OpenStack Tage 2019 -- 10% Rabatt auf den Ticketpreis ----
------------------------ https://openstack-tage.de (Code DOST-B1) ----

Matt Martz

unread,
Aug 6, 2019, 10:26:23 AM8/6/19
to ansible...@googlegroups.com
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.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/38b657c1-2383-443d-b9e6-e6a62993cec0%40googlegroups.com.


--
Matt Martz
@sivel
sivel.net
Reply all
Reply to author
Forward
0 new messages