Interview Prep/Ansible/Roles, Collections and Project Structure

When would you pick `include_tasks` over `import_tasks`?

IntermediateScenarioRoles, Collections and Project Structure
Answer

Situation read → Static vs dynamic reuse.

What I'd do → Use include_tasks when the file name depends on a variable, when you loop over the include, or when you need when applied once at runtime. Use import_tasks when you want tags and --list-tasks to see everything.

- ansible.builtin.include_tasks: "{{ ansible_facts['os_family'] }}.yml"

Why → Imports are expanded before the run; includes are evaluated as the play reaches them, so tags on the include apply only to the include itself.

Interview tip → --list-tasks does not show tasks inside dynamic includes.

All Ansible questions Roadmap: Ansible

More on Roles, Collections and Project Structure