Please enable JavaScript.
Coggle requires JavaScript to display documents.
11 ANSIBLE ENTERPRISE LEVEL → PHASE 11 : ROLES - Coggle Diagram
11 ANSIBLE ENTERPRISE LEVEL → PHASE 11 : ROLES
What is a Role?
Definition
A role is a standardized, reusable directory structure for organizing Ansible automation.
It separates tasks, variables, templates, files, handlers, and metadata into dedicated directories.
Roles improve modularity, readability, scalability, and maintenance.
One role performs one job.
Example Roles
apache
nginx
mysql
docker
kubernetes
users
security
monitoring
backup
Enterprise Purpose
Standardization
Code Reusability
Easy Maintenance
Team Collaboration
Version Control
CI/CD Integration
Testing
Automation at Scale
Enterprise Project Structure
ansible-project/
ansible.cfg
inventory/
playbooks/
group_vars/
host_vars/
roles/
collections/
plugins/
files/
templates/
logs/
vault/
requirements.yml
README.md
Roles Directory
roles/
apache/
nginx/
mysql/
docker/
users/
security/
monitoring/
backup/
Create Role
Command
ansible-galaxy init apache
What happens?
Creates complete enterprise directory structure
Creates default YAML files
Creates README.md
Ready for development
Create inside project
cd ~/ansible-project
ansible-galaxy init roles/apache
Role Directory Structure
apache/
defaults/
files/
handlers/
meta/
tasks/
templates/
tests/
vars/
README.md
tasks/
Purpose
Main execution logic
Contains modules executed sequentially
First directory executed
File
tasks/main.yml
Example Tasks
Install package
Copy configuration
Create directories
Manage users
Configure firewall
Start services
Deploy application
Include other task files
Enterprise Practice
install.yml
configure.yml
firewall.yml
ssl.yml
security.yml
service.yml
cleanup.yml
handlers/
Purpose
Execute only when notified
Avoid unnecessary restarts
Improve efficiency
File
handlers/main.yml
Common Handlers
Restart Apache
Restart Nginx
Reload Systemd
Restart Docker
Restart SSH
Reload Firewall
Restart Database
defaults/
Purpose
Default variables
Lowest priority variables
Safe to override
File
defaults/main.yml
Typical Variables
package_name
service_name
document_root
listen_port
server_admin
log_path
backup_directory
vars/
Purpose
Internal role variables
High priority variables
Rarely overridden
File
vars/main.yml
Typical Variables
Package names by OS
Service names
Configuration paths
Binary locations
Internal constants
files/
Purpose
Static files
No variables inside
Used For
HTML files
Shell scripts
Certificates
Images
Binaries
RPM packages
License files
Backup files
templates/
Purpose
Dynamic files
Uses Jinja2 variables
Extension
.j2
Examples
httpd.conf.j2
nginx.conf.j2
sshd_config.j2
hosts.j2
index.html.j2
application.properties.j2
meta/
Purpose
Role metadata
Dependencies
Platform support
Galaxy information
File
meta/main.yml
Contains
author
company
description
license
supported platforms
dependencies
galaxy tags
tests/
Purpose
Testing role functionality
Validation
Development testing
Files
inventory
test.yml
README.md
Purpose
Documentation
Installation guide
Variables explanation
Usage examples
Dependencies
Supported platforms
Version history
Calling Role
Playbook
roles:
apache
mysql
docker
monitoring
Variable Flow
defaults
group_vars
host_vars
playbook vars
vars
extra vars
Runtime variables
Role Dependencies
Example
apache depends on firewall
monitoring depends on users
application depends on database
docker depends on kernel settings
Enterprise Best Practices
One role = One responsibility
Never hardcode values
Store defaults in defaults/
Store internal constants in vars/
Use handlers for service restart
Use templates for configuration files
Use files for static content
Keep tasks modular
Split large tasks into multiple YAML files
Document every role
Maintain Git repository
Follow idempotency
Test roles before production
Use meaningful role names
Version roles
Encrypt secrets with Ansible Vault
Store roles under roles/
Use Galaxy naming conventions
Reuse roles across environments
Maintain consistent directory structure
Enterprise Role Examples
apache
nginx
mysql
mariadb
postgres
docker
kubernetes
users
ssh
firewall
selinux
chrony
dns
monitoring
logging
backup
patching
security
antivirus
auditd
fail2ban
elastic-agent
wazuh-agent
zabbix-agent
node-exporter
application
loadbalancer
ssl
Enterprise Workflow
Design project structure
Create inventories
Configure ansible.cfg
Create role using ansible-galaxy init
Develop tasks
Define variables
Add templates
Add static files
Configure handlers
Add metadata
Test role
Commit to Git
Review through CI/CD
Deploy to Development
Deploy to QA
Deploy to Staging
Deploy to Production
Monitor deployment
Maintain versions
Reuse role across future projects