Ensure we only try to enable service if the unit file existed
Some checks failed
CI / test (push) Failing after 2m0s
Trivy / test (push) Successful in 18s
Lint / test (push) Successful in 31s

This commit is contained in:
Miguel Jacq 2025-12-15 12:18:26 +11:00
parent 2c5e901450
commit 2eecb73a49
Signed by: mig5
GPG key ID: 59B3F0C24135C6A9

View file

@ -379,16 +379,26 @@ Unowned /etc config files not attributed to packages or services.
task_parts.append(copy_task(mf, "[Restart service]"))
task_parts.append(
f"""- name: Ensure {unit} is enabled (preserve running state)
f"""
- name: Check if unit exists
ansible.builtin.command: systemctl cat "{{ unit_name }}"
register: _unit_exists
changed_when: false
failed_when: false
- name: Ensure {unit} is enabled (preserve running state)
ansible.builtin.service:
name: "{{{{ unit_name }}}}"
name: "{{ unit_name }}"
enabled: true
when: _unit_exists.rc == 0
- name: Start {unit} if it was active at harvest time
ansible.builtin.service:
name: "{{{{ unit_name }}}}"
state: started
when: {var_prefix}_start | bool
when:
- {var_prefix}_start | bool
- _unit_exists.rc == 0
"""
)