aboutsummaryrefslogtreecommitdiff
path: root/ansible/roles/caddy_add/tasks/main.yaml
blob: 80382d43dc260874552273aa5787bc3c647f31a3 (plain)
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
---

- tags: 'always'
  # noqa: name[missing]
  block:
    - name: 'Ensure required vars are set'
      ansible.builtin.assert:
        that:
          - item | mandatory
      loop:
        - '{{ caddy_add_hosts }}'
        - '{{ caddy_add_upstream_host }}'
        - '{{ caddy_add_upstream_port }}'

    - name: 'Call the Caddy API to add our new host'
      ansible.builtin.uri:
        url: 'http://rproxy:2019/config/apps/http/servers/serv0/routes'
        method: 'PUT'
        body_format: 'json'
        status_code: 200
        body: |
          {
            "match": [
              {
                "host": {{ caddy_add_hosts | to_json }}
              }
            ],
            "handle": [
              {
                "handler": "subroute",
                "routes": [
                  {
                    "handle": [
                      {
                        "handler": "reverse_proxy",
                        "upstreams": [
                          {
                            "dial": "{{ caddy_add_upstream_host }}:{{ caddy_add_upstream_port }}"
                          }
                        ]
                      }
                    ]
                  }
                ]
              }
            ],
            "terminal": true
          }

    - name: 'Get a copy of the new Caddy config'
      delegate_to: 'localhost'
      ansible.builtin.uri:
        url: 'http://rproxy:2019/config'
        return_content: true
      register: '_current_conf'

    - name: 'Save the new config to file'
      delegate_to: 'localhost'
      ansible.builtin.copy:
        dest: 'artifacts/caddy/{{ _file_name }}'
        content: '{{ _current_conf.json }}'
        mode: '644'
      vars:
        _file_name: 'v{{ lookup("pipe", "date +%Y%m%dT%H%M%S") }}.json'