bactch是将列表拆分成多个指定大小的子列表。
---
- hosts: localhost
gather_facts: no
tasks:
- name: use debug to view the vault values
vars:
sleep_durations:
- 1
- 2
- 3
- 4
- 5
durations: "{{ item }}"
debug:
msg: "{{ durations }}"
loop:
- "{{ sleep_durations | batch(2) | list }}"
TASK [use debug to view the vault values] *************************************************************
ok: [localhost] => (item=[[1, 2], [3, 4], [5]]) => {
"msg": [
[
1,
2
],
[
3,
4
],
[
5
]
]
}