Skip to main content

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.