Ansible提供了多种循环控制的方式,其中包括使用with_items
循环和with_together
循环。
with_items
循环示例:- name: 使用with_items循环
hosts: localhost
gather_facts: False
tasks:
- name: 打印列表中的元素
debug:
msg: "{{ item }}"
with_items:
- item1
- item2
- item3
with_together
循环示例:- name: 使用with_together循环
hosts: localhost
gather_facts: False
vars:
list1:
- item1
- item2
- item3
list2:
- value1
- value2
- value3
tasks:
- name: 打印两个列表中对应位置的元素
debug:
msg: "{{ item.0 }} - {{ item.1 }}"
with_together:
- "{{ list1 }}"
- "{{ list2 }}"
以上示例中,with_items
循环用于遍历一个列表中的元素,with_together
循环用于同时遍历两个列表中对应位置的元素。在每次循环中,item
变量都会被设置为当前的元素。
请注意,上述示例中的任务仅用于演示目的,实际使用时可以根据具体需求编写更复杂的任务。
上一篇:Ansible循环获取下一个值
下一篇:Ansible循环列表和范围