diff options
Diffstat (limited to 'roles/nginx/templates/nginx-validate.j2')
| -rw-r--r-- | roles/nginx/templates/nginx-validate.j2 | 38 |
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 | |||
| 6 | TEMP_FILE="$1" | ||
| 7 | DEST_FILE="$2" | ||
| 8 | |||
| 9 | if [ -z "$TEMP_FILE" ] || [ -z "$DEST_FILE" ]; then | ||
| 10 | echo "Usage: nginx-validate <temp_file> <dest_file>" >&2 | ||
| 11 | exit 1 | ||
| 12 | fi | ||
| 13 | |||
| 14 | TIMESTAMP=$(date +%Y%m%d-%H%M%S) | ||
| 15 | BACKUP_FILE="${DEST_FILE}.${TIMESTAMP}.bak" | ||
| 16 | |||
| 17 | # Back up current destination if it exists | ||
| 18 | if [ -f "$DEST_FILE" ]; then | ||
| 19 | cp "$DEST_FILE" "$BACKUP_FILE" | ||
| 20 | fi | ||
| 21 | |||
| 22 | # Apply the rendered temporary file for testing | ||
| 23 | cp "$TEMP_FILE" "$DEST_FILE" | ||
| 24 | |||
| 25 | # Test master configuration | ||
| 26 | if nginx -t; then | ||
| 27 | rm -f "$BACKUP_FILE" | ||
| 28 | exit 0 | ||
| 29 | else | ||
| 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 | ||
| 38 | fi | ||
