mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2025-08-30 07:18:09 +02:00
refactor: improve get_service_name suffix handling and handler usage
- Updated filter_plugins/get_service_name.py: * Default suffix handling: auto-select .service (no '@') or .timer (with '@') * Explicit False disables suffix entirely * Explicit string suffix still supported - Updated sys-systemctl handler to use new filter instead of SYS_SERVICE_SUFFIX - Extended unit tests to cover new suffix behavior Ref: https://chat.openai.com/share/8c2de9e6-daa0-44dd-ae13-d7a7d8d8b6d9
This commit is contained in:
@@ -1,23 +1,36 @@
|
||||
# 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}".
|
||||
"{systemctl_id_without_at}.{software_name}@{suffix_handling}".
|
||||
- Else: return "{systemctl_id}.{software_name}{suffix_handling}".
|
||||
|
||||
Suffix handling:
|
||||
- Default "" → automatically pick:
|
||||
- ".service" if no '@' in systemctl_id
|
||||
- ".timer" if '@' in systemctl_id
|
||||
- Explicit False → no suffix at all
|
||||
- Any string → ".{suffix}" (lowercased)
|
||||
"""
|
||||
|
||||
def get_service_name(systemctl_id, software_name, suffix="service"):
|
||||
def get_service_name(systemctl_id, software_name, suffix=""):
|
||||
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}"
|
||||
# Determine suffix
|
||||
if suffix is False:
|
||||
sfx = "" # no suffix at all
|
||||
elif suffix == "" or suffix is None:
|
||||
sfx = ".timer" if sid.endswith("@") else ".service"
|
||||
else:
|
||||
return f"{sid}.{sw}.{sfx}"
|
||||
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):
|
||||
|
Reference in New Issue
Block a user