我有以下 Ansible 剧本(我使用的是 2.9.21),它工作得很好,正如我希望的那样:
- name: Sync package_00 package to the remote hosts
ansible.builtin.synchronize:
src: /home/ansible/files/package_00
dest: /tmp/
recursive: false
archive: false
checksum: true
delete: false
- name: Sync package_01 package to the remote hosts
ansible.builtin.synchronize:
src: /home/ansible/files/package_01
dest: /tmp/
recursive: false
archive: false
checksum: true
delete: false
- name: Sync package_02 package to the remote hosts
ansible.builtin.synchronize:
src: /home/ansible/files/package_02
dest: /tmp/
recursive: false
archive: false
checksum: true
delete: false
它正确地抓取第一个任务并将第一个文件 package_00 复制到所有指定的主机。一旦第一个文件准备好,它就会启动第二个文件。
现在,由于我有大量文件,并且上面提到的示例非常丑陋,因此最好有一些循环或 with_items 解决方案,但不幸的是我无法实现与所描述的完全相同的功能。每当我更改为类似的内容时,执行都完全不同:
- name: Sync the packages to the remote host
ansible.builtin.synchronize:
src: /home/ansible/files/package_{{ item }}
dest: /tmp/
recursive: false
archive: false
checksum: true
delete: false
loop: "{{ range(0, 100) | list }}"
现在 Ansible 尝试将所有文件复制到第一台主机,然后将所有文件复制到第二台主机,依此类推。
我尝试过使用loop、with_items、serial关键字进行实验,但没有成功。
由于显着的开销、性能下降和运行时间增加,以下最小示例展示了如何实现此类行为。
库存
example.ini
[managed]
remote.example.com
managed.example.com
external.example.com
任务文件
debug.yml
- name: "Loop included {{ item }}"
debug:
msg: "package_{{ ansible_loop.index0 }}"
剧本
main.yml
---
- hosts: managed
become: false
gather_facts: false
tasks:
- name: Loop directly
debug:
msg: "package_{{ item }}"
loop: "{{ range(0, 3) | list }}"
- name: Include
include_tasks:
file: debug.yml
loop: "{{ ansible_play_hosts }}"
loop_control:
extended: true
通过
致电ansible-playbook --inventory example.ini main.yml
结果输出和运行时间
PLAY [managed] *****************************************************************************************************
Friday 04 October 2024 18:46:44 +0200 (0:00:00.027) 0:00:00.027 ********
TASK [Loop directly] ***********************************************************************************************
ok: [remote.example.com] => (item=0) =>
msg: package_0
ok: [remote.example.com] => (item=1) =>
msg: package_1
ok: [remote.example.com] => (item=2) =>
msg: package_2
ok: [managed.example.com] => (item=0) =>
msg: package_0
ok: [managed.example.com] => (item=1) =>
msg: package_1
ok: [external.example.com] => (item=0) =>
msg: package_0
ok: [managed.example.com] => (item=2) =>
msg: package_2
ok: [external.example.com] => (item=1) =>
msg: package_1
ok: [external.example.com] => (item=2) =>
msg: package_2
Friday 04 October 2024 18:46:44 +0200 (0:00:00.065) 0:00:00.093 ********
TASK [Include] *****************************************************************************************************
included: debug.yml for remote.example.com, managed.example.com, external.example.com => (item=remote.example.com)
included: debug.yml for remote.example.com, managed.example.com, external.example.com => (item=managed.example.com)
included: debug.yml for remote.example.com, managed.example.com, external.example.com => (item=external.example.com)
Friday 04 October 2024 18:46:45 +0200 (0:00:00.048) 0:00:00.141 ********
TASK [Loop included remote.example.com] ****************************************************************************
ok: [remote.example.com] =>
msg: package_0
ok: [managed.example.com] =>
msg: package_0
ok: [external.example.com] =>
msg: package_0
Friday 04 October 2024 18:46:45 +0200 (0:00:00.037) 0:00:00.179 ********
TASK [Loop included managed.example.com] ***************************************************************************
ok: [remote.example.com] =>
msg: package_1
ok: [managed.example.com] =>
msg: package_1
ok: [external.example.com] =>
msg: package_1
Friday 04 October 2024 18:46:45 +0200 (0:00:00.036) 0:00:00.215 ********
TASK [Loop included external.example.com] **************************************************************************
ok: [remote.example.com] =>
msg: package_2
ok: [managed.example.com] =>
msg: package_2
ok: [external.example.com] =>
msg: package_2
PLAY RECAP *********************************************************************************************************
external.example.com : ok=7 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
managed.example.com : ok=7 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
remote.example.com : ok=7 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Playbook run took 0 days, 0 hours, 0 minutes, 0 seconds
Friday 04 October 2024 18:46:45 +0200 (0:00:00.067) 0:00:00.283 ********
===============================================================================
Loop included external.example.com --------------------------------------------------------------------------- 0.07s
Loop directly ------------------------------------------------------------------------------------------------ 0.07s
Include ------------------------------------------------------------------------------------------------------ 0.05s
Loop included remote.example.com ----------------------------------------------------------------------------- 0.04s
Loop included managed.example.com ---------------------------------------------------------------------------- 0.04s