From 4a600ac53192559ba77abb4b59b77b47c3cfdd33 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Mon, 18 Aug 2025 22:10:52 +0200 Subject: [PATCH] Added get_service_name --- filter_plugins/get_service_name.py | 25 ++++++++++++++++++++++ roles/sys-ctl-hlth-msmtp/tasks/01_core.yml | 2 +- roles/sys-svc-msmtp/tasks/01_core.yml | 3 +-- 3 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 filter_plugins/get_service_name.py diff --git a/filter_plugins/get_service_name.py b/filter_plugins/get_service_name.py new file mode 100644 index 00000000..83b51811 --- /dev/null +++ b/filter_plugins/get_service_name.py @@ -0,0 +1,25 @@ +# filter_plugins/get_service_name.py +""" +Custom Ansible filter to build a systemctl unit name (always lowercase). + +Rules: +- If `systemctl_id` ends with '@': drop the '@' and return + "{systemctl_id_without_at}.{software_name}@.{suffix}". +- Else: return "{systemctl_id}{software_name}.{suffix}". +""" + +def get_service_name(systemctl_id, software_name, suffix="service"): + sid = str(systemctl_id).strip().lower() + sw = str(software_name).strip().lower() + sfx = str(suffix).strip().lower() + + if sid.endswith('@'): + base = sid[:-1] # drop the trailing '@' + return f"{base}.{sw}@.{sfx}" + else: + return f"{sid}{sw}.{sfx}" + + +class FilterModule(object): + def filters(self): + return {"get_service_name": get_service_name} diff --git a/roles/sys-ctl-hlth-msmtp/tasks/01_core.yml b/roles/sys-ctl-hlth-msmtp/tasks/01_core.yml index f6967c30..d8fa212c 100644 --- a/roles/sys-ctl-hlth-msmtp/tasks/01_core.yml +++ b/roles/sys-ctl-hlth-msmtp/tasks/01_core.yml @@ -6,5 +6,5 @@ - include_role: name: sys-systemctl vars: - systemctl_on_calendar: "{{ SYS_SCHEDULE_HEALTH_MSMTP }}" + systemctl_on_calendar: "{{ SYS_SCHEDULE_HEALTH_MSMTP }}" systemctl_timer_enabled: true \ No newline at end of file diff --git a/roles/sys-svc-msmtp/tasks/01_core.yml b/roles/sys-svc-msmtp/tasks/01_core.yml index 95b207c5..5c4a7a5e 100644 --- a/roles/sys-svc-msmtp/tasks/01_core.yml +++ b/roles/sys-svc-msmtp/tasks/01_core.yml @@ -1,5 +1,4 @@ -- name: Include dependency 'sys-ctl-hlth-msmtp' - include_role: +- include_role: name: sys-ctl-hlth-msmtp when: run_once_sys_ctl_hlth_msmtp is not defined