Ansible - Roles π
Description π
Roles are a way of automatically loading related vars_files, tasks, and handlers based on a known file structure. They can also help you better organize your files, especially if youβre using Ansible to deploy multiple apps or systems that have similar roles. Roles are a way of grouping variables, tasks, files, templates, and modules in a way that makes your infrastructure easier to manage.
Roles introduce a set of best practices that must be followed.
- tasks are to go in the
tasks/
directory - vars are to go in the
vars/
directory - defaults are to go in the
defaults/
directory - handlers are to go in the
handlers/
directory - templates are to go in the
templates/
directory
Basics π
-
create a role
ansible-galaxy init <role-name>
# generated role directory structure <role-name>/ βββ defaults βΒ Β βββ main.yml βββ files βββ handlers βΒ Β βββ main.yml βββ meta βΒ Β βββ main.yml βββ tasks βΒ Β βββ main.yml βββ templates βββ tests βΒ Β βββ inventory βΒ Β βββ test.yml βββ vars β βββ main.yml βββ README.md
- how does my playbook know where to find the role?
- add a roles directory to the playbook directory
- add the role to the
/etc/ansible/roles
directory- this is the default location ansible looks for roles defined in the
/etc/ansible/ansible.cfg
file
- this is the default location ansible looks for roles defined in the
- how does my playbook know where to find the role?
-
search for existing community roles
ansible-galaxy search <role-name>