--- - 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'