Remove special handling of common services and refactor getting service attributes
This commit is contained in:
parent
0347efcb38
commit
ff92241ddb
13 changed files with 92 additions and 49 deletions
playbooks
|
@ -1,29 +1,25 @@
|
|||
class FilterModule(object):
|
||||
def filters(self):
|
||||
return {
|
||||
'my_service_attributes': self.my_service_attributes,
|
||||
'services_for_host': self.services_for_host,
|
||||
'services_to_dnscontrol': self.services_to_dnscontrol,
|
||||
'services_get_backups': self.services_get_backups,
|
||||
'service_get_backups': self.service_get_backups,
|
||||
'service_get_domain': self.service_get_domain,
|
||||
}
|
||||
|
||||
def my_service_attributes(self, services, host, attribute="name"):
|
||||
def services_for_host(self, services, host):
|
||||
result = []
|
||||
for service in services:
|
||||
# only compare the host if it is set
|
||||
if host and service["host"] != host:
|
||||
if not host:
|
||||
result.append(service)
|
||||
continue
|
||||
|
||||
attribute_value = service.get(attribute)
|
||||
if not attribute_value:
|
||||
if service["host"] == host:
|
||||
result.append(service)
|
||||
continue
|
||||
if service["host"] == "*":
|
||||
result.append(service)
|
||||
continue
|
||||
|
||||
if type(attribute_value) == list:
|
||||
result.extend(attribute_value)
|
||||
else:
|
||||
result.append(attribute_value)
|
||||
|
||||
return result
|
||||
|
||||
def find_service(self, services, name):
|
||||
|
@ -34,10 +30,8 @@ class FilterModule(object):
|
|||
|
||||
def services_get_backups(self, all_services, wanted_services):
|
||||
result = []
|
||||
for service in all_services:
|
||||
if service.get("name") in wanted_services:
|
||||
for backup in service.get("backup") or []:
|
||||
result.append(backup["name"])
|
||||
for wanted_service in wanted_services:
|
||||
result.extend(self.service_get_backups(all_services, wanted_service))
|
||||
return result
|
||||
|
||||
def service_get_backups(self, all_services, wanted_service):
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
- name: Run all roles
|
||||
hosts: serguzim_net
|
||||
vars:
|
||||
host_services: "{{ all_services | my_service_attributes(inventory_hostname) | union(common_services) }}"
|
||||
host_services: "{{ all_services | services_for_host(inventory_hostname) }}"
|
||||
roles:
|
||||
- acme_dns
|
||||
- always
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
---
|
||||
backup_list: "{{ all_services | my_service_attributes(inventory_hostname, 'backup') }}"
|
||||
backup_list_all: "{{ all_services | my_service_attributes('', 'backup') }}"
|
||||
backup_list: "{{ host_services | map(attribute='backup') | flatten }}"
|
||||
backup_list_all: "{{ all_services | map(attribute='backup') | flatten }}"
|
||||
|
||||
backup_msg_start: "Backup started"
|
||||
backup_msg_fail: "Backup failed"
|
||||
|
|
|
@ -5,11 +5,8 @@ caddy_acmedns_subd: "{{ vault_caddy.acmedns.subd }}"
|
|||
caddy_acmedns_url: "https://{{ acme_dns.host }}"
|
||||
|
||||
caddy_ports_default:
|
||||
- 80:80
|
||||
- 443:443
|
||||
- 443:443/udp
|
||||
- "{{ host_vpn.ip }}:2019:2019"
|
||||
caddy_ports_extra: "{{ all_services | my_service_attributes(inventory_hostname, 'ports') }}"
|
||||
caddy_ports_extra: "{{ host_services | map(attribute='ports') | flatten }}"
|
||||
caddy_ports: "{{ caddy_ports_default | union(caddy_ports_extra) }}"
|
||||
|
||||
caddy_env:
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
---
|
||||
lego_host_certificates: "{{ all_services | my_service_attributes(inventory_hostname, 'certificates') }}"
|
||||
lego_host_certificates: "{{ host_services | map(attribute='certificates') | flatten }}"
|
||||
|
||||
lego_env:
|
||||
ACME_DNS_API_BASE: https://{{ acme_dns.host }}
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
- name: Run all roles
|
||||
hosts: serguzim_net
|
||||
vars:
|
||||
host_services: "{{ all_services | my_service_attributes(inventory_hostname) | union(common_services) }}"
|
||||
host_services: "{{ all_services | services_for_host(inventory_hostname) }}"
|
||||
tasks:
|
||||
- name: Install software
|
||||
ansible.builtin.include_role:
|
||||
|
@ -21,9 +21,9 @@
|
|||
|
||||
- name: Include service roles
|
||||
ansible.builtin.include_role:
|
||||
name: "{{ services_item }}"
|
||||
name: "{{ services_item.name }}"
|
||||
apply:
|
||||
tags: "{{ services_item }}"
|
||||
tags: "{{ services_item.name }}"
|
||||
tags: always
|
||||
loop: "{{ host_services }}"
|
||||
loop_control:
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
- name: Stop and backup services
|
||||
hosts: serguzim_net
|
||||
vars:
|
||||
host_services: "{{ all_services | my_service_attributes(inventory_hostname) | union(common_services) }}"
|
||||
host_services: "{{ all_services | services_for_host(inventory_hostname) }}"
|
||||
tasks:
|
||||
- name: Get unused services
|
||||
ansible.builtin.include_tasks:
|
||||
|
|
|
@ -14,4 +14,4 @@
|
|||
|
||||
- name: Set unused services
|
||||
ansible.builtin.set_fact:
|
||||
unused_services: "{{ docker_compose_projects_result.stdout_lines | difference(host_services) }}"
|
||||
unused_services: "{{ docker_compose_projects_result.stdout_lines | difference(host_services | map(attribute='name')) }}"
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
- 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') }}"
|
||||
caddy_site_configs_want: "{{ host_services | map(attribute='name') | list_prefix_path_suffix(caddy_config_path, '.conf') }}"
|
||||
|
||||
- name: Remove unwanted caddy site configs
|
||||
ansible.builtin.file:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue