Middle
What is the difference between the role and playbook in Ansible?
sobes.tech AI
Answer from AI
In Ansible, a playbook is the main file that describes a sequence of tasks to be executed on target hosts. A playbook contains plays, where hosts, roles or tasks, variables, and other parameters are specified.
Role is a structured way to organize tasks, files, templates, and variables, allowing for reuse and division of configuration into logical blocks. A role typically contains directories such as tasks, handlers, templates, files, vars, and meta.
In simpler terms:
- A playbook describes what and where to do — the sequence of actions on hosts.
- A role is how to implement part of the configuration, a module with a set of tasks and resources that can be included in a playbook.
Example of using a role in a playbook:
- hosts: webservers
roles:
- nginx
Here, the playbook calls the nginx role, which contains all the necessary tasks and files for installing and configuring Nginx.