gpt4 book ai didi

ansible - 在完成一台主机而非所有 fork 主机后,为下一台主机执行 ansible 剧本

转载 作者:行者123 更新时间:2023-12-04 15:35:40 26 4
gpt4 key购买 nike

通过 fork 变量,我可以告诉 ansible 应该并行执行多少台主机。当之前的所有主机完成时,将执行下一个主机。如果主机上的执行时间不同,我必须等待最后一台主机完成。

是否可以告诉 ansible 在一个主机完成后开始下一个主机的下一个播放,而不是全部运行。

换句话说,我有数百个客户,播放时间为 5 分钟,而一些主持人需要 30 分钟。我们测试了 fork 的数量,发现 50 是一个很好的值。有时,接下来的 50 个必须等待一个主机 25 分钟。

最佳答案

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

:是的。这是可能的。策略插件host_pinned是你要找的。引用:

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.

示例

让我们创建一个 list

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

下面的剧本

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"

给予

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"

结果

因为 -f 3 Ansible 启动了 3 个主机 (1,9,6)。主机 1 首先完成并向等待启动的新主机打开其插槽。主机 2 启动。与主机 6 完成后主机 3 立即启动的方式相同。主机 2 在主机 9 之后开始,在主机 9 之前完成。

关于ansible - 在完成一台主机而非所有 fork 主机后,为下一台主机执行 ansible 剧本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59877712/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com