Going further / Lesson 2 of 2
Roles
Package tasks, templates and defaults into reusable roles, and share them through Ansible Galaxy.
7 min Intermediate 7 steps
IN THIS LESSON
Step 1 of 7
STEP 1 / 7
Playbooks grow
As you automate more, one playbook fills up with tasks, templates and variables for many different jobs. It gets hard to read, and copying pieces into other playbooks leads to drift.
← → step · R replay · Space play/pause
YOU'LL LEARN
- Why playbooks are split into roles
- What a role directory contains
- How to apply a role from a playbook
- Where to find shared roles and collections
- Playbooks grow. As you automate more, one playbook fills up with tasks, templates and variables for many different jobs. It gets hard to read, and copying pieces into other playbooks leads to drift.
- A role bundles one job. A role packages everything needed for one purpose, such as installing and configuring nginx. The playbook then just says which roles to apply.
- A standard layout. Roles follow a fixed folder structure, so Ansible knows where to look and anyone can find their way around. Only the folders you need are required.
- Defaults can be overridden. Variables in defaults/ are the role's sensible starting values. They have the lowest precedence, so a playbook, an inventory or the command line can override them without editing the role.
- Apply it to your servers. When the play runs, Ansible loads the role's tasks, templates, handlers and defaults and applies them to every host in the group.
- Share through Galaxy. Ansible Galaxy is the public place to find roles and collections written by the community and vendors. Collections bundle modules, roles and plugins together. Review third-party content before running it on your servers.
- Recap. Roles keep automation organised, reusable and shareable.