在Ansible中,可以使用with_nested
和with_together
两个循环控制结构来处理多个with序列。
with_nested
循环:使用with_nested
循环可以嵌套遍历多个序列,并将它们的元素传递给任务。下面是一个示例:- name: Example with_nested loop
hosts: localhost
vars:
fruits: ['apple', 'banana']
colors: ['red', 'yellow']
tasks:
- name: Print fruit and color
debug:
msg: "Fruit: {{ item[0] }}, Color: {{ item[1] }}"
with_nested:
- "{{ fruits }}"
- "{{ colors }}"
with_together
循环:使用with_together
循环可以同时遍历多个序列,并将它们的元素传递给任务。下面是一个示例:- name: Example with_together loop
hosts: localhost
vars:
fruits: ['apple', 'banana']
colors: ['red', 'yellow']
tasks:
- name: Print fruit and color
debug:
msg: "Fruit: {{ item.0 }}, Color: {{ item.1 }}"
with_together:
- "{{ fruits }}"
- "{{ colors }}"
以上两种循环控制结构都可以用来处理多个with序列,只需根据具体情况选择适合的方式即可。
下一篇:Ansible循环与列表