Imports are static, includes are dynamic. Imports happen at parsing time, includes at runtime.
Imports basically replace the task with the tasks from the file. There's no
import_task at runtime. So, attributes like
when (and most likely other attributes) are copied to every imported task.
includes are indeed executed.
when of an included task apply only to the task itself.
Tagged tasks from an imported file get executed if
import task is untagged. No tasks is executed from an included file if
include task is untagged.
All tasks from an imported file get executed if
import task is tagged. Only tagged tasks from an included file get executed if
include task is tagged.
- can't be used with
- can't import a file, which name depends on a variable
--list-tags doesn't show tags from included files
--list-tasks doesn't show tasks from included files
- you cannot use
notify to trigger a handler name which comes from inside a dynamic include
- you cannot use
--start-at-task to begin execution at a task inside a dynamic include
More on it here and here.
For me that basically comes down to the fact that
imports can't be used with loop attributes.
import would certainly fail in cases like this:
- import_tasks: set-x.yml
when: x is not defined
debug is not executed, since it inherits
import_tasks task. So, no importing task files that change variables used in
I had a policy to start with
imports, but once I need an
include make sure nothing is imported by that included file or the files it includes. But that's pretty damn hard to maintain. And it's still not clear if it'll protect me from troubles. That's mixing
imports which they don't recommend.
I can't use only
imports, since I occasionally need to loop
include tasks. I could probably switch to only
includes. But I decided to switch to imports everywhere except for the cases where the task is supposed to be run several times. I decided to experience all those tricky edge cases first-hand. Maybe there won't be any in my playbooks. Or hopefully I'll find a way to make it work.