Sobes.tech
Junior — Middle

Can you explain what a role in Ansible is and how it is used in configuration automation?

sobes.tech AI

Answer from AI

The role in Ansible is a way to structure and reuse configurations and tasks. A role combines related playbooks, variables, templates, files, and handlers into a separate directory with a specific structure.

Using roles allows:

  • To organize code more modularly and clearly.
  • To easily reuse configurations across different projects.
  • To divide responsibilities among teams.

Example of a role structure:

roles/
  webserver/
    tasks/main.yml
    handlers/main.yml
    templates/
    files/
    vars/main.yml

In a playbook, a role is included as follows:

- hosts: servers
  roles:
    - webserver

This simplifies automation and makes configurations more maintainable.