In an Ansible playbook, roles are processed before tasks.
Roles are a collection of tasks, handlers, templates, files, and other resources grouped together to perform a specific function. When a role is included in a playbook, Ansible first processes the role’s tasks and handlers before continuing with the rest of the playbook’s tasks.
The order in which Ansible processes a playbook is as follows:
- The playbook’s pre-tasks are processed.
- The playbook’s roles are processed in the order in which they are included in the playbook.
- The playbook’s tasks are processed.
- The playbook’s post-tasks are processed.
Note that handlers defined in roles are processed in the order in which they are defined in the role, and then in the order in which they are included in the playbook.
This order of processing ensures that roles are fully processed and all their dependencies are installed before the tasks in the playbook are executed. This also makes it easier to reuse roles across multiple playbooks, as the role’s tasks are defined and processed separately from the tasks in the playbook.