DevOps / Ansible Interview questions
What is the difference between include and import in Ansible?
| import_* (static) | include_* (dynamic) |
| Processed at playbook parse time, before the run starts. | Processed at runtime, as the play executes. |
| Tags and conditionals apply to every task inside up front. | Can use variables/loops to decide what to include on the fly. |
| Can't use a variable that's only known at runtime for the file path. | Can use runtime variables in the included file's path. |
| Slightly more predictable and easier to reason about statically. | More flexible for conditional or looped inclusion. |
Use import_tasks/import_playbook when the structure is fixed and known ahead of time, since static processing lets tools like --list-tasks show the full task list without running anything. Use include_tasks/include_role when the set of tasks to run genuinely depends on a runtime condition or needs to loop over a dynamic list, since only dynamic inclusion can evaluate that condition as the play unfolds rather than upfront.
More Related questions...