LOGBOOK

HELP

Quiz Entry - updated: 2026.11.12

What is an Ansible block, and how does a when on a block work?

A block groups several tasks into one unit; keywords set on the block, such as when, apply to every task inside it.

- name: block example
  hosts: all
  tasks:
    - name: installing and configuring DNF versionlock plugin
      block:
        - name: package needed by dnf
          ansible.builtin.dnf:
            name: python3-dnf-plugin-versionlock
            state: present
        - name: lock version of tzdata
          ansible.builtin.lineinfile:
            dest: /etc/yum/pluginconf.d/versionlock.list
            line: tzdata-2016j-1
            state: present
      when: ansible_distribution == "RedHat"

Both tasks only make sense on Red Hat systems. Without a block, the same when would have to be repeated on each task, and someone adding a third task could forget it. With a block, the condition is written once at the block's level (aligned with block:, not inside it) and every task in the group inherits it. Other keywords inherit the same way, for example become.

The condition is still evaluated for each task in the block, not once for the whole group. That matters if a task inside the block changes a value the condition depends on.

Go deeper:

From Quiz: ITIA / Ansible Task Control: Loops, Conditionals, Handlers and Error Handling | Updated: Nov 12, 2026