Sobes.tech
Senior

What is a role in Ansible? What does it contain?

sobes.tech AI

Answer from AI

In Ansible, a role is a way to organize and reuse configurations and tasks. A role contains a set of files and directories organized in a specific structure to automate the setup of a particular component or service.

Typical contents of a role:

  • tasks/ — main tasks (playbook) that the role performs.
  • handlers/ — handlers called upon changes (e.g., restarting a service).
  • templates/ — template files with variables that are substituted during deployment.
  • files/ — static files copied to target machines.
  • vars/ — variables specific to the role.
  • defaults/ — default variable values.
  • meta/ — role metadata, including dependencies on other roles.

Roles allow breaking down complex playbooks into modules, improving readability, support, and reusability of code.

Example of a role structure:

myrole/
├── tasks/
│   └── main.yml
├── handlers/
│   └── main.yml
├── templates/
│   └── config.j2
├── files/
│   └── script.sh
├── vars/
│   └── main.yml
├── defaults/
│   └── main.yml
└── meta/
    └── main.yml