with 项目 - 预定义的字典

可以使用字典创建更复杂的循环。

来自 vars:

packages:
  - present: tree
  - present: nmap
  - absent: apache2

然后循环:

- name: manage packages
  package: name={{ item.value }} state={{ item.key }}
  with_items: '{{ packages }}'

或者,如果你不喜欢使用键值:

瓦尔:

packages:
  - name: tree
    state: present
  - name: nmap
    state: present
  - name: apache2
    state: absent

然后循环:

- name: manage packages
  package: name={{ item.name }} state={{ item.state }}
  with_items: '{{ packages }}'