在Ansible中,可以使用'with_items'关键字来创建一个列表。以下是一个示例代码:
- name: Create a list using with_items
hosts: localhost
gather_facts: false
vars:
my_list:
- item1
- item2
- item3
tasks:
- name: Print the list
debug:
var: item
with_items: "{{ my_list }}"
在上面的示例中,我们定义了一个名为'my_list'的变量,其中包含三个元素。然后,在tasks部分中使用'with_items'关键字来遍历该列表,并在每次迭代中打印出当前的元素。
运行上述代码,将会输出以下结果:
TASK [Print the list] ***************************************
ok: [localhost] => (item=item1) => {
"item": "item1"
}
ok: [localhost] => (item=item2) => {
"item": "item2"
}
ok: [localhost] => (item=item3) => {
"item": "item3"
}
可以看到,使用'with_items'关键字成功创建了一个列表,并且在每次迭代中都打印出了相应的元素。