Handle missing libvirt services in evacuate hook

On Debian 13 (Trixie), libvirt packaging is modularized and
the libvirt-daemon-lock package (providing virtlockd) is
optional. The evacuate hook previously assumed all libvirt
services were installed and failed when stopping/starting
missing units.

Extract a reusable manage_libvirt_service.yaml task file that
checks if a service exists via systemctl list-unit-files
before managing its units. This prevents failures when
optional libvirt packages are not installed and future-proofs
against further packaging changes.

Generated-By: claude-code
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Change-Id: Ie84e2e8ab2d3065b1562ee5e256fa163541955f7
Signed-off-by: Sean Mooney <work@seanmooney.info>
This commit is contained in:
Sean Mooney
2026-02-17 18:22:22 +00:00
parent f3d9188a93
commit 6c9110bb8b
2 changed files with 70 additions and 34 deletions
+28 -14
View File
@@ -48,20 +48,27 @@
state: destroyed
with_items: "{{ subnode_vms.list_vms }}"
- name: Stop libvirtd on "{{ inventory_hostname }}"
become: true
systemd:
name: "{{ item }}"
state: stopped
enabled: no
with_items:
- name: Stop libvirt services on "{{ inventory_hostname }}"
include_tasks: manage_libvirt_service.yaml
vars:
service_name: "{{ item.name }}"
service_state: stopped
service_enabled: "no"
service_units: "{{ item.units }}"
loop:
- name: libvirtd
units:
- libvirtd.service
- libvirtd.socket
- libvirtd-admin.socket
- libvirtd-ro.socket
- name: virtlogd
units:
- virtlogd.service
- virtlogd-admin.socket
- virtlogd.socket
- name: virtlockd
units:
- virtlockd.service
- virtlockd-admin.socket
- virtlockd.socket
@@ -73,20 +80,27 @@
environment:
CONTROLLER_HOSTNAME: "{{ hostvars['controller']['ansible_hostname'] }}"
- name: Start libvirtd on "{{ inventory_hostname }}"
become: true
systemd:
name: "{{ item }}"
state: started
enabled: yes
with_items:
- name: Start libvirt services on "{{ inventory_hostname }}"
include_tasks: manage_libvirt_service.yaml
vars:
service_name: "{{ item.name }}"
service_state: started
service_enabled: "yes"
service_units: "{{ item.units }}"
loop:
- name: libvirtd
units:
- libvirtd.service
- libvirtd.socket
- libvirtd-admin.socket
- libvirtd-ro.socket
- name: virtlogd
units:
- virtlogd.service
- virtlogd-admin.socket
- virtlogd.socket
- name: virtlockd
units:
- virtlockd.service
- virtlockd-admin.socket
- virtlockd.socket
@@ -0,0 +1,22 @@
# Manage a libvirt service (stop/start) only if it is installed.
# Parameters:
# service_name: base name of the service (e.g. "libvirtd", "virtlogd", "virtlockd")
# service_state: "stopped" or "started"
# service_enabled: "yes" or "no"
# service_units: list of unit names to manage
- name: "Check if {{ service_name }} is installed"
become: true
command: systemctl list-unit-files {{ service_name }}.service
register: _service_check
ignore_errors: true
changed_when: false
- name: "Manage {{ service_name }} units"
become: true
systemd:
name: "{{ item }}"
state: "{{ service_state }}"
enabled: "{{ service_enabled }}"
with_items: "{{ service_units }}"
when: _service_check is succeeded