# Execute task for specific host.

**URL:** <https://forum.ansible.com/t/execute-task-for-specific-host/17457>\
**Category:** Ansible Project\
**Created:** [February 6, 2015, 11:27am UTC](https://forum.ansible.com/t/execute-task-for-specific-host/17457 "2015-02-06T11:27:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexey\_Lesovsky](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@Alexey\_Lesovsky](https://forum.ansible.com/u/Alexey_Lesovsky)\
**Post date:** [February 6, 2015, 11:27am UTC](https://forum.ansible.com/t/execute-task-for-specific-host/17457/1 "2015-02-06T11:27:43Z")

</div>

Hello all!  
I have playbook with following parts:

`  
vars:  
servers:

- { address: “192.168.122.12”, role: “upstream” }
- { address: “192.168.122.13”, role: “downstream” }
- { address: “192.168.122.14”, role: “downstream” }  
task:
- name: Task  
command: “/usr/sbin/somecommand”  
`

How I can execute task only on upstream or downstream servers? Any help appreciated.

P.S. I try following part, but it doesn’t work:  
`  
task:

- name: Task  
command: “/usr/sbin/somecommand”  
when: “servers.role == upstream”

`

And got error:

fatal: [vm12-centos7] =\> error while evaluating conditional: servers.role == upstream  
fatal: [vm13-centos7] =\> error while evaluating conditional: servers.role == upstream

fatal: [vm14-centos7] =\> error while evaluating conditional: servers.role == upstream

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [February 6, 2015, 11:37am UTC](https://forum.ansible.com/t/execute-task-for-specific-host/17457/2 "2015-02-06T11:37:02Z")

</div>

​try this:  
​

when: servers.role ==​  
​ ​  
"  
​​  
​upstream"

---

<div class="post-metadata">

**Author:** ![Igor\_Homyakov](https://avatars.discourse-cdn.com/v4/letter/i/f4b2a3/32.png) [@Igor\_Homyakov](https://forum.ansible.com/u/Igor_Homyakov)\
**Post date:** [February 6, 2015, 12:24pm UTC](https://forum.ansible.com/t/execute-task-for-specific-host/17457/3 "2015-02-06T12:24:39Z")

</div>

Hi Alexey,

You do not need to quote a whole string here, could you try

when: servers.role == "upstream"

-- Best, Igor

---

<div class="post-metadata">

**Author:** ![Giovanni\_Tirloni](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Giovanni\_Tirloni](https://forum.ansible.com/u/Giovanni_Tirloni)\
**Post date:** [February 6, 2015, 12:47pm UTC](https://forum.ansible.com/t/execute-task-for-specific-host/17457/4 "2015-02-06T12:47:53Z")

</div>

IMHO, it's better to work with groups in this scenario:

# inventory  
[downstream]  
192.168.122.12  
192.168.122.14

[upstream]  
192.168.122.12

You can either define multiple plays targeting each group with a set of  
tasks/roles:

# playbook

---

<div class="post-metadata">

**Author:** ![Alexey\_Lesovsky](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@Alexey\_Lesovsky](https://forum.ansible.com/u/Alexey_Lesovsky)\
**Post date:** [February 6, 2015, 1:05pm UTC](https://forum.ansible.com/t/execute-task-for-specific-host/17457/5 "2015-02-06T13:05:15Z")

</div>

Yes, I am tried use quotes, and this not working.

---

<div class="post-metadata">

**Author:** ![Alexey\_Lesovsky](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@Alexey\_Lesovsky](https://forum.ansible.com/u/Alexey_Lesovsky)\
**Post date:** [February 6, 2015, 1:16pm UTC](https://forum.ansible.com/t/execute-task-for-specific-host/17457/6 "2015-02-06T13:16:07Z")

</div>

Eventually I rewrote playbook using groups and include statements for tasks, and it works fine. But, in the current circumstances I’m not quite satisfied, I need to make a single file playbook. Yes, I know it’s not quite right and not a best practice )))

---

<div class="post-metadata">

**Author:** ![cselden](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/cselden/32/4405_2.png) [@cselden](https://forum.ansible.com/u/cselden)\
**Post date:** [February 10, 2015, 7:10pm UTC](https://forum.ansible.com/t/execute-task-for-specific-host/17457/7 "2015-02-10T19:10:42Z")

</div>

Alexey,

The easiest way to do this is to use hostvars in your inventory and set a “role” variable for each host. Then, in your task you could be:

tasks:

- name: Task  
command: “/usr/bin/somecommand”  
when: role == “upstream”

Alternatively, if you want to set the variables here, this would work (tested):

---

<div class="post-metadata">

**Author:** ![Alexey\_Lesovsky](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@Alexey\_Lesovsky](https://forum.ansible.com/u/Alexey_Lesovsky)\
**Post date:** [February 10, 2015, 7:29pm UTC](https://forum.ansible.com/t/execute-task-for-specific-host/17457/8 "2015-02-10T19:29:47Z")

</div>

Thanks, it’s good for me!
