DevOps Bootcamp · Lesson

Splitting Playbooks with import & include

Static vs dynamic task inclusion.

Lesson 3 of 413 steps

Splitting Playbooks with import & include is a free DevOps Bootcamp lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, and your progress syncs across the web and the CoddyKit app. The DevOps Bootcamp course includes 4 lessons in total.

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.yml

Dynamic 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.yml

Reuse 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. ✅

Free to start

Learn DevOps Bootcamp with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
142
Lessons
568

Frequently Asked Questions

Is the “Splitting Playbooks with import & include” lesson free?

Yes — any 3 lessons of the DevOps Bootcamp learning path, including “Splitting Playbooks with import & include”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The DevOps Bootcamp course includes 4 lessons in total.

What will I learn in “Splitting Playbooks with import & include”?

Static vs dynamic task inclusion. You practise DevOps Bootcamp 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 DevOps Bootcamp?

No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

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 DevOps Bootcamp lesson?

Yes. Every DevOps Bootcamp 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

  1. The Recommended Directory Layout
  2. group_vars & host_vars Directories
  3. Splitting Playbooks with import & include
  4. Naming, Comments & Style That Scale
← Back to DevOps Bootcamp