--- # Append this Site's block to the shared Caddyfile. # # Caddy is its own playbook (ansible-playbooks/caddy/playbook.yml) and a rootless # Quadlet unit there, so this role does not manage the container — it only points # it at the app. The Caddyfile is a bind mount into that container, so Caddy sees # a written change as soon as the file lands; the handler only asks Caddy to # re-read it. That is a reload, not a restart, so the other hosted sites are # untouched. # # blockinfile is idempotent on its own via the marker, so there is no need to # slurp the file and compare first — which also matters because slurp *fails* # when the Caddyfile does not exist yet, on a host where the Caddy playbook has # not run. - name: Check the Caddyfile exists ansible.builtin.stat: path: '{{ caddyfile_path }}' register: menu_caddyfile - name: Fail when Caddy is not where we expect it ansible.builtin.fail: msg: >- {{ caddyfile_path }} is missing. Deploy the Caddy playbook (ansible-playbooks/caddy/playbook.yml), which owns the Caddy unit and its config, before this one. when: not menu_caddyfile.stat.exists # The Site's web address may still be claimed by an older snippet: the retired # nginx-menu playbook wrote one per hostname. Two blocks for one address make # Caddy refuse the whole config, and a refused reload leaves the old proxy in # place, which is a 502 once the old container is gone. - name: Drop any older snippet claiming this web address ansible.builtin.blockinfile: path: '{{ caddyfile_path }}' marker: '# {mark} Ansible managed Caddy snippet for {{ host }}' state: absent notify: Reload Caddy # blockinfile replaces whatever sits between its markers, so redeploying updates # the snippet in place rather than appending a second one. - name: Point the Site's web address at the app ansible.builtin.blockinfile: path: '{{ caddyfile_path }}' marker: '# {mark} ANSIBLE MANAGED: menu app ({{ site }})' block: '{{ caddy_snippet }}' create: false owner: '{{ podman_user }}' group: '{{ podman_user }}' mode: '0644' notify: Reload Caddy