Refactor filter_plugins

This commit is contained in:
Tobias Reisinger 2026-02-08 19:34:58 +01:00
parent 8ee096949d
commit bdf1f8891b
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
14 changed files with 34 additions and 30 deletions

View file

@ -1,9 +1,8 @@
class FilterModule(object): class FilterModule(object):
def filters(self): def filters(self):
return { return {
'list_prefix_suffix': self.list_prefix_suffix, 'utils_list_prefix_suffix': self.list_prefix_suffix,
'list_prefix_path_suffix': self.list_prefix_path_suffix, 'utils_list_prefix_path_suffix': self.list_prefix_path_suffix,
'postgresql_restart_required': self.postgresql_restart_required,
} }
def list_prefix_suffix(self, values, prefix, suffix): def list_prefix_suffix(self, values, prefix, suffix):
@ -18,9 +17,3 @@ 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 postgresql_restart_required(self, results):
for result in results:
if result.get('restart_required') and result.get('changed'):
return True
return False

View file

@ -46,7 +46,7 @@ backup_yml:
backends: "{{ backup_backends | mandatory }}" backends: "{{ backup_backends | mandatory }}"
locations: "{{ backup_list | map_backup_locations(backup_backends | mandatory, backup_default_hooks) }}" locations: "{{ backup_list | backup_map_locations(backup_backends | mandatory, backup_default_hooks) }}"
global: "{{ backup_global }}" global: "{{ backup_global }}"
@ -55,6 +55,6 @@ backup_yml_all:
backends: "{{ backup_backends | mandatory }}" backends: "{{ backup_backends | mandatory }}"
locations: "{{ backup_list_all | map_backup_locations(backup_backends | mandatory, backup_default_hooks) }}" locations: "{{ backup_list_all | backup_map_locations(backup_backends | mandatory, backup_default_hooks) }}"
global: "{{ backup_global }}" global: "{{ backup_global }}"

View file

@ -9,10 +9,10 @@ def hooks_add_or_create(location, key, value):
class FilterModule(object): class FilterModule(object):
def filters(self): def filters(self):
return { return {
'map_backup_locations': self.map_backup_locations 'backup_map_locations': self.map_locations
} }
def map_backup_locations(self, locations, backends, hooks): def map_locations(self, locations, backends, hooks):
result = {} result = {}
backends_list = list(backends.keys()) backends_list = list(backends.keys())

View file

@ -1,8 +1,8 @@
--- ---
gatus_external_endpoints_backups: "{{ hostvars | hosts_backup_to_gatus() }}" gatus_external_endpoints_backups: "{{ hostvars | gatus_from_hosts_backup() }}"
gatus_endpoints_hosts: "{{ opentofu.hosts | hosts_to_gatus() }}" gatus_endpoints_hosts: "{{ opentofu.hosts | gatus_from_hosts() }}"
gatus_endpoints_services: "{{ all_services | services_to_gatus() }}" gatus_endpoints_services: "{{ all_services | gatus_from_services() }}"
gatus_federation_tester: "https://federationtester.matrix.org/api/report?server_name=msrg.cc" gatus_federation_tester: "https://federationtester.matrix.org/api/report?server_name=msrg.cc"

View file

@ -1,9 +1,9 @@
class FilterModule(object): class FilterModule(object):
def filters(self): def filters(self):
return { return {
'hosts_to_gatus': self.hosts_to_gatus, 'gatus_from_hosts': self.from_hosts,
'hosts_backup_to_gatus': self.hosts_backup_to_gatus, 'gatus_from_hosts_backup': self.from_hosts_backup,
'services_to_gatus': self.services_to_gatus, 'gatus_from_services': self.from_services,
} }
default_alerts = [ default_alerts = [
@ -17,7 +17,7 @@ class FilterModule(object):
}, },
] ]
def hosts_to_gatus(self, hosts): def from_hosts(self, hosts):
result = [] result = []
for host in hosts.values(): for host in hosts.values():
result.append({ result.append({
@ -31,7 +31,7 @@ class FilterModule(object):
}) })
return result return result
def hosts_backup_to_gatus(self, hostvars): def from_hosts_backup(self, hostvars):
result = [] result = []
backup_alerts = [] backup_alerts = []
for a in self.default_alerts: for a in self.default_alerts:
@ -51,7 +51,7 @@ class FilterModule(object):
}) })
return result return result
def services_to_gatus(self, services): def from_services(self, services):
result = [] result = []
default_conditions = [ default_conditions = [

View file

@ -21,7 +21,7 @@
- name: Generate systemd timer names - name: Generate systemd timer names
ansible.builtin.set_fact: ansible.builtin.set_fact:
healthcheck_systemd_timers: "{{ healthcheck_svc.checks | list_prefix_suffix('healthcheck@', '.timer') }}" healthcheck_systemd_timers: "{{ healthcheck_svc.checks | utils_list_prefix_suffix('healthcheck@', '.timer') }}"
- name: Disable unused system timers - name: Disable unused system timers
ansible.builtin.systemd_service: ansible.builtin.systemd_service:

View file

@ -1,10 +1,10 @@
class FilterModule(object): class FilterModule(object):
def filters(self): def filters(self):
return { return {
'acmedns_to_lego': self.acmedns_to_lego, 'lego_from_acmedns': self.from_acmedns,
} }
def acmedns_to_lego(self, acmedns_registered): def from_acmedns(self, acmedns_registered):
result = {} result = {}
for (key, value) in acmedns_registered.items(): for (key, value) in acmedns_registered.items():
result[key] = { result[key] = {

View file

@ -10,7 +10,7 @@
- name: Create the acme-dns-accounts - name: Create the acme-dns-accounts
ansible.builtin.copy: ansible.builtin.copy:
dest: "{{ (lego_config_path, 'acme-dns-accounts.json') | path_join }}" dest: "{{ (lego_config_path, 'acme-dns-accounts.json') | path_join }}"
content: '{{ lego_acmedns_registered | acmedns_to_lego | to_json }}' content: '{{ lego_acmedns_registered | lego_from_acmedns | to_json }}'
mode: "0644" mode: "0644"
- name: Copy the hook script - name: Copy the hook script
ansible.builtin.copy: ansible.builtin.copy:

View file

@ -21,7 +21,7 @@
- name: Generate systemd timer names - name: Generate systemd timer names
ansible.builtin.set_fact: ansible.builtin.set_fact:
lego_systemd_timers: "{{ lego_host_certificates | list_prefix_suffix('lego@', '.timer') }}" lego_systemd_timers: "{{ lego_host_certificates | utils_list_prefix_suffix('lego@', '.timer') }}"
- name: Disable unused system timers - name: Disable unused system timers
ansible.builtin.systemd_service: ansible.builtin.systemd_service:

View file

@ -4,7 +4,7 @@ lgtm_stack_mimir_domain: mimir.serguzim.me
lgtm_stack_alloy_domain: alloy.serguzim.me lgtm_stack_alloy_domain: alloy.serguzim.me
lgtm_stack_loki_domain: "{{ all_services | service_get_domain('loki') }}" lgtm_stack_loki_domain: "{{ all_services | service_get_domain('loki') }}"
lgtm_stack_alloy_jobs: "{{ all_services | services_to_alloy() }}" lgtm_stack_alloy_jobs: "{{ all_services | lgtm_stack_services_to_alloy() }}"
lgtm_stack_grafana_secret_key: "{{ undef() }}" lgtm_stack_grafana_secret_key: "{{ undef() }}"

View file

@ -5,7 +5,7 @@ def transfer_optional_param(source, target, name, target_name=None):
class FilterModule(object): class FilterModule(object):
def filters(self): def filters(self):
return { return {
'services_to_alloy': self.services_to_alloy, 'lgtm_stack_services_to_alloy': self.services_to_alloy,
} }
def services_to_alloy(self, services): def services_to_alloy(self, services):

View file

@ -0,0 +1,11 @@
class FilterModule(object):
def filters(self):
return {
'postgresql_restart_required': self.restart_required,
}
def restart_required(self, results):
for result in results:
if result.get('restart_required') and result.get('changed'):
return True
return False

View file

@ -6,7 +6,7 @@
- name: Map exisiting/wanted caddy site configs - name: Map exisiting/wanted caddy site configs
ansible.builtin.set_fact: ansible.builtin.set_fact:
caddy_site_configs_have: "{{ find_result.files | map(attribute='path') }}" caddy_site_configs_have: "{{ find_result.files | map(attribute='path') }}"
caddy_site_configs_want: "{{ host_services | services_names() | list_prefix_path_suffix(caddy_site_config_path, '.conf') }}" caddy_site_configs_want: "{{ host_services | services_names() | utils_list_prefix_path_suffix(caddy_site_config_path, '.conf') }}"
- name: Remove unwanted caddy site configs - name: Remove unwanted caddy site configs
ansible.builtin.file: ansible.builtin.file: