Playbook in Ansible:
A **playbook** in Ansible is a file that contains a series of instructions (or "plays") that define what tasks to perform on your managed hosts. Playbooks are written in YAML (Yet Another Markup Language), which is easy to read and write. They allow you to automate complex tasks and configurations across multiple machines.
Playbook in Ansible:
A playbook in Ansible is a file that contains a series of instructions (or "plays") that define what tasks to perform on your managed hosts. Playbooks are written in YAML (Yet Another Markup Language), which is easy to read and write. They allow you to automate complex tasks and configurations across multiple machines.
Key Concepts of Ansible Playbooks
-
YAML Format: Playbooks are written in YAML, which uses indentation to define structure. This makes them human-readable.
-
Plays: A play is a mapping between a group of hosts and the tasks that should be run on those hosts. You can have multiple plays in a single playbook.
-
Tasks: Tasks are the individual actions that Ansible will perform. Each task typically calls an Ansible module (like installing a package, copying a file, etc.).
-
Variables: You can define variables in playbooks to make them more dynamic and reusable.
-
Handlers: Handlers are special tasks that only run when notified by other tasks. They are often used for actions that should only happen if a change occurs, like restarting a service.
Example of a Simple Playbook
Here’s an easy example of an Ansible playbook that installs nginx on a group of web servers:
---
- name: Install Nginx on web servers
hosts: webservers
become: yes # Use 'sudo' to run tasks as a superuser
tasks:
- name: Ensure Nginx is installed
apt:
name: nginx
state: present
- name: Start Nginx service
service:
name: nginx
state: started
enabled: yes
Breakdown of the Example:
-
---: This indicates the start of a YAML document. -
name: A description of what the playbook does. -
hosts: Specifies the group of hosts from the inventory file that this play will run on (in this case,webservers). -
become: yes: This tells Ansible to run the tasks with elevated privileges (like usingsudo). -
tasks: This section contains the individual tasks to be executed.-
First Task:
- Installs
nginxusing theaptmodule (for Debian-based systems). state: presentmeans that Ansible will ensurenginxis installed.
- Installs
-
Second Task:
- Starts the
nginxservice using theservicemodule. enabled: yesensures that the service starts on boot.
- Starts the
-
How to Use a Playbook in Ansible
-
Create a Playbook File: Save your playbook in a file with a .yml or .yaml extension, e.g., install_nginx.yml.
-
Run the Playbook: Use the
ansible-playbookcommand to execute the playbook. You can specify the inventory file if it's not the default.
ansible-playbook -i inventory_file.ini install_nginx.yml
In an Ansible playbook file, the state parameter is used to define the desired state of a resource.
- Here are some common types of states you might encounter:
-
present: Ensures that a resource (like a package, user, or file) exists. For example, installing a package.
-
absent: Ensures that a resource does not exist. For example, removing a package or deleting a user.
-
started: For services, this state ensures that the service is running.
-
stopped: For services, this state ensures that the service is not running.
-
restarted: For services, this state indicates that the service should be restarted. (Restart a service.)
-
enabled: For services, this state ensures that the service is enabled to start at boot.
-
disabled: For services, Make sure a service does not start automatically when the system boots up.
-
latest: This state is used with the
aptmodule to ensure that the package is at the latest version available. Make sure the newest version of a package is installed.
Summary
A playbook is a YAML file that contains a series of plays, which define tasks to be executed on hosts. Playbooks allow you to automate complex configurations and tasks across multiple servers. You can define tasks, use variables, and manage service states within your playbooks. By using playbooks, you can efficiently manage and automate your infrastructure with Ansible.
Related Documents
Visual Truth Engine: Product-Market Fit & Go-to-Market Strategy
**Date:** January 22, 2026 | **Status:** Early-Stage Launch Strategy
Media Handling Playbook - Zyeuté v3
**Last Updated:** December 15, 2025
Trader ROI Playbook (Codex + CI)
Purpose: increase engineering output per hour while keeping quality stable or better.
OSCP Attack Playbook
**Author:** Brad Turner