When a task has both loop and when, how often is the condition evaluated? Explain with "install MariaDB if the root filesystem has enough free space".
when is evaluated separately for every item; the module runs only for the items where the condition is true, and the others are skipped.
* With a loop, when is a per-item filter: one evaluation per element. *
- name: install mariadb-server if enough space on root
ansible.builtin.dnf:
name: mariadb-server
state: latest
loop: "{{ ansible_facts['mounts'] }}"
when: item['mount'] == "/" and item['size_available'] > 300000000
The fact ansible_facts['mounts'] is a list of dictionaries, one per mounted filesystem, with keys such as mount (the mount point) and size_available (free space in bytes). The loop walks through every filesystem; for each one, when checks both parts. Only the dictionary that is / and has enough free space passes, so dnf runs at most once; every other filesystem shows up as a skipped item.
Note the unit: 300000000 bytes is 300 MB, about 286 MiB. For exactly 300 MiB you would write 300 * 1024 * 1024 (314572800).
Go deeper:
Using conditionals in loops (official docs) โ per-item evaluation, with this mounts example.