Interview Prep/Ansible/Roles, Collections and Project Structure

A role is used twice with different parameters in one play, but only runs once.

IntermediateScenarioRoles, Collections and Project Structure
Answer

Situation read → Role de-duplication.

What I'd do → Set allow_duplicates: true in the role's meta/main.yml, or use include_role with vars.

# meta/main.yml
allow_duplicates: true
- ansible.builtin.include_role: { name: vhost, vars: { site: a.example.com } }
- ansible.builtin.include_role: { name: vhost, vars: { site: b.example.com } }

Why → Roles with identical parameters are deduplicated by default.

Interview tip → A loop over include_role is another common pattern.

All Ansible questions Roadmap: Ansible

More on Roles, Collections and Project Structure