Loading

Quipoin Menu

Learn • Practice • Grow

ansible / What are Playbooks?
tutorial

What are Playbooks?

Ad‑hoc commands are useful for one‑off tasks, but for repeatable, complex automation, you need playbooks. A playbook is a YAML file that defines a set of tasks to run on managed hosts.

Playbooks are the heart of Ansible. They are human‑readable, idempotent, and can orchestrate multi‑server deployments.

A playbook is a blueprint for automation – a list of plays, each mapping hosts to tasks.

What Is a Play?

A playbook contains one or more plays. Each play targets a set of hosts and defines tasks to execute on them. For example, one play might configure web servers, another play might configure database servers.

Your First Playbook

Create a file called first-playbook.yml:
---
- name: My first play
hosts: all
tasks:
- name: Print a message
debug:
msg: "Hello from Ansible playbook!"
  • --- indicates start of YAML document.
  • - name describes the play (optional but recommended).
  • hosts: all runs on every host in the inventory.
  • tasks is a list of actions.
  • debug module prints a message.

Running a Playbook

Use the ansible-playbook command:
ansible-playbook -i inventory.ini first-playbook.yml
You’ll see output showing each task’s status (ok, changed, failed).

Playbook Execution Flow

Ansible runs tasks sequentially from top to bottom. If any task fails, the play stops (unless you configure error handling). Each task calls a module with specific arguments.

Idempotency in Playbooks

Most Ansible modules are idempotent. Running the same playbook multiple times results in the same final state. For example, installing a package only runs if it’s not already installed.


Two Minute Drill
  • Playbooks are YAML files containing one or more plays.
  • Each play targets hosts and defines tasks.
  • Run playbooks with ansible-playbook.
  • Tasks are idempotent – safe to run repeatedly.

Need more clarification?

Drop us an email at career@quipoinfotech.com