aboutsummaryrefslogtreecommitdiff
path: root/ansible/roles/caddy_add
diff options
context:
space:
mode:
Diffstat (limited to 'ansible/roles/caddy_add')
-rw-r--r--ansible/roles/caddy_add/tasks/main.yaml64
1 files changed, 64 insertions, 0 deletions
diff --git a/ansible/roles/caddy_add/tasks/main.yaml b/ansible/roles/caddy_add/tasks/main.yaml
new file mode 100644
index 0000000..80382d4
--- /dev/null
+++ b/ansible/roles/caddy_add/tasks/main.yaml
@@ -0,0 +1,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'