aboutsummaryrefslogtreecommitdiff
path: root/ansible/roles/caddy_add/tasks/main.yaml
blob: 4dcbd8aad0f107b4da55490d7c8c6c491110bed1 (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
---

- 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/srv0/routes/0'
    method: 'PUT'
    follow_redirects: true
    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'
  ansible.builtin.uri:
    url: 'http://rproxy:2019/config'
    return_content: true
  register: '_current_conf'

- name: 'Save the new config to file'
  delegate_to: 'localhost'
  when: 'not ansible_check_mode'
  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'