Files
ansible/roles/docker/tasks/infrastructure/caddy.yml
T
PhilandClaude Opus 5.5 9696c0e441 caddy: reload instead of restart, validate before deploy, HTTP catch-all, header timeout, body limits; dns: drop stale records
- Caddyfile moves to config/ (directory mount) so 'caddy reload' sees the
  new file; the handler reloads gracefully instead of restarting
- template validate: runs 'caddy validate' with the image Caddy uses
- http:// catch-all aborts plain-HTTP requests for unknown hosts / bare IP;
  known hosts keep the automatic HTTPS redirect (tested)
- read_header timeout 10s (default 1 min)
- body_limit snippet: 1MB static sites, tinyauth, repaircafe; 10MB Pocket ID,
  Changedetection, ByteStash, Gotify, iGotify; 20MB Baikal
- dns: remove grist, habits, office, podcasts, pdftools (nothing serves them)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 13:13:16 -07:00

75 lines
2.3 KiB
YAML

- name: make caddy directories
ansible.builtin.file:
path: "{{ item}}"
state: directory
loop:
- /opt/stacks/caddy
- /opt/stacks/caddy/config
# The Caddyfile lives in config/, which is mounted as a directory: a
# single-file bind mount would keep showing Caddy the old file after
# ansible replaces it, and "caddy reload" would reload stale config.
# validate: the new file must pass "caddy validate" with the image Caddy
# runs before it replaces the live one, so a broken Caddyfile never lands.
- name: template Caddyfile
ansible.builtin.template:
src: Caddyfile.j2
dest: /opt/stacks/caddy/config/Caddyfile
owner: root
mode: '0640'
group: docker
validate: >-
docker run --rm --network none -e CROWDSEC_API_KEY=validate
-v %s:/etc/caddy/Caddyfile:ro {{ caddy_image }}
caddy validate --config /etc/caddy/Caddyfile --adapter caddyfile
notify: reload caddy
- name: remove the old Caddyfile location
ansible.builtin.file:
path: /opt/stacks/caddy/Caddyfile
state: absent
- name: template caddy .env (CrowdSec bouncer key)
ansible.builtin.template:
src: caddy-env.j2
dest: /opt/stacks/caddy/.env
owner: root
mode: '0640'
group: docker
diff: false
when: caddy_custom
register: caddy_env
- name: template caddy compose file
ansible.builtin.template:
src: caddy-compose.yml.j2
dest: /opt/stacks/caddy/compose.yml
owner: root
mode: '0644'
register: caddy_compose
- name: deploy caddy stack
community.docker.docker_compose_v2:
project_src: /opt/stacks/caddy
recreate: never
files:
- compose.yml
# recreate: never (above) keeps the running container, so a new image or env
# (caddy_custom on/off, a pinned tag) only applies through this task.
- name: recreate caddy when its compose file or .env changed
community.docker.docker_compose_v2:
project_src: /opt/stacks/caddy
recreate: always
files:
- compose.yml
when: caddy_compose.changed or (caddy_env.changed | default(false))
# Lets public traffic through to Caddy. Re-adds the rule if Caddy's IP changed.
- name: allow public traffic to caddy
ansible.builtin.command: /usr/local/bin/ufw-docker.sh allow caddy-caddy-1 {{ item }}
loop: [80/tcp, 443/tcp, 443/udp]
register: caddy_ufw
changed_when: "'Skipping' not in caddy_ufw.stdout"
when: not ansible_check_mode