You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Roles are the standard way to organise and share Ansible automation. A role packages tasks, variables, files, templates, and handlers into a reusable, self-contained unit with a well-defined directory structure.
| Benefit | Description |
|---|---|
| Reusability | Write once, use across multiple playbooks and projects |
| Organisation | Standard directory structure makes code easy to find and maintain |
| Sharing | Share roles via Ansible Galaxy or private repositories |
| Testing | Test roles independently with Molecule |
| Encapsulation | Each role has its own variables, defaults, files, and templates |
roles/
+-- nginx/
+-- defaults/
| +-- main.yml # Default variables (lowest precedence)
+-- vars/
| +-- main.yml # Role variables (higher precedence)
+-- tasks/
| +-- main.yml # Main task list
+-- handlers/
| +-- main.yml # Handler definitions
+-- templates/
| +-- nginx.conf.j2 # Jinja2 templates
+-- files/
| +-- index.html # Static files
+-- meta/
| +-- main.yml # Role metadata and dependencies
+-- tests/
| +-- test.yml # Test playbook
+-- README.md # Documentation
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.