# Run a task on only one server in a group in inventory.

**URL:** <https://forum.ansible.com/t/run-a-task-on-only-one-server-in-a-group-in-inventory/33245>\
**Category:** Ansible Project\
**Tags:** gcp, aws\
**Created:** [June 24, 2020, 10:33am UTC](https://forum.ansible.com/t/run-a-task-on-only-one-server-in-a-group-in-inventory/33245 "2020-06-24T10:33:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![soumya\_dutta](https://avatars.discourse-cdn.com/v4/letter/s/3ab097/32.png) [@soumya\_dutta](https://forum.ansible.com/u/soumya_dutta)\
**Post date:** [June 24, 2020, 10:33am UTC](https://forum.ansible.com/t/run-a-task-on-only-one-server-in-a-group-in-inventory/33245/1 "2020-06-24T10:33:02Z")

</div>

Hi guys,

I have a situation where I need to run one task in only one server of a particular group.

For example i have inventory as below  
[aws]  
server1  
server2

[gcp]  
server3  
server4

now I need to run a task on whichever server connects first and task should be run only once.  
I am using when condition to run tasks on gcp group as " inventory\_hostname in groups['gcp'] " but when I use run\_once: true it is taking the server1 from aws group and whole task is getting skipped.

Please help and guide me.

Regards,  
Soumya

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [June 24, 2020, 2:32pm UTC](https://forum.ansible.com/t/run-a-task-on-only-one-server-in-a-group-in-inventory/33245/2 "2020-06-24T14:32:48Z")

</div>

Split the playbook into two plays. For example

&nbsp;&nbsp;- hosts: aws  
&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: Run this task on one server only  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;run\_once: true  
&nbsp;&nbsp;- hosts: gcp  
&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: Run this task on one server only  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;run\_once: true

You might want to put such tasks into a role. For example

&nbsp;&nbsp;- hosts: aws  
&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- include\_role:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: my\_library\_of\_tasks  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tasks\_from: task\_on\_one\_server\_only.yml  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;run\_once: true  
&nbsp;&nbsp;- hosts: gcp  
&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- include\_role:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: my\_library\_of\_tasks  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tasks\_from: task\_on\_one\_server\_only.yml  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;run\_once: true

HTH,

&nbsp;&nbsp;-vlado
