Ansible Tutorial · Configuration Management · Lesson 13 of 20
Ansible Roles
Package tasks, templates and variables into reusable roles.
Why roles?
As your automation grows, a single playbook becomes hard to maintain. Roles let you split tasks, handlers, templates, variables and files into a standard directory structure that is reusable across projects.
Creating a role
ansible-galaxy role init myrole
This creates:
myrole/
├── defaults/
│ └── main.yml # low-priority default variables
├── files/ # static files to copy
├── handlers/
│ └── main.yml # handler definitions
├── meta/
│ └── main.yml # role metadata and dependencies
├── tasks/
│ └── main.yml # the main task list
├── templates/ # Jinja2 templates
├── tests/
│ ├── inventory
│ └── test.yml
└── vars/
└── main.yml # high-priority variables
Using a role in a playbook
---
- name: Configure web servers
hosts: webservers
become: true
roles:
- common
- nginx
- app_deploy
Ansible looks for roles in ./roles/, ~/.ansible/roles/ or the paths in roles_path in ansible.cfg.
A minimal role example
roles/nginx/tasks/main.yml:
---
- name: Install nginx
ansible.builtin.apt:
name: nginx
state: present
update_cache: true
- name: Deploy nginx config
ansible.builtin.template:
src: nginx.conf.j2
dest: /etc/nginx/nginx.conf
notify: restart nginx
- name: Start nginx
ansible.builtin.service:
name: nginx
state: started
enabled: true
roles/nginx/handlers/main.yml:
---
- name: restart nginx
ansible.builtin.service:
name: nginx
state: restarted
roles/nginx/defaults/main.yml:
---
nginx_worker_processes: auto
nginx_worker_connections: 1024
import_role vs include_role
tasks:
- ansible.builtin.import_role:
name: nginx
# Static: loaded at parse time, all tasks visible
- ansible.builtin.include_role:
name: nginx
# Dynamic: loaded at runtime, can use loops and when
Use import_role by default; use include_role when you need conditional or looped role inclusion.
Role dependencies
In meta/main.yml you can declare that a role depends on another:
# roles/app_deploy/meta/main.yml
dependencies:
- role: common
- role: nginx
vars:
nginx_worker_connections: 2048
Dependencies are applied before the role's own tasks run.
mkdir -p /tmp/roles/demo/tasks /tmp/roles/demo/defaults
cat > /tmp/roles/demo/tasks/main.yml << 'EOF'
---
- name: Show role variable
ansible.builtin.debug:
msg: "App name is {{ app_name }}"
EOF
cat > /tmp/roles/demo/defaults/main.yml << 'EOF'
---
app_name: my-default-app
EOF
cat > /tmp/role_test.yml << 'EOF'
---
- name: Role test
hosts: localhost
connection: local
gather_facts: false
roles:
- demo
EOF
ansible-playbook /tmp/role_test.yml --roles-path=/tmp/roles
Output
PLAY [Role test] **************************************************************
TASK [demo : Show role variable] **********************************************
ok: [localhost] => {
"msg": "App name is my-default-app"
}
PLAY RECAP ********************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0Run this on your computer — see “Get Started” for setup.
ansible-galaxy role init --help 2>&1 | head -5
Output
usage: ansible-galaxy role init [-h] [--offline] [-s API_SERVER]
[--token API_KEY] [--timeout TIMEOUT] [-v]
[-f] [-p INIT_PATH] [--role-skeleton ROLE_SKELETON]
role_nameRun this on your computer — see “Get Started” for setup.
Test yourself
Where should you define variables that the role consumer can easily override?
vars/main.ymldefaults/main.ymltasks/main.ymlmeta/main.yml
Show answer
Answer: defaults/main.yml
defaults/main.yml has the lowest precedence, so any variable set in inventory, playbook vars, or extra-vars will override it. vars/main.yml has higher precedence and is harder to override.