How to run a role on a specific host group only (Ansible Automation Platform)

Hi, I have a template with a playbook running 2 roles. Each role has to be ran on a specific set of hosts. So, created an inventory with all the hosts and within this inventory created 2 groups. How do I call each role from the main playbook with the hosts group it needs to ran on?

Kind regards.

You can use the group_names special var in a when condition with import_role/include_role tasks and play roles:. If you don’t want to see the role tasks skipped for hosts outside of the group you’re trying to match, use include_role:

- include_role:
    name: role_name
  when: group_names|intersect(['group_name'])

Hi shertel, that worked flawlesly. Thanks much.

1 Like