0Pricing
Ansible Academy · Lesson

Group Tasks with block

Bundle tasks for shared behavior.

Group Tasks with block is a free Ansible Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ansible Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Tasks That Belong Together

Some tasks naturally form a unit. The block keyword groups several tasks so they can share settings and behavior. 📦

What a block Looks Like

A block is a list of tasks nested under one block key. They run top to bottom, just like normal tasks.

- name: Set up the app
  block:
    - name: Install package
      ansible.builtin.package:
        name: nginx
    - name: Start service
      ansible.builtin.service:
        name: nginx
        state: started

Share Directives Once

Directives set on a block apply to every task inside it. Set become or when once instead of repeating it per task.

- block:
    - ansible.builtin.package:
        name: nginx
    - ansible.builtin.service:
        name: nginx
        state: started
  become: true

A Shared when Condition

Put when on the block and Ansible applies it to all child tasks. The whole group runs or skips together.

- block:
    - ansible.builtin.package:
        name: httpd
    - ansible.builtin.service:
        name: httpd
        state: started
  when: ansible_os_family == "RedHat"

Tags on a Whole Block

A tag on the block tags every task within it. One label lets you run or skip the entire group with --tags.

- block:
    - ansible.builtin.package:
        name: nginx
  tags:
    - webserver

Blocks Can Be Named

Give a block a name for readable output. The name appears in the run log, framing the tasks it contains.

- name: Configure web server
  block:
    - ansible.builtin.template:
        src: nginx.conf.j2
        dest: /etc/nginx/nginx.conf

Blocks Are Not Loops

A block groups tasks; it does not repeat them. To run a block many times you still loop on the individual tasks inside it.

Inheritance, Not Override

Block directives are inherited by tasks. A task can still set its own when, which combines with the block condition with logical and.

Nesting Blocks

Blocks can contain other blocks. Nesting lets you scope settings tightly, but keep it shallow so the playbook stays readable.

The Real Power: Error Handling

Grouping is handy, but blocks shine paired with rescue and always. They turn a group into a try and catch unit, coming up next. 🛟

Where Blocks Live

A block sits in the tasks list of a play, exactly where a single task would. Anywhere a task is valid, a block is too.

Quick Check

Let us check what block does.

Recap

A block groups tasks under one key so they share directives like become, when and tags, and unlock error handling. ✅

Frequently asked questions

Is the “Group Tasks with block” lesson free?

Yes — the full text of “Group Tasks with block” is free to read here on the web, and the Ansible Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ansible Academy course, upgrade to CoddyKit PRO.

What will I learn in “Group Tasks with block”?

Bundle tasks for shared behavior. You practise Ansible Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Ansible Academy?

No prior experience is required. Ansible Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Group Tasks with block” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Ansible Academy lesson?

Yes. Every Ansible Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Group Tasks with block
  2. Try / Catch with rescue & always
  3. Control Failure: failed_when & ignore_errors
  4. Custom Assertions with the fail Module
← Back to Ansible Academy