我正在用ansible同时更新几个主机,但是我有一个限制…
我必须从一个通用的存储库中下载工件,同时下载不超过3次!
我目前的解决方案是将整个攻略限制为最多三个并发任务
strategy: linear
serial: 3
是否可以仅限制特定任务步骤的并发性,而不是整个行动手册?
发布于 2018-02-06 20:37:06
没有直接的方法。只有像run_once
这样的变通方法才能使用delegate_to
循环,或者使用循环乘以任务,并在每个主机上只执行一个项目。
参见issue #12170,关闭状态为with‘t fix。
delegate_to
循环:
- mytask: ..
delegate_to: "{{item}}"
run_once: true
# many diff ways to make the loop
with_inventory_hostnames: all
乘以任务:
- name: target task
debug: msg="Performing task on {{ inventory_hostname }}, item is {{ item }}"
with_items: "{{ play_hosts }}"
when: "hostvars[item].inventory_hostname == inventory_hostname"
发布于 2020-08-19 02:28:15
是的,可以只限制某个任务的并发性。
您只需将throttle
keyword添加到您的下载任务中。
示例:
- name: Download payload.tar.gz
get_url:
url: https://example.com/path/payload.tar.gz
dest: /mnt/scratch/payload.tar.gz
mode: '0640'
throttle: 3
请注意,Ansible 2.9中引入了throttle
。
发布于 2018-02-07 21:08:01
多亏了之前的评论和#12170,我提出了我的建议,但对于下载案例来说,我的建议仍然没有达到预期的效果。
请注意,2是所需的并发任务执行的最大数量。
- name: Download at ratio three at most
win_get_url:
url: http://ipv4.download.thinkbroadband.com/100MB.zip
dest: c:/ansible/100MB.zip
force: yes
with_sequence: start=0 end={{ (( play_hosts | length ) / 2 ) | round (0, 'floor') | int }}
when: "(( ansible_play_batch.index(inventory_hostname) % (( play_hosts | length ) / 2 )) | round) == (item | int)"
虽然这将匹配每次迭代的when
,但对于某些主机,我仍然可以看到所有服务器同时执行下载。
另一种测试方法是调试一条消息,并在迭代之间添加延迟。这种方式很清楚,每次迭代只执行两个。
- debug:
msg: "Item {{ item }} with modulus {{ (( ansible_play_batch.index(inventory_hostname) % (( play_hosts | length ) / 2 )) | round) }}"
with_sequence: start=0 end={{ (( play_hosts | length ) / 2 ) | round (0, 'floor') | int }}
loop_control:
pause: 2
when: "(( ansible_play_batch.index(inventory_hostname) % (( play_hosts | length ) / 2 )) | round) == (item | int)"
https://stackoverflow.com/questions/48642853
复制相似问题