While doing maintenance on some roles/playbooks, I came across several template tasks that create systemd service unit files.
A few years ago I had a question about how to validate those: Custom name for a template's temporary file name?
What I wanted was impossible, so I created an issue for it in the systemd github repo.
That eventually got fixed in systemd version 250:
systemd-analyze verifygained support for verifying unit files under an explicitly specified unit name, independently of what the filename actually is.
I just returned to this and can confirm it now does what I wanted back in 2021.
The validate parameter would look like this:
- name: Ensure systemd service unit is available for the foobar service
become: true
ansible.builtin.template:
src: foobar.service.j2
dest: /etc/systemd/system/foobar.service
owner: root
group: root
mode: "0644"
validate: systemd-analyze verify %s:foobar.service