# Is there a strategy that runs on X hosts as fast as possible

**URL:** <https://forum.ansible.com/t/is-there-a-strategy-that-runs-on-x-hosts-as-fast-as-possible/33570>\
**Category:** Ansible Project\
**Created:** [August 9, 2020, 11:33am UTC](https://forum.ansible.com/t/is-there-a-strategy-that-runs-on-x-hosts-as-fast-as-possible/33570 "2020-08-09T11:33:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Igor\_Gnatenko](https://avatars.discourse-cdn.com/v4/letter/i/e8c25b/32.png) [@Igor\_Gnatenko](https://forum.ansible.com/u/Igor_Gnatenko)\
**Post date:** [August 9, 2020, 11:33am UTC](https://forum.ansible.com/t/is-there-a-strategy-that-runs-on-x-hosts-as-fast-as-possible/33570/1 "2020-08-09T11:33:58Z")

</div>

Basically I am looking for something like `parallel -jX` linux command. At any time it runs up to X threads with a command.

I noticed that serial: 2 with any strategy will wait for play to run on these 2 hosts and only after that it will move to the next ones. However, I’d like Ansible to run on 2 hosts at any time, so once it finishes with 1 of 2 in batch, it would already run playbook for the 3rd host.

Is such thing does not exist or I just can’t find how to do it?

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [August 9, 2020, 2:51pm UTC](https://forum.ansible.com/t/is-there-a-strategy-that-runs-on-x-hosts-as-fast-as-possible/33570/2 "2020-08-09T14:51:58Z")

</div>

> Basically I am looking for something like `parallel -jX` linux command. At  
> any time it runs up to X threads with a command.

That's what strategy "host\_pinned" does. See  
[https://docs.ansible.com/ansible/latest/plugins/strategy/host\_pinned.html#host-pinned-executes-tasks-on-each-host-without-interruption](https://docs.ansible.com/ansible/latest/plugins/strategy/host_pinned.html#host-pinned-executes-tasks-on-each-host-without-interruption)

> I noticed that serial: 2 with any strategy will wait for play to run on  
> these 2 hosts and only after that it will move to the next ones.

Only "linear" works this way, I think.

> I'd like Ansible to run on 2 hosts at any time, so once it finishes with 1  
> of 2 in batch, it would already run playbook for the 3rd host.  
> Is such thing does not exist or I just can't find how to do it?

See this example. Is this what you're looking for?  
[https://stackoverflow.com/questions/59877712/execute-ansible-playbook-for-next-host-after-finishing-one-host-not-all-forked-h](https://stackoverflow.com/questions/59877712/execute-ansible-playbook-for-next-host-after-finishing-one-host-not-all-forked-h)

$ cat hosts  
all:  
&nbsp;&nbsp;hosts:  
&nbsp;&nbsp;&nbsp;&nbsp;test\_01:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wait\_timeout: 1  
&nbsp;&nbsp;&nbsp;&nbsp;test\_02:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wait\_timeout: 2  
&nbsp;&nbsp;&nbsp;&nbsp;test\_03:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wait\_timeout: 3  
&nbsp;&nbsp;&nbsp;&nbsp;test\_06:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wait\_timeout: 4  
&nbsp;&nbsp;&nbsp;&nbsp;test\_09:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wait\_timeout: 5

$ cat pinned-01.yml  
- name: Play A  
&nbsp;&nbsp;hosts: all  
&nbsp;&nbsp;gather\_facts: false  
&nbsp;&nbsp;strategy: host\_pinned  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "A:{{ inventory\_hostname }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ lookup('pipe', 'date +%H-%M-%S') }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;started"  
&nbsp;&nbsp;&nbsp;&nbsp;- wait\_for:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;timeout: "{{ wait\_timeout }}"  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "A:{{ inventory\_hostname }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ lookup('pipe', 'date +%H-%M-%S') }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;finished"

$ ansible-playbook pinned-01.yml -f 3 | grep msg\":  
&nbsp;&nbsp;&nbsp;&nbsp;"msg": "A:test\_06 15-33-05 started"  
&nbsp;&nbsp;&nbsp;&nbsp;"msg": "A:test\_09 15-33-05 started"  
&nbsp;&nbsp;&nbsp;&nbsp;"msg": "A:test\_01 15-33-05 started"  
&nbsp;&nbsp;&nbsp;&nbsp;"msg": "A:test\_01 15-33-08 finished"  
&nbsp;&nbsp;&nbsp;&nbsp;"msg": "A:test\_02 15-33-08 started"  
&nbsp;&nbsp;&nbsp;&nbsp;"msg": "A:test\_06 15-33-11 finished"  
&nbsp;&nbsp;&nbsp;&nbsp;"msg": "A:test\_03 15-33-11 started"  
&nbsp;&nbsp;&nbsp;&nbsp;"msg": "A:test\_02 15-33-11 finished"  
&nbsp;&nbsp;&nbsp;&nbsp;"msg": "A:test\_09 15-33-12 finished"  
&nbsp;&nbsp;&nbsp;&nbsp;"msg": "A:test\_03 15-33-15 finished"  
Results

Because of -f 3 Ansible started 3 hosts (1,9,6). Host 1 finished  
first and opened it's slot to a new host that was waiting to start.  
Host 2 started. The same way host 3 started right after host 6  
finished. Host 2, which started after host 9, finished before host 9.

---

<div class="post-metadata">

**Author:** ![Igor\_Gnatenko](https://avatars.discourse-cdn.com/v4/letter/i/e8c25b/32.png) [@Igor\_Gnatenko](https://forum.ansible.com/u/Igor_Gnatenko)\
**Post date:** [August 9, 2020, 5:48pm UTC](https://forum.ansible.com/t/is-there-a-strategy-that-runs-on-x-hosts-as-fast-as-possible/33570/3 "2020-08-09T17:48:31Z")

</div>

Hello Vladimir,

I saw that one, but I misunderstood how that works because I also had  
`serial` set in the playbook. Controlling how many tasks are executed  
via forks does exactly what I was looking for.

Thank you for your prompt response!
