What steps should be followed when creating and managing a role in Ansible?
sobes.tech AI
Answer from AI
Creating and managing a role in Ansible usually involves the following steps:
-
Creating the role structure — you can use the command
ansible-galaxy init <role_name>, which creates a standard directory structure (tasks, handlers, defaults, vars, files, templates, meta). -
Defining tasks — in the
tasksfolder, describe the main tasks of the role in themain.ymlfile. -
Handlers — in the
handlersfolder, describe actions triggered by events (e.g., restarting a service). -
Variables — in the
defaultsandvarsfolders, set variables with different priorities. -
Templates and files — place Jinja2 templates and static files in the
templatesandfilesfolders, which the role can use. -
Metadata — in
meta/main.yml, specify role dependencies and other information. -
Using the role — include the role in a playbook via the
roleskey. -
Testing and debugging — run the playbook with the role and check for correct execution.
Example of including a role in a playbook:
- hosts: all
roles:
- my_role
Thus, roles help structure configuration, reuse code, and simplify maintenance.