Ansible: Import & Include
In Ansible playbooks are a collection of different tasks. It's a good idea to break the tasks into the different files, which make it easier to include/import these task in different playbooks. Now the question is when to use include or import and how these two are different from each other?
Ansible provides four distributed – Variables – Task – Playbook – Role
Including variables, tasks, or role adds them into the playbook dynamically. This means when Ansible processes these files as they come up they are included in the current playbook as its variable, task, or role. So, these can be affected by the previous tasks.
Playbook's can not be used with the include
Importing task, playbook, or role add them into playbook statically. Ansible pre-processes these files before it runs any task in the playbook, which means these are not affected by other tasks.
Tip: Import variables if you want to use these more than once in the playbook.
Import and Include differ with the way Ansible loads these files into the playbook. So, it is good to use the one which best fits your use case.