在Ansible中,可以使用循环遍历字典和列表。以下是一些示例代码:
- name: Loop through a dictionary
hosts: localhost
vars:
my_dict:
key1: value1
key2: value2
key3: value3
tasks:
- name: Print key-value pairs
debug:
msg: "Key: {{ item.key }}, Value: {{ item.value }}"
loop: "{{ my_dict | dict2items }}"
- name: Loop through a list
hosts: localhost
vars:
my_list:
- item1
- item2
- item3
tasks:
- name: Print items
debug:
msg: "Item: {{ item }}"
loop: "{{ my_list }}"
- name: Loop through a dictionary with a list
hosts: localhost
vars:
my_dict:
key1:
- item1
- item2
key2:
- item3
- item4
tasks:
- name: Print key and items
debug:
msg: "Key: {{ item.key }}, Items: {{ item.value }}"
loop: "{{ my_dict | dict2items }}"
这些示例代码演示了如何使用Ansible循环遍历字典和列表,并打印出相应的键、值或项。你可以根据自己的需求进行修改和扩展。
上一篇:Ansible循环遍历字典
下一篇:Ansible循环并打印字典变量