Add auto-cleaning for caddy sites
This commit is contained in:
parent
7a1a8a348a
commit
aa9c76a622
7 changed files with 40 additions and 14 deletions
|
@ -2,6 +2,7 @@ class FilterModule(object):
|
||||||
def filters(self):
|
def filters(self):
|
||||||
return {
|
return {
|
||||||
'list_prefix_suffix': self.list_prefix_suffix,
|
'list_prefix_suffix': self.list_prefix_suffix,
|
||||||
|
'list_prefix_path_suffix': self.list_prefix_path_suffix,
|
||||||
}
|
}
|
||||||
|
|
||||||
def list_prefix_suffix(self, values, prefix, suffix):
|
def list_prefix_suffix(self, values, prefix, suffix):
|
||||||
|
@ -9,3 +10,10 @@ class FilterModule(object):
|
||||||
for value in values:
|
for value in values:
|
||||||
result.append(f"{prefix}{value}{suffix}")
|
result.append(f"{prefix}{value}{suffix}")
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
def list_prefix_path_suffix(self, values, prefix, suffix):
|
||||||
|
prefix = prefix if prefix.endswith('/') else f"{prefix}/"
|
||||||
|
result = []
|
||||||
|
for value in values:
|
||||||
|
result.append(f"{prefix}{value}{suffix}")
|
||||||
|
return result
|
||||||
|
|
23
playbooks/tasks/reload-caddy.yml
Normal file
23
playbooks/tasks/reload-caddy.yml
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
- name: Find existing caddy site configs
|
||||||
|
ansible.builtin.find:
|
||||||
|
paths: "{{ caddy_config_path }}"
|
||||||
|
recurse: no
|
||||||
|
register: find_result
|
||||||
|
- name: Map exisiting/wanted caddy site configs
|
||||||
|
ansible.builtin.set_fact:
|
||||||
|
caddy_site_configs_have: "{{ find_result.files | map(attribute='path') }}"
|
||||||
|
caddy_site_configs_want: "{{ all_services | my_service_attributes(inventory_hostname) | list_prefix_path_suffix(caddy_config_path, '.conf') }}"
|
||||||
|
|
||||||
|
- name: Remove unwanted caddy site configs
|
||||||
|
ansible.builtin.file:
|
||||||
|
path: "{{ item }}"
|
||||||
|
state: absent
|
||||||
|
loop: "{{ caddy_site_configs_have | difference(caddy_site_configs_want) }}"
|
||||||
|
become: true
|
||||||
|
|
||||||
|
- name: Reload caddy container
|
||||||
|
ansible.builtin.command:
|
||||||
|
cmd: docker compose exec app sh -c "caddy validate --config /etc/caddy/Caddyfile && caddy reload --config /etc/caddy/Caddyfile"
|
||||||
|
chdir: "{{ caddy_path }}"
|
||||||
|
when: "'local-dev' != inventory_hostname"
|
||||||
|
changed_when: true
|
|
@ -2,11 +2,7 @@
|
||||||
- name: Template caddy site
|
- name: Template caddy site
|
||||||
ansible.builtin.template:
|
ansible.builtin.template:
|
||||||
src: caddy_site.conf.j2
|
src: caddy_site.conf.j2
|
||||||
dest: "{{ (caddy_config_path, svc.domain + '.conf') | path_join }}"
|
dest: "{{ (caddy_config_path, role_name + '.conf') | path_join }}"
|
||||||
mode: "0644"
|
mode: "0644"
|
||||||
notify:
|
notify:
|
||||||
- Reload caddy
|
- Reload caddy
|
||||||
|
|
||||||
- name: Register caddy site
|
|
||||||
ansible.builtin.set_fact:
|
|
||||||
managed_sites: "{{ managed_sites + [svc.domain + '.conf'] }}"
|
|
||||||
|
|
|
@ -36,7 +36,9 @@
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endmacro -%}
|
{% endmacro -%}
|
||||||
|
|
||||||
|
{% if svc.domain|default(false) %}
|
||||||
{{ caddy_site(svc) }}
|
{{ caddy_site(svc) }}
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
{%- for extra_svc in svc.extra_svcs|default([]) %}
|
{%- for extra_svc in svc.extra_svcs|default([]) %}
|
||||||
{{ caddy_site(extra_svc) }}
|
{{ caddy_site(extra_svc) }}
|
||||||
|
|
|
@ -1,7 +1,3 @@
|
||||||
---
|
---
|
||||||
- name: Reload caddy
|
- name: Reload caddy
|
||||||
ansible.builtin.command:
|
ansible.builtin.include_tasks: tasks/reload-caddy.yml
|
||||||
cmd: docker compose exec app sh -c "caddy validate --config /etc/caddy/Caddyfile && caddy reload --config /etc/caddy/Caddyfile"
|
|
||||||
chdir: "{{ caddy_path }}"
|
|
||||||
when: "'local-dev' != inventory_hostname"
|
|
||||||
changed_when: true
|
|
||||||
|
|
|
@ -3,9 +3,8 @@
|
||||||
ansible.builtin.import_tasks: tasks/set-default-facts.yml
|
ansible.builtin.import_tasks: tasks/set-default-facts.yml
|
||||||
|
|
||||||
- name: Deploy extra services
|
- name: Deploy extra services
|
||||||
|
vars:
|
||||||
|
svc: "{{ extra_services_svc }}"
|
||||||
block:
|
block:
|
||||||
- name: Import tasks to template the site and functions for the reverse proxy
|
- name: Import tasks to template the site and functions for the reverse proxy
|
||||||
ansible.builtin.include_tasks: tasks/steps/template-site-config.yml
|
ansible.builtin.include_tasks: tasks/steps/template-site-config.yml
|
||||||
loop: "{{ extra_services_all }}"
|
|
||||||
loop_control:
|
|
||||||
loop_var: svc
|
|
||||||
|
|
|
@ -1,2 +1,4 @@
|
||||||
---
|
---
|
||||||
extra_services_all: "{{ vault_extra_services }}"
|
extra_services_svc:
|
||||||
|
name: extra_services
|
||||||
|
extra_svcs: "{{ vault_extra_services }}"
|
||||||
|
|
Loading…
Reference in a new issue