From 782e4429c89c6e91cd9aa7e5ba59517a52200dcd Mon Sep 17 00:00:00 2001 From: Martin Michalec Date: Thu, 22 Oct 2026 23:12:08 +0300 Subject: feat: add nginx --- roles/nginx/handlers/main.yml | 5 ++++ roles/nginx/tasks/main.yml | 50 +++++++++++++++++++++++++++++++++ roles/nginx/templates/nginx-validate.j2 | 38 +++++++++++++++++++++++++ roles/nginx/templates/nginx.conf.j2 | 33 ++++++++++++++++++++++ 4 files changed, 126 insertions(+) create mode 100644 roles/nginx/handlers/main.yml create mode 100644 roles/nginx/tasks/main.yml create mode 100644 roles/nginx/templates/nginx-validate.j2 create mode 100644 roles/nginx/templates/nginx.conf.j2 (limited to 'roles') diff --git a/roles/nginx/handlers/main.yml b/roles/nginx/handlers/main.yml new file mode 100644 index 0000000..0f31453 --- /dev/null +++ b/roles/nginx/handlers/main.yml @@ -0,0 +1,5 @@ +--- +- name: Reload nginx + ansible.builtin.service: + name: nginx + state: reloaded diff --git a/roles/nginx/tasks/main.yml b/roles/nginx/tasks/main.yml new file mode 100644 index 0000000..f4ffb6c --- /dev/null +++ b/roles/nginx/tasks/main.yml @@ -0,0 +1,50 @@ +--- +- block: + - name: Install nginx and required modules + community.general.apk: + name: + - nginx + - nginx-openrc + - nginx-mod-http-xslt-filter + state: present + + # permission are not right + # - name: Ensure nginx web and log directories exist + # ansible.builtin.file: + # path: "{{ item }}" + # state: directory + # owner: nginx + # group: nginx + # mode: '0755' + # loop: + # - /srv/http + # - /var/log/nginx + # - /var/lib/nginx + # - /var/www/acme + + - name: Deploy main nginx.conf + ansible.builtin.template: + src: nginx.conf.j2 + dest: /etc/nginx/nginx.conf + owner: root + group: root + mode: '0444' + validate: nginx-validate %s /etc/nginx/nginx.conf + notify: Reload nginx + + - name: Enable and start nginx service + ansible.builtin.service: + name: nginx + enabled: true + state: started + + - name: Install atomic nginx validation wrapper + ansible.builtin.template: + src: nginx-validate.j2 + dest: /usr/local/bin/nginx-validate + owner: root + group: root + mode: '0755' + + tags: + - nginx diff --git a/roles/nginx/templates/nginx-validate.j2 b/roles/nginx/templates/nginx-validate.j2 new file mode 100644 index 0000000..81c6fb8 --- /dev/null +++ b/roles/nginx/templates/nginx-validate.j2 @@ -0,0 +1,38 @@ +#!/bin/sh + +# {{ ansible_managed }} +# Atomic Nginx configuration validator wrapper for Ansible + +TEMP_FILE="$1" +DEST_FILE="$2" + +if [ -z "$TEMP_FILE" ] || [ -z "$DEST_FILE" ]; then + echo "Usage: nginx-validate " >&2 + exit 1 +fi + +TIMESTAMP=$(date +%Y%m%d-%H%M%S) +BACKUP_FILE="${DEST_FILE}.${TIMESTAMP}.bak" + +# Back up current destination if it exists +if [ -f "$DEST_FILE" ]; then + cp "$DEST_FILE" "$BACKUP_FILE" +fi + +# Apply the rendered temporary file for testing +cp "$TEMP_FILE" "$DEST_FILE" + +# Test master configuration +if nginx -t; then + rm -f "$BACKUP_FILE" + exit 0 +else + # Restore previous config on failure and clean up + if [ -f "$BACKUP_FILE" ]; then + cp "$BACKUP_FILE" "$DEST_FILE" + rm -f "$BACKUP_FILE" + else + rm -f "$DEST_FILE" + fi + exit 1 +fi diff --git a/roles/nginx/templates/nginx.conf.j2 b/roles/nginx/templates/nginx.conf.j2 new file mode 100644 index 0000000..110fa02 --- /dev/null +++ b/roles/nginx/templates/nginx.conf.j2 @@ -0,0 +1,33 @@ +# {{ ansible_managed }} +user nginx; +error_log /var/log/nginx/error.log warn; +pid /var/run/nginx/nginx.pid; + +worker_processes auto; +worker_rlimit_nofile 65535; + +events { + worker_connections 4096; + use epoll; + multi_accept on; +} + +http { + 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; + server_tokens off; + keepalive_timeout 65; + + client_body_buffer_size 512k; + client_max_body_size 64M; + + default_type application/octet-stream; + include /etc/nginx/mime.types; + include /etc/nginx/http.d/*.conf; +} -- cgit v1.3.1