allow_duplicates & Role Idempotency
Control repeated role execution.
allow_duplicates & Role Idempotency is a free Ansible Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ansible Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Roles Run Once by Default
If two roles both depend on a common role, Ansible is smart: it runs common only once, not twice, during a single play.
Why Dedup Exists
This deduplication stops shared setup roles from repeating expensive work, like re-running a base hardening role for every app role.
Same Role, Same Params
The skip only applies when the role is requested with the same parameters. Identical calls are treated as already done.
Different Params, Runs Again
Call the same role with different variables and Ansible runs it again. A vhost role with two configs applies twice, as you want.
roles:
- role: vhost
vars: { name: site_a }
- role: vhost
vars: { name: site_b }Meet allow_duplicates
The allow_duplicates setting in meta/main.yml controls this behavior. Set it to true and the role may run repeatedly within a play.
# meta/main.yml
allow_duplicates: trueWhen to Allow Duplicates
Turn it on for roles meant to apply many times, like creating multiple users or virtual hosts, each call doing genuinely new work.
Default Is False
By default allow_duplicates is false, so a role with identical params runs just once. Leave it off for one-time setup roles.
Dedup Is Not Idempotency
Skipping a duplicate call is a runtime convenience. True idempotency is each task converging to a state, no matter how often it runs.
Build Idempotent Tasks
Inside a role, prefer state-based modules over raw command. A role that is truly idempotent is safe to apply twice with no harm.
include_role Always Re-runs
Watch out: include_role does not honor the dedup logic the same way. A dynamically included role runs each time you reach it.
Composable and Safe
Combine dedup, allow_duplicates, and idempotent tasks to build role hierarchies that compose cleanly and stay safe to re-run.
Quick Check
Recall the setting that lets a role run more than once in a play.
Recap
Ansible dedups identical role calls; set allow_duplicates to true to repeat one, and keep tasks idempotent for safe re-runs. ♻️
Frequently asked questions
Is the “allow_duplicates & Role Idempotency” lesson free?
Yes — the full text of “allow_duplicates & Role Idempotency” is free to read here on the web, and the Ansible Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ansible Academy course, upgrade to CoddyKit PRO.
What will I learn in “allow_duplicates & Role Idempotency”?
Control repeated role execution. You practise Ansible Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Ansible Academy?
No prior experience is required. Ansible Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “allow_duplicates & Role Idempotency” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Ansible Academy lesson?
Yes. Every Ansible Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Role Dependencies in meta/main.yml
- defaults vs vars: The Override Order
- include_role & import_role at Runtime
- allow_duplicates & Role Idempotency