Chapter 06 · Ansible Roles
Ansible Roles
Introduction to roles
A role is a standardised structure that brings together: tasks, handlers, templates, files, defaults, vars, meta. It is the structured way to use Ansible at scale.
roles/
webserver/
tasks/main.yml
handlers/main.yml
templates/
files/
defaults/main.yml
vars/main.yml
Why use roles?
- Reusability (the same role across several projects)
- Readability (you know where to look)
- Testability (molecule, CI, linting)
- Sharing (Ansible Galaxy / collections)
Structuring your code with roles
- One role = one responsibility (webserver, database, users, hardening…)
- Default variables in
defaults/(easy to override) - Higher-precedence variables in
vars/(harder to override) - Jinja2 templates for configuration files
By using Ansible roles, you will only need to write a simple, lightweight playbook such as the following:
- name: Name of the App
hosts: [destination]
become: true [optional]
serial: 1 [optional]
vars:
index_msg: "Hello from Ansible (chapter 6)" [this one provide a low level of reutilisability]
roles:
- webserver [name of the the role]
You will then only need to modify existing files and/or add new ones within the appropriate role components: templates, defaults, vars, tasks and handlers.
Practical exercise: turn your playbook into a role
- Create a role
roles/webserverusingansible-galaxy init. - Move the nginx tasks into it.
- Use
templates/index.html.j2instead of ancopyinline. - In
site.yml, replace the tasks with:
- name: Web
hosts: web
become: true
roles:
- webserver
Minimal example (multiple files)
mkdir -p roles
ansible-galaxy init roles/webserver # creates the directory structure
roles/webserver/templates/index.html.j2 :
<h1>{{ index_msg }}</h1>
<p>Host: {{ inventory_hostname }}</p>
roles/webserver/tasks/main.yml :
- name: Install nginx
ansible.builtin.apt:
name: nginx
state: present
update_cache: true
tags: [install]
- name: Deploy the home page using a template
ansible.builtin.template:
src: index.html.j2
dest: /var/www/html/index.html
owner: root
group: root
mode: "0644"
notify: restart nginx
tags: [config]
- name: Start and enable nginx
ansible.builtin.service:
name: nginx
state: started
enabled: true
tags: [service]
roles/webserver/handlers/main.yml :
- name: restart nginx
ansible.builtin.service:
name: nginx
state: restarted
roles/webserver/defaults/main.yml :
index_msg: "Hello from the webserver role"
roles/webserver/vars/main.yml :
index_msg: "Hello from a variable in vars/ that has not been overridden"
site.yml :
- name: Web via role
hosts: web
become: true
serial: 1
vars:
index_msg: "Hello from Ansible (chapter 6)"
roles:
- webserver
Multiple-choice quiz
1) A role is primarily used to:
- A. Design websites
- B. Structure and reuse Ansible code
- C. Replace SSH
2) Where are default variables stored?
- A. defaults/main.yml
- B. handlers/main.yml
- C. meta/main.yml
3) A good role should:
- A. Have one clear responsibility
- B. Do everything (web+database+monitoring+coffee)
- C. Never be documented
Answer key
1) B · 2) A · 3) A