Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

execute ansible playbook for next host after finishing one host not all forked hosts

Tags:

fork

ansible

With the fork variable I can tell ansible how many hosts should pe executed in parallel. The next hosts are executed when all hosts from before finished. In case the execution time differs on the hosts, I have to wait for the last host finishing.

Is it possible to tell ansible start the next play for one host after one has finished, not all running.

In other words, I have hundreds clients with a play running at 5 minutes and some hosts need 30 minutes. We tested the amount of fork and found that 50 is a good value. Sometimes the next 50 had to wait for one host 25 minutes.

like image 339
hen ning Avatar asked Oct 15 '25 16:10

hen ning


1 Answers

Q: "Is it possible to tell Ansible to start the next play for one host after one has finished?"

A: Yes. It's possible. The strategy plugin host_pinned is what you're looking for. Quoting:

Ansible will not wait for other hosts to finish the current task before queuing the next task for a host that has finished. Once a host is done with the play, it opens its slot to a new host that was waiting to start.

Example

Lets' create an inventory

shell> 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

The playbook below

shell> 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"

gives

shell> 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 its 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.

like image 57
Vladimir Botka Avatar answered Oct 19 '25 08:10

Vladimir Botka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!