Sobes.tech
Senior

What is the difference between a playbook and Ansible roles?

sobes.tech AI

Answer from AI

Playbook in Ansible is a file (usually in YAML format) that describes a sequence of tasks to be executed on target hosts. The playbook defines what needs to be done and in what order, for example, installing packages, copying files, starting services.

Ansible roles are a way to structure and organize playbook code. Roles allow breaking down configuration into logical parts with a clear directory structure (tasks, handlers, templates, vars, etc.). This makes it easier to reuse, maintain, and scale the code.

In simpler terms:

  • Playbook is a script of tasks to be executed.
  • Role is a module that encapsulates a set of related tasks and resources, which can be included in a playbook.

Example of using a role in a playbook:

- hosts: webservers
  roles:
    - nginx

Here, the nginx role contains all the necessary tasks for installing and configuring nginx, and the playbook simply includes this role.