Sobes.tech
Junior — Middle

How can the storage and structure of an Ansible Role be organized for effective use?

sobes.tech AI

Answer from AI

Ansible Role is organized according to a standard directory structure, which facilitates code reuse and maintenance. The main directories within a role:

  • tasks/ — contains the main playbook of the role, usually main.yml with tasks.
  • handlers/ — event handlers, such as restarting services.
  • templates/ — Jinja2 file templates for dynamic configuration generation.
  • files/ — static files that are copied to target machines.
  • vars/ — role variables defined in YAML.
  • defaults/ — default variable values that can be overridden.
  • meta/ — role metadata, dependencies on other roles.

Example structure:

my_role/
├── tasks/
│   └── main.yml
├── handlers/
│   └── main.yml
├── templates/
│   └── config.j2
├── files/
│   └── static_file.conf
├── vars/
│   └── main.yml
├── defaults/
│   └── main.yml
└── meta/
    └── main.yml

This separation helps clearly delineate logic, configurations, and files, making roles easier to maintain and scale.