# How to run delegate\_to task only on one host in group with if/when condition

**URL:** <https://forum.ansible.com/t/how-to-run-delegate-to-task-only-on-one-host-in-group-with-if-when-condition/27949>\
**Category:** Ansible Project\
**Created:** [July 4, 2018, 8:19am UTC](https://forum.ansible.com/t/how-to-run-delegate-to-task-only-on-one-host-in-group-with-if-when-condition/27949 "2018-07-04T08:19:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![abhijith.devaraju](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@abhijith.devaraju](https://forum.ansible.com/u/abhijith.devaraju)\
**Post date:** [July 4, 2018, 8:19am UTC](https://forum.ansible.com/t/how-to-run-delegate-to-task-only-on-one-host-in-group-with-if-when-condition/27949/1 "2018-07-04T08:19:30Z")

</div>

Hi All,

My host file looks like below where I am using specific host variables “smca\_role”.

[smca]  
smca1 ansible\_host=10.66.69.194 smca\_role=True  
smca2 ansible\_host=10.66.69.193 smca\_role=False

My playbook task looks like below. I am delegating a task to the above **‘smca’** group and it is going to run on both the hosts which I don’t want.

- name: Check the flag for smca role  
shell: _ **Some Command** _  
register: enable\_result  
delegate\_to: ‘{{ item }}’  
with\_items: ‘{{ groups.smca }}’

**My problem:** I want to run it on only one host which matches the condition **“smca\_role=True”.** How can I use jinja template & put if/when condition inside with\_items: ‘{{ groups.smca }}’ so that it runs only on the required host ?

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [July 4, 2018, 8:39am UTC](https://forum.ansible.com/t/how-to-run-delegate-to-task-only-on-one-host-in-group-with-if-when-condition/27949/2 "2018-07-04T08:39:19Z")

</div>

Try this.

`

- name: Check the flag for smca role  
shell: _ **Some Command** _  
register: enable\_result  
delegate\_to: ‘{{ groups.smca[0] }}’

`

The [0] gives you first member of the list.

To be honest if you are just doing tasks in a playbook I might just have a second play in the playbook to address the host I needed as I think it makes the playbook more readable.

`

- hosts: smca  
tasks:

- name: connect test  
wait\_for\_connection:

- hosts: smca\_lead\_host  
tasks:

- name: do a shell thing  
shell: echo Hello  
register: enable\_result  
`

But it depends on what you are trying to achieve.

Hope this helps,

Jon

---

<div class="post-metadata">

**Author:** ![abhijith.devaraju](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@abhijith.devaraju](https://forum.ansible.com/u/abhijith.devaraju)\
**Post date:** [July 4, 2018, 8:48am UTC](https://forum.ansible.com/t/how-to-run-delegate-to-task-only-on-one-host-in-group-with-if-when-condition/27949/3 "2018-07-04T08:48:07Z")

</div>

Hi Jon,

Thanks for your response. ‘{{ groups.smca[0] }}’ won’t work in my case as there is no guarantee that only the first host will have that condition true it can keep changing & smca group can have more than 2 hosts. The only thing unique would be one host will have \*\*"\*\ ***smca\_role=True"** and that’s the reason I want to check with that variable and then delegate. I guess there should be someway to specific conditions inside jinja template.

The separate playbook won’t work in what I am trying to achieve.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 4, 2018, 9:32am UTC](https://forum.ansible.com/t/how-to-run-delegate-to-task-only-on-one-host-in-group-with-if-when-condition/27949/4 "2018-07-04T09:32:35Z")

</div>

when: smca\_role | bool

will give you that, only run on host where smca\_role is True.

---

<div class="post-metadata">

**Author:** ![abhijith.devaraju](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@abhijith.devaraju](https://forum.ansible.com/u/abhijith.devaraju)\
**Post date:** [July 4, 2018, 5:26pm UTC](https://forum.ansible.com/t/how-to-run-delegate-to-task-only-on-one-host-in-group-with-if-when-condition/27949/5 "2018-07-04T17:26:16Z")

</div>

Hi Kai,

Thank You for the response. Using plain when condition won’t work in my case as the playbook is run on another host group where I’m gathering/setting facts and I’m using those facts when delegating it to “smca” host group.

My only requirement is to put this kind of when condition inside “with\_items” so that it selects host which has "smca\_role=True " and delegates the task only to that particular host.

---

<div class="post-metadata">

**Author:** ![Ilya\_Ulis](https://avatars.discourse-cdn.com/v4/letter/i/43a26b/32.png) [@Ilya\_Ulis](https://forum.ansible.com/u/Ilya_Ulis)\
**Post date:** [July 5, 2018, 5:53am UTC](https://forum.ansible.com/t/how-to-run-delegate-to-task-only-on-one-host-in-group-with-if-when-condition/27949/6 "2018-07-05T05:53:06Z")

</div>

And if you will try to separate main playbook to playbook + role?  
Like that:

hosts:  
[smca]  
smca1 ansible\_host=10.66.69.194  
smca2 ansible\_host=10.66.69.193

playbook.yml:

- hosts: localhost  
roles:
- { role: some\_role, node: smca1, smca\_role: True }
- { role: some\_role, node: smca2, smca\_role: False }

some\_role/tasks/main.yml

- name: Check the flag for smca role  
shell: _ **Some Command** _  
register: enable\_result  
delegate\_to: ‘{{ node }}’  
when: smca\_role == “True”

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [July 5, 2018, 7:25am UTC](https://forum.ansible.com/t/how-to-run-delegate-to-task-only-on-one-host-in-group-with-if-when-condition/27949/7 "2018-07-05T07:25:14Z")

</div>

If it isn’t working for you I suggest splitting it up and doing a set\_fact to determine the host that has smca\_role=True set. That way you can just delegate\_to: the relevant host and don’t need to use ‘with\_items’.

Hope this helps,

Jon
