Splitting Playbooks with import & include
Static vs dynamic task inclusion.
Splitting Playbooks with import & include is a free Ansible Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ansible Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
One Giant File Hurts
A 500-line playbook is hard to read and reuse. Ansible lets you split work into pieces and pull them in where needed. ✂️
Two Families: import and include
Ansible offers two reuse styles: import_* for static inclusion and include_* for dynamic inclusion. The difference is timing.
Static import_tasks
import_tasks is pre-processed when the playbook is parsed, before any task runs. It is fixed and predictable.
- import_tasks: setup.ymlDynamic include_tasks
include_tasks is processed at runtime, in order, so it can react to variables and results decided earlier in the play.
- include_tasks: "{{ os_family }}.yml"Why the Timing Matters
Because import is static, you cannot use a runtime variable in its filename. For that you need the dynamic include_tasks instead.
Loops Need include
Want to repeat a whole file? Only include_tasks supports loop, since the file set is decided while the play runs.
- include_tasks: user.yml
loop: "{{ users }}"Tags Behave Differently
A tag on import_tasks applies to every imported task, but a tag on include_tasks applies only to the include statement itself.
Importing Whole Playbooks
At the top level use import_playbook to combine entire playbooks. This is exactly how site.yml assembles your project.
- import_playbook: webservers.yml
- import_playbook: dbservers.ymlReuse Across Plays
Shared task files like a common bootstrap can be imported into many playbooks, so you write the logic once and reuse it everywhere.
A Sensible Default
Prefer import for predictable, always-run steps. Reach for include only when you truly need runtime decisions like loops or variable filenames.
Splitting in Action
Here a play imports a static setup file, then conditionally includes an OS-specific one. Two tools, two jobs. 🎯
tasks:
- import_tasks: base.yml
- include_tasks: "{{ ansible_os_family }}.yml"Quick Check
You need a filename built from a runtime variable. Which directive works?
Recap
You learned import_* is static and parsed up front, while include_* is dynamic and runs in place, enabling loops and variable filenames. ✅
Frequently asked questions
Is the “Splitting Playbooks with import & include” lesson free?
Yes — the full text of “Splitting Playbooks with import & include” is free to read here on the web, and the Ansible Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ansible Academy course, upgrade to CoddyKit PRO.
What will I learn in “Splitting Playbooks with import & include”?
Static vs dynamic task inclusion. You practise Ansible Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Ansible Academy?
No prior experience is required. Ansible Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Splitting Playbooks with import & include” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Ansible Academy lesson?
Yes. Every Ansible Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- The Recommended Directory Layout
- group_vars & host_vars Directories
- Splitting Playbooks with import & include
- Naming, Comments & Style That Scale