real-world-examples
๐ Real-World Ansible Examples from Your Projectโ
This document walks through real Ansible examples taken directly from your /home/ansible/
structure. It includes playbooks, inventories, roles, templates, and variable management โ all applied in real-world scenarios.
๐ site.yaml
โ Main Playbookโ
---
- name: Generate reports for report servers
hosts: demo_prod_aws
become: yes
vars_files:
- ../vars/main.yml
roles:
- goaccess-report
- report
- ssh-key-reset
Optional setups are commented for:
- Promtail, Node Exporter, and Tailscale
- NGINX setup
- Utilities install
๐ Inventory Example: inventory/test.ini
โ
[test]
Portfolio ansible_host=13.201.191.80 ansible_port=33000 ansible_user=root
โ๏ธ Apache Role Logicโ
๐ roles/apache/tasks/main.yaml
- name: Set Apache package & service names
set_fact:
apache_pkg: "{{ os_pkg_map[ansible_os_family] | default('httpd') }}"
apache_svc: "{{ os_pkg_map[ansible_os_family] | default('httpd') }}"
vars:
os_pkg_map:
Debian: apache2
RedHat: httpd
Amazon: httpd
Alpine: apache2
- name: Install Apache
package:
name: "{{ apache_pkg }}"
state: present
- name: Enable and start Apache
service:
name: "{{ apache_svc }}"
enabled: yes
state: started
notify: Restart Apache
๐ roles/apache/handlers/main.yaml
- name: Restart Apache
service:
name: "{{ apache_svc }}"
state: restarted
๐ Report Role: System Auditโ
๐ roles/report/tasks/main.yaml
performs actions like:
- Gather OS details
- Detect languages: Python, Node.js, PHP, MySQL
- Check for services like NGINX, Apache, Tailscale
- Disk usage on
/
,/var
,/www
- Export reports to control machine via Jinja2
๐ Template: roles/report/templates/main-txt.j2
===============================
๐ Host Info & OS
===============================
Hostname: {{ hostname_cmd.stdout }}
OS: {{ os_release.stdout }}
...
Disk Usage:
{% for line in disk_usage_var.stdout_lines %}- {{ line }}{% endfor %}
๐ NGINX Role Logicโ
๐ roles/nginx/tasks/main.yaml
- name: Stop Apache if running
service:
name: "{{ item }}"
state: stopped
enabled: no
loop:
- apache2
- httpd
when: apache_status.stdout == "active"
- name: Install NGINX
package:
name: nginx
state: present
- name: Enable and start NGINX
service:
name: nginx
enabled: yes
state: started
๐ฆ Variable Definitionsโ
๐ vars/main.yml
promtail_version: "2.5.0"
node_exporter_version: "1.8.1"
nginx_port: 80
apache_port: 80
report_dir: "/home/devops/Documents/.../reports/"
goaccess_report_dir: "/home/devops/.../reports/goaccess/"
log_path: /var/log/nginx/access.log
log_format: COMBINED
timezone: Asia/Kolkata
date_format: "+%Y-%m-%d"
remote_report_path: /tmp/
cpu_mem_summary_filename: process-summary.txt
โ Summaryโ
This setup includes everything you'd expect in a real infrastructure:
- ๐ฏ Host targeting via inventory
- ๐ Reusable roles (Apache, NGINX, Report)
- ๐ System diagnostics & reports
- ๐งพ Templated output for auditing
- ๐ฅ Centralized variable management
๐ This is a powerful example of how to structure and scale Ansible for production environments.