# Running ansible playbook on two hosts

**URL:** <https://forum.ansible.com/t/running-ansible-playbook-on-two-hosts/36408>\
**Category:** Ansible Project\
**Created:** [March 8, 2022, 10:15am UTC](https://forum.ansible.com/t/running-ansible-playbook-on-two-hosts/36408 "2022-03-08T10:15:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Netanel\_Peretz](https://avatars.discourse-cdn.com/v4/letter/n/ed655f/32.png) [@Netanel\_Peretz](https://forum.ansible.com/u/Netanel_Peretz)\
**Post date:** [March 8, 2022, 10:15am UTC](https://forum.ansible.com/t/running-ansible-playbook-on-two-hosts/36408/1 "2022-03-08T10:15:31Z")

</div>

Hey,  
I want to be able to use a playbook to run a task on host1 then run a task on localhost(ansible host) and then run another task on host1

Is that supposed to be as simple as copying something like this and changing the hostname and the task that comes after that?  
Is there a better way to do so?

- name: TASKNAME  
hosts: host1  
tasks:

- name: TASK ON HOST1  
shell: 

- name: TASKNAME  
hosts: localhost  
tasks:

- name: TASK ON LOCALHOST  
shell: 

- name: TASKNAME  
hosts: host1  
tasks:

- name: ANTOHER TASK ON HOST1  
shell:

---

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [March 8, 2022, 11:27am UTC](https://forum.ansible.com/t/running-ansible-playbook-on-two-hosts/36408/2 "2022-03-08T11:27:06Z")

</div>

I don’t understand 100% what you want to do. But generally I have learned any TASK can be sent to localhost with a preceding delegate\_to: localhost  
so you have a bunch of hosts your playbook runs against and some TASKs inside that playbok you want to run against your localhost (exclusively)

- name: TASKNAME  
copy:  
blablabla:

- name: TASK ON LOCALHOST  
delegate\_to: localhost  
shell:

---

<div class="post-metadata">

**Author:** ![Rilindo\_Foster1](https://avatars.discourse-cdn.com/v4/letter/r/8e8cbc/32.png) [@Rilindo\_Foster1](https://forum.ansible.com/u/Rilindo_Foster1)\
**Post date:** [March 8, 2022, 1:12pm UTC](https://forum.ansible.com/t/running-ansible-playbook-on-two-hosts/36408/3 "2022-03-08T13:12:17Z")

</div>

Hi Netanel,

You can use delegate\_to for this.

[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_delegation.html#delegating-tasks](https://docs.ansible.com/ansible/latest/user_guide/playbooks_delegation.html#delegating-tasks)

- Rilindo

---

<div class="post-metadata">

**Author:** ![Netanel\_Peretz](https://avatars.discourse-cdn.com/v4/letter/n/ed655f/32.png) [@Netanel\_Peretz](https://forum.ansible.com/u/Netanel_Peretz)\
**Post date:** [March 8, 2022, 1:55pm UTC](https://forum.ansible.com/t/running-ansible-playbook-on-two-hosts/36408/4 "2022-03-08T13:55:21Z")

</div>

Yup exactly what I was looking for.  
I read more about it with your link

Thanks!!!
