Junior — Middle
Can you explain what is meant by a role in Ansible and how it is used?
sobes.tech AI
Answer from AI
In Ansible, a role is a way to structure and reuse configurations and tasks. A role combines related tasks, files, templates, variables, and handlers into a separate directory with a specific structure.
Using roles allows:
- Organizing code into functional blocks.
- Reusing configurations across different playbooks.
- Simplifying maintenance and scaling of infrastructure.
Example of a role structure:
roles/
myrole/
tasks/
main.yml
handlers/
main.yml
templates/
config.j2
files/
script.sh
vars/
main.yml
In a playbook, a role is included as follows:
- hosts: servers
roles:
- myrole
Thus, a role encapsulates all necessary elements for configuring a specific part of the system, making playbooks cleaner and easier to maintain.