playbooks-and-roles
๐ Playbooks & Roles (Based on Your Project Structure)โ
Your Ansible project is well-organized using roles and playbooks. Below is a tailored explanation of how Playbooks and Roles work with examples taken directly from your directory structure.
๐ Project Structure Overviewโ
D:\aops
โโโ playbooks/ # Main playbook files
โ โโโ report/
โ โโโ go-access/
โโโ roles/ # All role logic here
โ โโโ apache/
โ โโโ goaccess-install/
โ โโโ goaccess-report/
โ โโโ nginx/
โ โโโ node_exporter/
โ โโโ promtail/
โ โโโ report/
โ โโโ ssh-key-reset/
โ โโโ tailscale/
โ โโโ utilities/
๐ง What is a Playbook?โ
A playbook is a YAML file that tells Ansible what tasks to run and on which hosts. Think of it as the automation blueprint.
๐งพ Example: playbooks/report/go-access/main.yml
โ
---
- name: Generate GoAccess Report
hosts: webservers
become: true
roles:
- goaccess-install
- goaccess-report
This playbook runs two roles on the webservers
group: one to install GoAccess, and another to generate a report.
โ๏ธ What is a Role?โ
A role is a pre-defined structure for organizing related Ansible content like tasks, templates, handlers, etc.
๐ Role Example: roles/goaccess-report
โ
roles/goaccess-report/
โโโ tasks/
โ โโโ main.yml
โโโ templates/
โโโ goaccess.html.j2
๐งพ tasks/main.ymlโ
---
- name: Generate GoAccess HTML report
command: >
goaccess /var/log/nginx/access.log \
-o /var/www/html/report.html \
--log-format=COMBINED
๐ Role Example: roles/apache
โ
roles/apache/
โโโ tasks/
โ โโโ main.yml
โโโ handlers/
โ โโโ main.yml
๐งพ tasks/main.ymlโ
---
- name: Install Apache
apt:
name: apache2
state: present
notify: Restart Apache
๐ handlers/main.ymlโ
---
- name: Restart Apache
service:
name: apache2
state: restarted
๐ Using Roles in a Playbookโ
- name: Install and configure Apache
hosts: webservers
become: true
roles:
- apache
This makes your automation clean, modular, and maintainable.
โ ๏ธ Note on Nested Role Pathโ
๐ roles/playbooks/roles/nginx/
โ This looks like a nested role and may not work unless explicitly included. It's better to keep roles directly under roles/
.
โ Summaryโ
Component | Purpose |
---|---|
Playbooks | Define orchestration and logic per host/group |
Roles | Modular units (Apache, GoAccess, etc.) |
Inventory | Defines target systems |
Vars | Centralized variable management |
๐ With this setup, you can easily scale automation by plugging roles into playbooks as needed. Clean, simple, and powerful!