0 / 13 lessons — 0%
Lesson 08 / 13
Roles & project structure
One playbook with 200 tasks in it is unreadable and unreusable. A role packages related tasks, templates, files, variables, and handlers into a standard folder structure — "the nginx role," "the postgres role" — that any playbook can pull in with one line.
roles/ nginx/ tasks/main.yml # the actual steps handlers/main.yml # restart nginx, etc. templates/nginx.conf.j2 defaults/main.yml # default variable values, easy to override vars/main.yml # variables not meant to be overridden files/ # static files to copy as-is
# site.yml — now just a short list of which roles apply where - name: Configure web tier hosts: webservers roles: - nginx - app - name: Configure db tier hosts: dbservers roles: - postgres
# scaffold the folder structure for you ansible-galaxy role init nginx
Same shape as roles/modules everywhere else in this track: a Docker Compose service, a Helm chart, an Ansible role — all the same idea of "package it once, parameterize it, reuse it everywhere" wearing a different hat.
Try it yourselfRun
ansible-galaxy role init myrole and look at the folder tree it generates. Recognizing that structure on sight is most of what you need to read anyone else's roles later.