要在Ansible的同一任务中循环遍历不同的项,可以使用Ansible的with_items和nested的功能。
下面是一个示例代码,演示如何在同一个任务中循环遍历不同的项:
- name: Loop through different items in the same task
hosts: localhost
gather_facts: false
vars:
items_a:
- item_a1
- item_a2
items_b:
- item_b1
- item_b2
tasks:
- name: Loop through items_a
debug:
msg: "{{ item }}"
with_items: "{{ items_a }}"
- name: Loop through items_b
debug:
msg: "{{ item }}"
with_items: "{{ items_b }}"
在上述示例中,我们定义了两个变量items_a
和items_b
,分别包含不同的项。然后,在同一个任务中,我们使用with_items来循环遍历这些项,并使用debug模块输出每个项的值。
运行上述代码示例,将会依次输出item_a1
、item_a2
、item_b1
和item_b2
。