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

**URL:** <https://forum.ansible.com/t/how-to-run-a-role-on-a-specific-host-group-only-ansible-automation-platform/3679>\
**Category:** Get Help\
**Created:** [February 5, 2024, 4:22pm UTC](https://forum.ansible.com/t/how-to-run-a-role-on-a-specific-host-group-only-ansible-automation-platform/3679 "2024-02-05T16:22:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![slekkus](https://avatars.discourse-cdn.com/v4/letter/s/e480ec/32.png) [@slekkus](https://forum.ansible.com/u/slekkus)\
**Post date:** [February 5, 2024, 4:22pm UTC](https://forum.ansible.com/t/how-to-run-a-role-on-a-specific-host-group-only-ansible-automation-platform/3679/1 "2024-02-05T16:22:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![shertel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/shertel/32/1241_2.png) [@shertel](https://forum.ansible.com/u/shertel)\
**Post date:** [February 5, 2024, 11:15pm UTC](https://forum.ansible.com/t/how-to-run-a-role-on-a-specific-host-group-only-ansible-automation-platform/3679/2 "2024-02-05T23:15:24Z")

</div>

You can use the [group\_names](https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html#term-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`:

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

```

---

<div class="post-metadata">

**Author:** ![slekkus](https://avatars.discourse-cdn.com/v4/letter/s/e480ec/32.png) [@slekkus](https://forum.ansible.com/u/slekkus)\
**Post date:** [February 6, 2024, 3:57pm UTC](https://forum.ansible.com/t/how-to-run-a-role-on-a-specific-host-group-only-ansible-automation-platform/3679/3 "2024-02-06T15:57:34Z")

</div>

Hi shertel, that worked flawlesly. Thanks much.
