An Ansible role is a standard way to package related tasks, variables, handlers, templates and files so they can be reused across Playbooks and projects. A role for “nginx,” for example, contains everything needed to install, configure and run nginx, and any Playbook can apply it with one line.
Ansible role structure
roles/nginx/
tasks/main.yml
handlers/main.yml
defaults/main.yml
vars/main.yml
templates/
files/
meta/main.yml
defaults/main.yml holds variables meant to be overridden; meta/main.yml holds metadata and dependencies.
include_role vs import_role: import_role is static and processed when the Playbook is parsed. include_role is dynamic and processed at runtime, so it can be used in loops and conditionals.