aboutsummaryrefslogtreecommitdiff
path: root/roles/nginx/templates/nginx-validate.j2
diff options
context:
space:
mode:
authorLibravatar Martin Michalec <martin@michalec.dev>2026-10-22 20:12:08 +0000
committerLibravatar Martin Michalec <martin@michalec.dev>2026-10-22 20:12:08 +0000
commit782e4429c89c6e91cd9aa7e5ba59517a52200dcd (patch)
treee7191c9c998070d9393ce22748ebcfa859991dc0 /roles/nginx/templates/nginx-validate.j2
parentfeat: add fail2ban (diff)
downloadinfra-782e4429c89c6e91cd9aa7e5ba59517a52200dcd.tar.gz
feat: add nginx
Diffstat (limited to 'roles/nginx/templates/nginx-validate.j2')
-rw-r--r--roles/nginx/templates/nginx-validate.j238
1 files changed, 38 insertions, 0 deletions
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 @@
1#!/bin/sh
2
3# {{ ansible_managed }}
4# Atomic Nginx configuration validator wrapper for Ansible
5
6TEMP_FILE="$1"
7DEST_FILE="$2"
8
9if [ -z "$TEMP_FILE" ] || [ -z "$DEST_FILE" ]; then
10 echo "Usage: nginx-validate <temp_file> <dest_file>" >&2
11 exit 1
12fi
13
14TIMESTAMP=$(date +%Y%m%d-%H%M%S)
15BACKUP_FILE="${DEST_FILE}.${TIMESTAMP}.bak"
16
17# Back up current destination if it exists
18if [ -f "$DEST_FILE" ]; then
19 cp "$DEST_FILE" "$BACKUP_FILE"
20fi
21
22# Apply the rendered temporary file for testing
23cp "$TEMP_FILE" "$DEST_FILE"
24
25# Test master configuration
26if nginx -t; then
27 rm -f "$BACKUP_FILE"
28 exit 0
29else
30 # Restore previous config on failure and clean up
31 if [ -f "$BACKUP_FILE" ]; then
32 cp "$BACKUP_FILE" "$DEST_FILE"
33 rm -f "$BACKUP_FILE"
34 else
35 rm -f "$DEST_FILE"
36 fi
37 exit 1
38fi