Ansible Play Execution Order
A play has four task sections and they do not run top to bottom in file order. Add role dependencies, which run before the role that declares them, and handlers, which run at the end, and the execution order becomes something worth seeing printed. Each section here announces its own number.
Roles and Collections Guide 26 of 45 Intermediate
- OSUbuntu 26.04 LTS
- ansible-core2.20.1
- Python3.14.4
- TimeAbout 14 min
Role dependencies are deduplicated: a role listed as a dependency by two others runs once, at the point of the first. That is usually what you want and is occasionally surprising - a dependency with side effects will not run again for the second consumer.
| Server Name | IP Address | OS | Roles | CPU | RAM | HDD |
|---|---|---|---|---|---|---|
| ANS-CTL01 | 192.168.0.36 | Ubuntu 26.04 LTS | Ansible Control Node | 2 Core | 3 GB | 50 GB |
| ANS-A01 | 192.168.0.37 | Ubuntu 26.04 LTS | Managed Node (group: web) | 2 Core | 3 GB | 50 GB |
This guide includes
Use this when a task runs earlier or later than you expected. This matters because a play has four task sections that do not run in file order - and role dependencies run once, before the role that asked for them.
- building two tiny roles with a dependency between them
- reading the real order - pre_tasks, then roles, then tasks, then post_tasks
- confirming dependencies run once even when both roles are listed
- finding where a role's handlers actually live
Before you start
- A control node configured as shown in the first step.
- The session creates two tiny roles,
baseandappsvc, whereappsvcdepends onbase. Nothing is changed on the managed node.
-
The configuration this guide assumes
-
Two tiny roles to order
-
The order a play runs in
-
Dependencies run once
-
Where a role's handlers live