# Do task on one hosts with skipping other ones

**URL:** <https://forum.ansible.com/t/do-task-on-one-hosts-with-skipping-other-ones/37533>\
**Category:** Ansible Project\
**Created:** [January 9, 2023, 11:00am UTC](https://forum.ansible.com/t/do-task-on-one-hosts-with-skipping-other-ones/37533 "2023-01-09T11:00:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Newbie1](https://avatars.discourse-cdn.com/v4/letter/n/ee7513/32.png) [@Newbie1](https://forum.ansible.com/u/Newbie1)\
**Post date:** [January 9, 2023, 11:00am UTC](https://forum.ansible.com/t/do-task-on-one-hosts-with-skipping-other-ones/37533/1 "2023-01-09T11:00:26Z")

</div>

Hi  
Maybe idiotic problem but I don’t have clue how to bite it 😕  
I have example list of 3 hosts all are needed in inventory. I need to run one task to stop application  
Host 1  
Host 2  
Host 3  
app can be stopped only on host 2 and 3 and it need to run once because it will stop for whole landscape. I know that I can create a static var with host name but then any landscape i need to change it.

so I though to eliminate host1 from list and then run only once on one of rest hosts (host2/host3)  
I tried to do double block but in first iteration skip host 1 but in second block don’t do anything

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [January 9, 2023, 11:25am UTC](https://forum.ansible.com/t/do-task-on-one-hosts-with-skipping-other-ones/37533/2 "2023-01-09T11:25:37Z")

</div>

I can’t make any sense of what you’re trying to achieve. Can you rephrase and be more clear?

---

<div class="post-metadata">

**Author:** ![Newbie1](https://avatars.discourse-cdn.com/v4/letter/n/ee7513/32.png) [@Newbie1](https://forum.ansible.com/u/Newbie1)\
**Post date:** [January 9, 2023, 11:48am UTC](https://forum.ansible.com/t/do-task-on-one-hosts-with-skipping-other-ones/37533/3 "2023-01-09T11:48:02Z")

</div>

I’m running task on group called A  
[A]  
dbhost1  
apphost2  
apphost3

task “stop application” should run only on apphost2 or 3 and need to run only once(script stop whole environment )  
so I would like to eliminate from “stop application” dbhost1 and then run stop part only on one of apphosts(no different which one)  
before stop part there some task running on all of nodes  
I have multiple environments so and in each of them also is db and app servers and only app servers can stop  
I tried to use

- block
- block:  
  
run\_once: yes  
when: ansible\_hostname[0] == “a”

but if it will catch DB host it will skip it and then skip block part  
I hope is more clear now 😕

---

<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 9, 2023, 12:13pm UTC](https://forum.ansible.com/t/do-task-on-one-hosts-with-skipping-other-ones/37533/4 "2023-01-09T12:13:20Z")

</div>

This looks like an inventory issue to me. You could have more fine-grained control with hierarchical groups:

```auto
[a:children]
a_db
a_app

[a_db]
dbhost1

[a_app]
apphost2
apphost3

```

Then run your “stop application” task like so:

- name: Stop application  
…  
when: ‘inventory\_hostname in group[“a\_app”]’  
run\_once: true

---

<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 9, 2023, 1:12pm UTC](https://forum.ansible.com/t/do-task-on-one-hosts-with-skipping-other-ones/37533/5 "2023-01-09T13:12:55Z")

</div>

You mentioned multiple environments. The suggestion I made before is only sufficient for one environment. In our shop, we use “fully articulated group names” that consist of the problem _“domain”,_ the “_environment_”, plus host classes as required to specify the hosts without relying on special hostname patterns or other tricks.

In your case, suppose your app is called “xkcd”, and you have environments named “dev”, “tst”, “spt”, and “prd”, each of which contains a db host and a few app hosts. Using our shop’s inventory conventions for the “xkcd” problem domain would result in the inventory below. Though not required, by convention we always use a host’s FQDN as the inventory name (i.e. ansible\_fqdn==inventory\_hostname). We may also set up an “all” pseudo environment that encompasses all the hosts in the problem domain. It’s occasionally useful in playbooks, and it also helps ensure we’ve created our group hierarchy the way we expect. In particular, if we find we’re having to specify a hostname more than once in a problem domain, we probably have not created the correct hierarchy of sub\_groups.

```auto
[xkcd_all:children]
xkcd_dev
xkcd_tst
xkcd_spt
xkcd_prd

[xkcd_all_db:children]
xkcd_dev_db
xkcd_tst_db
xkcd_spt_db
xkcd_prd_db

[xkcd_all_app:children]
xkcd_dev_app
xkcd_tst_app
xkcd_spt_app
xkcd_prd_app

[xkcd_dev:children]
xkcd_dev_db
xkcd_dev_app

[xkcd_dev_db]
dbhost0d.our.org

[xkcd_dev_app]
apphost0d.our.org

[xkcd_tst:children]
xkcd_tst_db
xkcd_tst_app

[xkcd_tst_db]
dbhost0t.our.org

[xkcd_tst_app]
apphost0t.our.org
apphost1t.our.org

[xkcd_spt:children]
xkcd_spt_db
xkcd_spt_app

[xkcd_spt_db]
dbhost0s.our.org
dbhost1s.our.org

[xkcd_spt_app]
apphost0s.our.org
apphost1s.our.org

[xkcd_prd:children]
xkcd_prd_db
xkcd_prd_app

[xkcd_prd_db]
dbhost0p.our.org
dbhost1p.our.org

[xkcd_prd_app]
apphost0p.our.org
apphost1p.our.org
apphost2p.our.org
apphost3p.our.org

```

---

<div class="post-metadata">

**Author:** ![Newbie1](https://avatars.discourse-cdn.com/v4/letter/n/ee7513/32.png) [@Newbie1](https://forum.ansible.com/u/Newbie1)\
**Post date:** [January 10, 2023, 3:09pm UTC](https://forum.ansible.com/t/do-task-on-one-hosts-with-skipping-other-ones/37533/6 "2023-01-10T15:09:29Z")

</div>

Hi  
I solved issue with interfering with inventory  
I put stop script on one host and lookup for it and start only on server what have it, that solve issue across. I believe whole landscape i will test it in next weeks to see
