Running ansible playbook on two hosts

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:

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:

Hi Netanel,

You can use delegate_to for this.

https://docs.ansible.com/ansible/latest/user_guide/playbooks_delegation.html#delegating-tasks

  • Rilindo

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

Thanks!!!