diff --git a/filter_plugins/get_service_script_path.py b/filter_plugins/get_service_script_path.py index ee355f60..9e879be9 100644 --- a/filter_plugins/get_service_script_path.py +++ b/filter_plugins/get_service_script_path.py @@ -1,18 +1,18 @@ # filter_plugins/get_service_script_path.py # Custom Ansible filter to generate service script paths. -def get_service_script_path(system_service_id, script_type): +def get_service_script_path(systemctl_id, script_type): """ - Build the path to a service script based on system_service_id and type. + Build the path to a service script based on systemctl_id and type. - :param system_service_id: The identifier of the system service. + :param systemctl_id: The identifier of the system service. :param script_type: The script type/extension (e.g., sh, py, yml). :return: The full path string. """ - if not system_service_id or not script_type: - raise ValueError("Both system_service_id and script_type are required") + if not systemctl_id or not script_type: + raise ValueError("Both systemctl_id and script_type are required") - return f"/opt/scripts/{system_service_id}/script.{script_type}" + return f"/opt/scripts/{systemctl_id}/script.{script_type}" class FilterModule(object): diff --git a/roles/sys-ctl-alm-compose/vars/main.yml b/roles/sys-ctl-alm-compose/vars/main.yml index 5603f2b1..112d0aaa 100644 --- a/roles/sys-ctl-alm-compose/vars/main.yml +++ b/roles/sys-ctl-alm-compose/vars/main.yml @@ -1 +1 @@ -systemctl_id: sys-ctl-alm-compose \ No newline at end of file +systemctl_id: sys-ctl-alm-compose@ \ No newline at end of file diff --git a/roles/sys-ctl-alm-email/vars/main.yml b/roles/sys-ctl-alm-email/vars/main.yml index 0a33b685..fc53fd40 100644 --- a/roles/sys-ctl-alm-email/vars/main.yml +++ b/roles/sys-ctl-alm-email/vars/main.yml @@ -1 +1 @@ -systemctl_id: sys-ctl-alm-email +systemctl_id: sys-ctl-alm-email@ diff --git a/roles/sys-ctl-alm-telegram/vars/main.yml b/roles/sys-ctl-alm-telegram/vars/main.yml index a98e4665..20dc5d0f 100644 --- a/roles/sys-ctl-alm-telegram/vars/main.yml +++ b/roles/sys-ctl-alm-telegram/vars/main.yml @@ -1,2 +1 @@ -systemctl_id: sys-ctl-alm-telegram - +systemctl_id: sys-ctl-alm-telegram@ diff --git a/roles/sys-systemctl/tasks/03_base.yml b/roles/sys-systemctl/tasks/03_base.yml index eb027b1d..054b0c03 100644 --- a/roles/sys-systemctl/tasks/03_base.yml +++ b/roles/sys-systemctl/tasks/03_base.yml @@ -1,18 +1,20 @@ +# roles/sys-systemctl/tasks/03_base.yml - name: "find best matching source for service script" set_fact: service_src: >- - {{ - lookup('first_found', { + {{ lookup('first_found', + { 'files': [ - 'templates/script.sh.j2', + 'templates/script.sh.j2', 'templates/script.py.j2', - 'files/script.sh', + 'files/script.sh', 'files/script.py' - ] - }, errors='strict') - }} - when: - - systemctl_copy_files | bool + ], + 'paths': [ systemctl_role_dir ] + }, + errors='strict' + ) }} + when: systemctl_copy_files | bool - name: "Load file logic for '{{ systemctl_id }}'" include_tasks: 04_files.yml diff --git a/roles/sys-systemctl/tasks/05_service.yml b/roles/sys-systemctl/tasks/05_service.yml index 20700a51..37a9f07b 100644 --- a/roles/sys-systemctl/tasks/05_service.yml +++ b/roles/sys-systemctl/tasks/05_service.yml @@ -1,19 +1,38 @@ -- name: "setup systemctl {{ item }} '{{ systemctl_id }}'" +# 1) Find the template (prefer target role, then fall back to this role) +- name: Resolve systemctl template source + set_fact: + systemctl_template_src: >- + {{ lookup( + 'first_found', + { + 'files': [ + 'templates/systemctl@.service.j2', + 'templates/systemctl.service.j2' + ], + 'paths': [ + systemctl_role_dir, + role_path + ] + }, + errors='strict' + ) }} + +# Optional: sanity check with a clear error if truly nothing found +- name: Ensure a systemctl template was found + assert: + that: systemctl_template_src | length > 0 + fail_msg: >- + Could not resolve any systemctl template. Looked in: + {{ systemctl_role_dir }}/templates/ and {{ role_path }}/templates/. + +# 2) Now we may safely derive whether it’s the β€œ@” variant +- name: Flag whether @-template is used + set_fact: + systemctl_uses_at: "{{ (systemctl_template_src | basename) is search('@\\.service\\.j2$') }}" + +# 3) Use it +- name: "setup systemctl '{{ systemctl_id }}'" template: - src: "{{ lookup( - 'first_found', - { - 'files': ['templates/systemctl' ~ item ~ '.service.j2'], - 'paths': [systemctl_role_dir, role_path] - }, - errors='strict' - ) }}" - dest: "{{ [ PATH_SYSTEM_SERVICE_DIR, systemctl_id ~ item ~ SYS_SERVICE_SUFFIX ] | path_join }}" - notify: "{{ 'reload system daemon' if item == '@' else 'refresh systemctl service' }}" - register: services_template - failed_when: - - services_template is failed - - "'Could not find or access' not in services_template.msg" - loop: - - "" - - "@" + src: "{{ systemctl_template_src }}" + dest: "{{ [ PATH_SYSTEM_SERVICE_DIR, systemctl_id | get_service_name(SOFTWARE_NAME) ] | path_join }}" + notify: "{{ 'reload system daemon' if systemctl_uses_at else 'refresh systemctl service' }}" diff --git a/roles/sys-systemctl/tasks/06_timer.yml b/roles/sys-systemctl/tasks/06_timer.yml index 7c9024f8..dda5a5fa 100644 --- a/roles/sys-systemctl/tasks/06_timer.yml +++ b/roles/sys-systemctl/tasks/06_timer.yml @@ -1,4 +1,11 @@ -- set_fact: +- name: Fail if systemctl_id contains "@" + assert: + that: + - "'@' not in systemctl_id" + fail_msg: "Invalid systemctl_id '{{ systemctl_id }}' β†’ must not contain '@'." + +- name: "Make '{{ systemctl_id }}' available for sys-timer" + set_fact: systemctl_timer_service: "{{ systemctl_id }}" - name: "include role for sys-timer for {{ systemctl_timer_service }}" diff --git a/roles/sys-systemctl/templates/systemctl.service.j2 b/roles/sys-systemctl/templates/systemctl.service.j2 index 597500cc..2e00f7cd 100644 --- a/roles/sys-systemctl/templates/systemctl.service.j2 +++ b/roles/sys-systemctl/templates/systemctl.service.j2 @@ -1,5 +1,5 @@ [Unit] -Description=Service for {{ SOFTWARE_NAME }} role 'systemctl_id' (DEFAULT TEMPLATE) +Description=Service for {{ SOFTWARE_NAME }} role '{{ systemctl_id }}' (DEFAULT TEMPLATE) OnFailure={{ SYS_SERVICE_ON_FAILURE_COMPOSE }} [Service] diff --git a/roles/sys-systemctl/vars/main.yml b/roles/sys-systemctl/vars/main.yml index 8d7eafd1..de096cc0 100644 --- a/roles/sys-systemctl/vars/main.yml +++ b/roles/sys-systemctl/vars/main.yml @@ -1,6 +1,11 @@ UNIT_SUFFIX_REMOVER_PACKAGE: "unsure" -systemctl_script_dir: "{{ [ PATH_SYSTEMCTL_SCRIPTS, systemctl_id ] | path_join }}" -systemctl_role_dir: "{{ playbook_dir }}/roles/{{ systemctl_id }}" -systemctl_copy_files: true # When set to false file copying will be skipped -systemctl_timer_enabled: false # When set to true timmer will be loaded -systemctl_state: "{{ omit }}" \ No newline at end of file + +## Paths +systemctl_role_name: "{{ systemctl_id | regex_replace('@','') }}" +systemctl_role_dir: "{{ [ playbook_dir, 'roles', systemctl_role_name ] | path_join }}" +systemctl_script_dir: "{{ [ PATH_SYSTEMCTL_SCRIPTS, systemctl_id ] | path_join }}" + +## Settings +systemctl_copy_files: true # When set to false file copying will be skipped +systemctl_timer_enabled: false # When set to true timmer will be loaded +systemctl_state: "{{ omit }}" \ No newline at end of file