Ansible Role Variables and Overrides
There are three ways to call a role and they are not interchangeable. roles: runs it before the play's tasks; import_role inserts it statically where you put it; include_role resolves it at run time. The difference shows up most sharply in tags, where one propagates and the other does not - which is captured here rather than described.
Roles and Collections Guide 25 of 45 Intermediate
- OSUbuntu 26.04 LTS
- ansible-core2.20.1
- Python3.14.4
- TimeAbout 16 min
import_* is static: parsed when the playbook is loaded, so its tasks appear in --list-tasks and inherit tags. include_* is dynamic: resolved when the task runs, so its contents are invisible until then and tags do not reach inside. Dynamic is what you need for a loop or a when on the include itself; static is what you need for tags.
| 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 role has to behave differently in two places. This matters because there are three ways to call a role and they are not interchangeable - and a value in vars/main.yml cannot be overridden the way a default can.
- calling a role from a play's
roles:section - overriding its defaults at the call site
- finding what you cannot override, when the same syntax is aimed at
vars/main.yml - comparing
import_rolewithinclude_rolefrom inside tasks - seeing the tag difference between the two
Before you start
- The
webapprole from guide 40. - The session deploys to
/opt/webappand/etc/webapp.confon thewebhost and removes both at the end.
-
The configuration this guide assumes
-
Calling a role
-
Overriding the defaults
-
What you cannot override
-
import_role versus include_role
-
The tag difference