0Pricing
Ansible Academy · Lesson

Multiple Plays in One Playbook

Target web then db in a single file.

Multiple Plays in One Playbook is a free Ansible Academy lesson on CoddyKit — lesson 3 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.

More Than One Play

A playbook is a YAML list, so it can hold multiple plays. Each play targets its own host group. 🎭

Why Split Into Plays

Different tiers need different work. One play sets up web servers, another configures the database hosts.

Each Play Starts With a Dash

A new dash at the top level begins a new play. That is how Ansible knows one play ended and the next started.

- name: Web play
  hosts: web
  tasks: []

- name: DB play
  hosts: db
  tasks: []

Web Then DB

Plays run in file order. The web play finishes completely before the db play starts.

Independent hosts Lines

Each play has its own hosts value. They can point to totally separate groups with no overlap.

- hosts: web
  tasks: []
- hosts: db
  tasks: []

Separate Settings Per Play

Play-level keys like become or vars apply only to that play. The next play starts with its own clean settings.

Facts Per Play

By default each play gathers facts for its own hosts before tasks run, so the db play sees db machine data.

Orchestrating a Stack

Multiple plays let one file orchestrate a whole stack: configure the database, then the app, then the load balancer. 🏗️

A Three-Play File

Here three plays target three groups in sequence. One command rolls out the entire environment.

- hosts: db
  tasks: []
- hosts: app
  tasks: []
- hosts: lb
  tasks: []

One Command, Many Tiers

You still run it with a single ansible-playbook call. Ansible walks every play for you, top to bottom.

Keep Plays Focused

Give each play one clear job and one host group. Focused plays keep a large playbook easy to read and reuse.

Quick Check

Let us confirm how multiple plays execute.

Recap

One file can hold many plays, each with its own hosts and settings. They run in order to orchestrate a full stack. ✅

Frequently asked questions

Is the “Multiple Plays in One Playbook” lesson free?

Yes — the full text of “Multiple Plays in One Playbook” 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 “Multiple Plays in One Playbook”?

Target web then db in a single file. 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 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Multiple Plays in One Playbook” 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