Roles

Organize automation logic into reusable components.

Role Structure


roles/
└── webserver/
    ├── tasks/
    │   └── main.yml
    ├── handlers/
    │   └── main.yml
    ├── templates/
    ├── vars/
    ├── defaults/
    └── meta/

Using Roles in Playbooks


- hosts: web
  roles:
    - webserver

Defaults vs Vars

Default variables have the lowest precedence and can be overridden easily, while vars have higher priority and should be used sparingly.

Ansible Galaxy

Ansible Galaxy provides a repository of community-maintained roles that can accelerate development.