Sobes.tech
Junior

What is the difference between roles and playbooks in DevOps?

sobes.tech AI

Answer from AI

Role — is a set of files describing the state of the system to achieve a specific goal. It is a unified, reusable structure.

Playbook — is a YAML file describing a list of tasks to be executed on a specific group of hosts. It can use one or more roles.

Main differences:

  • Structure and reusability: Roles provide a standardized structure for organizing tasks and can be easily reused in different playbooks. Playbooks, on the other hand, describe a specific set of actions to achieve a particular state.
  • Organization: Roles break down complex configurations into smaller, manageable parts (tasks, handlers, vars, files, templates). Playbooks combine these parts (roles or individual tasks) to execute a scenario.
  • Purpose: A role aims to achieve a specific state of a component or service (e.g., installing and configuring a web server), while a playbook is for executing a complete deployment or system configuration process.
Characteristic Role Playbook
Type Directory and file structure YAML file
Purpose Organizing and reusing tasks Executing a scenario, combining tasks/roles
Organization Separation by components (tasks, vars) List of tasks, host groups, and roles
Reusability High Low (for a specific scenario)

Example of using roles in a playbook:

---
# playbook.yml
- name: Deploy web application
  hosts: web_servers
  roles:
    - common
    - web_server
    - application
  vars:
    app_version: 1.2.0

Example of the structure of the "web_server" role:

web_server/
├── defaults/
│   └── main.yml  # Default variable values
├── handlers/
│   └── main.yml  # Event handlers (restart service)
├── tasks/
│   └── main.yml  # Main tasks of the role
├── templates/
│   └── nginx.conf.j2 # Template files with variables
├── files/
│   └── index.html # Static files
└── meta/
    └── main.yml # Role dependencies