Skip to content

Ansible_roles

This chapter covers Ansible roles, role structure, and using Ansible Galaxy.

Roles are a way to organize playbooks and other Ansible files for reuse.

┌─────────────────────────────────────────────────────────────────────────────┐
│ Ansible Role Concept │
├─────────────────────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────────────────────────────────────────────────────────────┐ │
│ │ Role Structure │ │
│ │ │ │
│ │ role_name/ │ │
│ │ ├── defaults/ │ │
│ │ │ └── main.yml # Default variables │ │
│ │ ├── files/ │ │
│ │ │ └── config.conf # Static files │ │
│ │ ├── handlers/ │ │
│ │ │ └── main.yml # Handlers │ │
│ │ ├── meta/ │ │
│ │ │ └── main.yml # Role metadata │ │
│ │ ├── tasks/ │ │
│ │ │ └── main.yml # Main tasks │ │
│ │ ├── templates/ │ │
│ │ │ └── config.j2 # Jinja2 templates │ │
│ │ ├── tests/ │ │
│ │ │ ├── inventory │ │
│ │ │ └── test.yml # Test playbooks │ │
│ │ └── vars/ │ │
│ │ └── main.yml # Role variables │ │
│ │ │ │
│ └─────────────────────────────────────────────────────────────────────┘ │
│ │
│ Benefits: │
│ ✓ Reusability - Use across projects │
│ ✓ Organization - Structured approach │
│ ✓ Shareability - Distribute via Galaxy │
│ ✓ Dependency management - Role dependencies │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
roles/
└── nginx/
├── defaults/
│ └── main.yml
├── files/
│ └── nginx.conf
├── handlers/
│ └── main.yml
├── meta/
│ └── main.yml
├── tasks/
│ └── main.yml
├── templates/
│ └── nginx.conf.j2
├── tests/
│ ├── inventory
│ └── test.yml
└── vars/
└── main.yml
roles/nginx/defaults/main.yml
---
nginx_port: 80
nginx_worker_processes: auto
nginx_worker_connections: 1024
nginx_server_names_hash_bucket_size: 64
nginx_keepalive_timeout: 65
nginx_client_max_body_size: 10M
nginx_packages:
- nginx
nginx_user: www-data
nginx_group: www-data
nginx_config_dir: /etc/nginx
roles/nginx/tasks/main.yml
---
- name: Install nginx
apt:
name: "{{ nginx_packages }}"
state: present
when: ansible_os_family == "Debian"
- name: Install nginx (RHEL)
yum:
name: nginx
state: present
when: ansible_os_family == "RedHat"
- name: Configure nginx
template:
src: nginx.conf.j2
dest: "{{ nginx_config_dir }}/nginx.conf"
validate: nginx -t %s
notify: Restart nginx
- name: Enable and start nginx
service:
name: nginx
state: started
enabled: yes
roles/nginx/handlers/main.yml
---
- name: Restart nginx
service:
name: nginx
state: restarted
- name: Reload nginx
service:
name: nginx
state: reloaded
roles/nginx/templates/nginx.conf.j2
user {{ nginx_user }} {{ nginx_group }};
worker_processes {{ nginx_worker_processes }};
events {
worker_connections {{ nginx_worker_connections }};
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout {{ nginx_keepalive_timeout }};
types_hash_max_size {{ nginx_server_names_hash_bucket_size }};
include /etc/nginx/conf.d/*.conf;
}
roles/nginx/vars/main.yml
---
# Variables with higher precedence than defaults
nginx_config_dir: /etc/nginx
nginx_user: www-data
nginx_group: www-data
roles/nginx/meta/main.yml
---
galaxy_info:
author: Your Name
description: Nginx web server role
company: Your Company
license: MIT
min_ansible_version: '2.9'
platforms:
- name: Ubuntu
versions:
- all
- name: Debian
versions:
- all
- name: EL
versions:
- 7
- 8
galaxy_tags:
- web
- nginx
- server
dependencies: []
# - role: common
# - role: ssl_certs
site.yml
---
- name: Deploy web server
hosts: webservers
become: yes
roles:
- nginx
# With role parameters
- name: Deploy web server
hosts: webservers
become: yes
roles:
- role: nginx
nginx_port: 8080
nginx_worker_processes: 4
- role: php
php_version: "8.1"
- role: mysql
mysql_port: 3307
# Role with tags
- name: Configure webservers
hosts: webservers
become: yes
roles:
- role: nginx
tags: [web, nginx]
- role: php
tags: [web, php]
# Using include_role
- name: Include nginx role
include_role:
name: nginx
# With parameters
- name: Include role conditionally
include_role:
name: nginx
when: install_nginx | default(true)
# With variables
- name: Include role with variables
include_role:
name: webserver
vars:
server_port: 8080
server_name: example.com
┌─────────────────────────────────────────────────────────────────────────────┐
│ Ansible Galaxy │
├─────────────────────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────────────────────────────────────────────────────────────┐ │
│ │ Ansible Galaxy │ │
│ │ │ │
│ │ Galaxy is a hub for finding, sharing, and managing Ansible roles │ │
│ │ │ │
│ │ Commands: │ │
│ │ ansible-galaxy search <search_term> │ │
│ │ ansible-galaxy info <role_name> │ │
│ │ ansible-galaxy install <role_name> │ │
│ │ ansible-galaxy list │ │
│ │ ansible-galaxy remove <role_name> │ │
│ │ │ │
│ │ Install from requirements.yml: │ │
│ │ ansible-galaxy install -r requirements.yml │ │
│ │ │ │
│ └─────────────────────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
Terminal window
# Search for roles
ansible-galaxy search nginx
# Install a role
ansible-galaxy install geerlingguy.nginx
# Install specific version
ansible-galaxy install geerlingguy.nginx,4.3.0
# List installed roles
ansible-galaxy list
requirements.yml
---
roles:
# From Ansible Galaxy
- name: geerlingguy.nginx
version: "4.3.0"
- name: geerlingguy.php
version: "5.1.0"
- name: geerlingguy.mysql
version: "4.1.0"
# From GitHub
- src: https://github.com/username/ansible-role-nginx
name: custom_nginx
version: "1.0.0"
# From Git
- src: git+https://github.com/company/ansible-role-webserver.git
name: company_webserver
version: main
roles/webserver/meta/main.yml
---
dependencies:
- role: common
vars:
common_packages:
- curl
- vim
- role: nginx
- role: php
php_version: "8.1"
# Project structure
ansible-project/
├── inventory
│ ├── production
│ └── staging
├── playbooks/
│ ├── site.yml
│ └── web.yml
├── roles/
│ ├── common/
│ ├── nginx/
│ ├── php/
│ └── mysql/
└── ansible.cfg
playbooks/site.yml
---
- name: Configure all servers
hosts: all
roles:
- common
- name: Configure webservers
hosts: webservers
become: yes
roles:
- nginx
- php
- composer
- name: Configure databases
hosts: databases
become: yes
roles:
- mysql

In this chapter, you learned:

  • Role Concept: What are roles and why use them
  • Role Structure: Directory layout and files
  • Creating Roles: defaults, tasks, handlers, templates, vars
  • Using Roles: Including roles in playbooks
  • Ansible Galaxy: Finding and installing community roles
  • Role Dependencies: Managing role relationships