# How to set a task to run for a specific inventory group

**URL:** <https://forum.ansible.com/t/how-to-set-a-task-to-run-for-a-specific-inventory-group/37584>\
**Category:** Ansible Project\
**Created:** [January 26, 2023, 6:11pm UTC](https://forum.ansible.com/t/how-to-set-a-task-to-run-for-a-specific-inventory-group/37584 "2023-01-26T18:11:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Emanuel\_Gonzalez](https://avatars.discourse-cdn.com/v4/letter/e/b3f665/32.png) [@Emanuel\_Gonzalez](https://forum.ansible.com/u/Emanuel_Gonzalez)\
**Post date:** [January 26, 2023, 6:11pm UTC](https://forum.ansible.com/t/how-to-set-a-task-to-run-for-a-specific-inventory-group/37584/1 "2023-01-26T18:11:41Z")

</div>

Good morning everyone, this is my hosts file:

web-servers:  
hosts:  
web01:  
ansible\_host: x.x.x.x  
web02:  
ansible\_host: x.x.x.x

backend-servers:  
hosts:  
balance1:  
ansible\_host: x.x.x.x  
balance2:  
ansible\_host: x.x.x.x  
balance3:  
ansible\_host: x.x.x.x

what is the correct way to call backend group servers? I tried this but it doesn’t work:

when: inventory\_hostname in groups[“backend-servers”]

Regards,

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [January 26, 2023, 6:25pm UTC](https://forum.ansible.com/t/how-to-set-a-task-to-run-for-a-specific-inventory-group/37584/2 "2023-01-26T18:25:51Z")

</div>

Not to sound snippy but did you test it to see?

I usually add a when condition to a debug task to test it.

> - debug: msg=“running on {{ inventory\_hostname }}”  
> when: inventory\_hostname in groups[“backend-servers”]

Then I run the playbook twice - once with the expected group, once with the other group.

> $ ansible-playbook my\_test\_playbook.yml -i backend-servers
> 
> running on balance1  
> running on balance2  
> running on balance3
> 
> $
> 
> $ ansible-playbook my\_test\_playbook.yml -i web-servers  
> $

This is not actual output … just a mock up.

Walter

---

<div class="post-metadata">

**Author:** ![Emanuel\_Gonzalez](https://avatars.discourse-cdn.com/v4/letter/e/b3f665/32.png) [@Emanuel\_Gonzalez](https://forum.ansible.com/u/Emanuel_Gonzalez)\
**Post date:** [January 26, 2023, 7:02pm UTC](https://forum.ansible.com/t/how-to-set-a-task-to-run-for-a-specific-inventory-group/37584/3 "2023-01-26T19:02:53Z")

</div>

For some reason you are evaluating the condition in all servers, shouldn’t you do it in one server?

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [January 27, 2023, 2:26am UTC](https://forum.ansible.com/t/how-to-set-a-task-to-run-for-a-specific-inventory-group/37584/4 "2023-01-27T02:26:41Z")

</div>

To be fair, the condition is evaluated on only one server: the Ansible controller. The task is skipped for other hosts in scope for that play.

And you did specifically ask for the task.

Alternatively, you could end the play, and in the same file start a new play that is scoped via “hosts:” to just the host group(s) you’re interested in, and put your task inside that play. That will eliminate the messages about the skipped hosts, because there are none.
