1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
- name: Set up Caddy
  hosts: lotus-land-story
  vars:
    version: "{{ versions.caddy }}"
  tasks:

    - name: Make /mnt/lotus-land-story/caddy
      ansible.builtin.file:
        path: /mnt/lotus-land-story/{{ item }}
        state: directory
        mode: "0755"
      loop:
        - caddy
        - caddy/data
        - caddy/data/gists

    - name: Get docker network for trusted proxies
      community.docker.docker_network:
        name: lotus_land_story
      register: docker_network

    - name: Get Undefined Fantastic Object IP
      command: tailscale ip --1 undefined-fantastic-object
      register: undefined_fantastic_object_ip_result

    # TODO Reload Caddy when this changes:
    #      docker exec -w /etc/caddy $caddy_container_id caddy reload
    - name: Set up Caddyfile
      ansible.builtin.template:
        src: templates/Caddyfile
        dest: /mnt/lotus-land-story/caddy/Caddyfile
        mode: "0644"
      vars:
        trusted_proxies: "{{ docker_network.network.IPAM.Config[0].Subnet }}"
        undefined_fantastic_object_ip: "{{ undefined_fantastic_object_ip_result.stdout }}"

    - name: Run Caddy
      community.docker.docker_container:
        name: caddy
        image: ghcr.io/kejadlen/caddy-kejadlen:{{ version }}
        restart: true
        env:
          TS_AUTHKEY: "{{ tailscale.auth_key }}"
          DNSIMPLE_API_ACCESS_TOKEN: "{{ dnsimple.caddy_token }}"
        ports:
          - "80:80"
          - "443:443"
          - "443:443/udp"
          - "2019:2019"
        volumes:
          - /mnt/lotus-land-story/caddy/Caddyfile:/etc/caddy/Caddyfile
          - /mnt/lotus-land-story/caddy/data:/data
          - caddy-config:/config # TODO probably put this somewhere else???
        restart_policy: unless-stopped
        # Unbounded, caddy takes the host down with it. Equal memory_swap denies swap.
        memory: 1g
        memory_swap: 1g
        networks:
          - name: lotus_land_story
        etc_hosts:
          host.docker.internal: host-gateway
      no_log: true

  handlers:
    - name: Import restarts
      ansible.builtin.import_tasks: restarts.yml

# vim: ft=yaml.ansible