ansible Ansible: Looping with_items - predefined dictionary


Example

It is possible to create more complex loops with dictionaries.

From vars:

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

then the loop:

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

Or, if you don't like to use the key value:

vars:

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

then the loop:

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