0Pricing
Ansible Academy · Lesson

A Play = Hosts + Tasks

How a play maps tasks onto a host group.

A Play = Hosts + Tasks 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.

Meet the Play

A play is the heart of a playbook. It maps a set of tasks onto a group of hosts and runs them there. 🎯

Two Halves of a Play

Every play answers two questions: which machines, and what to do. That is exactly hosts plus tasks.

The hosts Key

The hosts key picks the target. It can be one host, a group from your inventory, or the special pattern all.

- name: Configure web servers
  hosts: web

The tasks List

Under tasks you list the actions to run, in order. Each task is one item that calls a module.

  tasks:
    - name: Install nginx
      ansible.builtin.package:
        name: nginx

A Play Is a List Item

A playbook is a YAML list of plays. The dash before name marks where each play begins.

- name: My first play
  hosts: all
  tasks: []

One Play, Many Hosts

The same task list runs on every host in the group. Ansible fans it out so ten servers get the identical configuration.

Each Task Calls a Module

A task is just a module call with arguments. The module does the real work, like installing a package or copying a file.

Putting It Together

Hosts on top, tasks below: that single block is a complete, runnable play. 🧩

- name: Web setup
  hosts: web
  tasks:
    - name: Ensure nginx is present
      ansible.builtin.package:
        name: nginx

Play-Level Settings

Keys beside hosts apply to the whole play. For example become tells every task in it to run with elevated privileges.

- hosts: web
  become: true
  tasks: []

Why This Mapping Matters

Separating hosts from tasks lets you reuse the same actions on different fleets just by changing the hosts line.

Plays Stack Up

One file can hold several plays. Ansible runs them top to bottom, so you can target web first, then db.

Quick Check

Let us make sure the core mapping is clear.

Recap

A play is hosts plus tasks: pick the machines, list the actions. Stack plays in one file to automate a whole fleet. ✅

Frequently asked questions

Is the “A Play = Hosts + Tasks” lesson free?

Yes — the full text of “A Play = Hosts + Tasks” 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 “A Play = Hosts + Tasks”?

How a play maps tasks onto a host group. 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 “A Play = Hosts + Tasks” 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. A Play = Hosts + Tasks
  2. Naming Tasks for Readable Output
  3. Multiple Plays in One Playbook
  4. Task Order & Top-Down Execution
← Back to Ansible Academy