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

74 views
Skip to first unread message

Igor Gnatenko

unread,
Aug 9, 2020, 7:33:59 AM8/9/20
to Ansible Project
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?

Vladimir Botka

unread,
Aug 9, 2020, 10:52:16 AM8/9/20
to Igor Gnatenko, ansible...@googlegroups.com
On Sun, 9 Aug 2020 04:33:58 -0700 (PDT)
Igor Gnatenko <i.gnaten...@gmail.com> wrote:

> 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

> 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

$ cat hosts
all:
hosts:
test_01:
wait_timeout: 1
test_02:
wait_timeout: 2
test_03:
wait_timeout: 3
test_06:
wait_timeout: 4
test_09:
wait_timeout: 5

$ cat pinned-01.yml
- name: Play A
hosts: all
gather_facts: false
strategy: host_pinned
tasks:
- debug:
msg: "A:{{ inventory_hostname }}
{{ lookup('pipe', 'date +%H-%M-%S') }}
started"
- wait_for:
timeout: "{{ wait_timeout }}"
- debug:
msg: "A:{{ inventory_hostname }}
{{ lookup('pipe', 'date +%H-%M-%S') }}
finished"


$ ansible-playbook pinned-01.yml -f 3 | grep msg\":
"msg": "A:test_06 15-33-05 started"
"msg": "A:test_09 15-33-05 started"
"msg": "A:test_01 15-33-05 started"
"msg": "A:test_01 15-33-08 finished"
"msg": "A:test_02 15-33-08 started"
"msg": "A:test_06 15-33-11 finished"
"msg": "A:test_03 15-33-11 started"
"msg": "A:test_02 15-33-11 finished"
"msg": "A:test_09 15-33-12 finished"
"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.


--
Vladimir Botka

Igor Gnatenko

unread,
Aug 9, 2020, 1:49:00 PM8/9/20
to Vladimir Botka, ansible...@googlegroups.com
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!
--
-Igor Gnatenko
Reply all
Reply to author
Forward
0 new messages