Ansible 2.9 vs 2.10: Delegation in Role Tasks
0 reputation · 17 Nov 2022, 12:46 UTC
0 reputation · 17 Nov 2022, 12:46 UTC
Determine whether the delegate_to setting applied inside a role task behaves the same in Ansible 2.9 and 2.10, and identify any changes that affect variable scoping or execution count.
In 2.9, delegate_to works at the task level and does not alter role‑level delegation. Version 2.10 preserves the syntax but introduces a default that treats the entire role as delegated unless overridden. Documentation notes that delegating a role may produce unexpected variable scoping and that run_once can prevent duplicate runs. No migration guide addresses this ambiguity, and minor patch releases after 2.10 may further shift the behavior.
delegate_to on a role task default to delegating the entire role in Ansible 2.10?run_once required to avoid duplicate executions when delegating a role to the same host in 2.10?35275 reputation · 17 Nov 2022, 13:16 UTC
delegate_to set on a role include is interpreted as delegating the whole role unless an inner task explicitly overrides it.delegate_to affects only the annotated task; variables (including facts) are evaluated on the host where the role is invoked. In 2.10, when the role is delegated, all role‑internal tasks inherit the delegated host for variable evaluation, shifting the scope to that host.run_once to the role include prevents the role from running multiple times when the same host appears repeatedly (e.g., in a loop). It is not strictly required if you guarantee no such duplication, but it is the recommended safeguard.delegate_to on a role task applies only to that task; the rest of the role runs on the original host.delegate_to on a role task is treated as delegating the entire role unless overridden.run_once on the role include mitigates the duplication.The shift likely stems from a refactor of role inclusion logic that now treats the role include as a single unit for delegation purposes. Consequently, any delegate_to attached to the include propagates to all tasks inside the role, altering where facts are gathered and where variables are resolved. When the delegated host matches the original host and the role is included in a loop, the role may be executed once per loop iteration on that host, leading to apparent duplicates unless run_once limits the execution to a single run.
testrole with a task that prints inventory_hostname and a variable defined in vars/main.yml.delegate_to: other_host and runs against a loop (e.g., with_items: [1,2]) on a target host.run_once: yes to the role include and verify that, when looping, the task output appears only once per iteration.To confirm whether the behavior remains stable in your environment, please specify the exact Ansible 2.10.x patch version you are using, as later minor releases may have adjusted the delegation default.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.