Back to .md Directory

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.

May 2, 2026
0 downloads
0 views
ai
View source

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

  1. YAML Format: Playbooks are written in YAML, which uses indentation to define structure. This makes them human-readable.

  2. 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.

  3. 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.).

  4. Variables: You can define variables in playbooks to make them more dynamic and reusable.

  5. 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:

  1. ---: This indicates the start of a YAML document.

  2. name: A description of what the playbook does.

  3. hosts: Specifies the group of hosts from the inventory file that this play will run on (in this case, webservers).

  4. become: yes: This tells Ansible to run the tasks with elevated privileges (like using sudo).

  5. tasks: This section contains the individual tasks to be executed.

    • First Task:

      • Installs nginx using the apt module (for Debian-based systems).
      • state: present means that Ansible will ensure nginx is installed.
    • Second Task:

      • Starts the nginx service using the service module.
      • enabled: yes ensures that the service starts on boot.

How to Use a Playbook in Ansible

  1. Create a Playbook File: Save your playbook in a file with a .yml or .yaml extension, e.g., install_nginx.yml.

  2. Run the Playbook: Use the ansible-playbook command 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:
  1. present: Ensures that a resource (like a package, user, or file) exists. For example, installing a package.

  2. absent: Ensures that a resource does not exist. For example, removing a package or deleting a user.

  3. started: For services, this state ensures that the service is running.

  4. stopped: For services, this state ensures that the service is not running.

  5. restarted: For services, this state indicates that the service should be restarted. (Restart a service.)

  6. enabled: For services, this state ensures that the service is enabled to start at boot.

  7. disabled: For services, Make sure a service does not start automatically when the system boots up.

  8. latest: This state is used with the apt module 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