Another fix for systemd unit file
Some checks failed
Trivy / test (push) Successful in 19s
CI / test (push) Failing after 1m37s
Lint / test (push) Failing after 28s

This commit is contained in:
Miguel Jacq 2025-12-15 12:28:21 +11:00
parent 283f3286f2
commit ac0c884c39
Signed by: mig5
GPG key ID: 59B3F0C24135C6A9
2 changed files with 15 additions and 13 deletions

View file

@ -380,25 +380,27 @@ Unowned /etc config files not attributed to packages or services.
task_parts.append( task_parts.append(
f""" f"""
- name: Check if unit exists - name: Probe whether systemd unit exists and is manageable
ansible.builtin.command: systemctl cat "{{ unit_name }}" ansible.builtin.systemd:
register: _unit_exists name: "{{ unit_name }}"
changed_when: false check_mode: true
register: _unit_probe
failed_when: false failed_when: false
changed_when: false
- name: Ensure {unit} is enabled (preserve running state) - name: Ensure {{ unit_name }} is enabled (preserve running state)
ansible.builtin.service: ansible.builtin.systemd:
name: "{{ unit_name }}" name: "{{ unit_name }}"
enabled: true enabled: true
when: _unit_exists.rc == 0 when: _unit_probe is succeeded
- name: Start {unit} if it was active at harvest time - name: Start {{ unit_name }} if it was active at harvest time
ansible.builtin.service: ansible.builtin.systemd:
name: "{{{{ unit_name }}}}" name: "{{ unit_name }}"
state: started state: started
when: when:
- {var_prefix}_start | bool - _unit_probe is succeeded
- _unit_exists.rc == 0 - {{ var_prefix }}_start | bool
""" """
) )

View file

@ -96,7 +96,7 @@ def test_manifest_writes_roles_and_playbook_with_clean_when(tmp_path: Path):
# Service role: conditional start must be a clean Ansible expression # Service role: conditional start must be a clean Ansible expression
tasks = (out / "roles" / "foo" / "tasks" / "main.yml").read_text(encoding="utf-8") tasks = (out / "roles" / "foo" / "tasks" / "main.yml").read_text(encoding="utf-8")
assert "when:\n - foo_start | bool\n - _unit_exists.rc == 0\n" in tasks assert "when:\n - _unit_probe is succeeded\n - { var_prefix }_start | bool\n" in tasks
# Ensure we didn't emit deprecated/broken '{{ }}' delimiters in when: # Ensure we didn't emit deprecated/broken '{{ }}' delimiters in when:
for line in tasks.splitlines(): for line in tasks.splitlines():
if line.lstrip().startswith("when:"): if line.lstrip().startswith("when:"):