[PATCH 4/8] nixos: destroy the libvirt domain before cleaning up local artefacts

Daniel Gomez <[email protected]> Thu, 23 Apr 2026 00:16:45 +0200
Newsgroups dev.linux.lists.kdevops
Message-ID <[email protected]>
From: Daniel Gomez <[email protected]>

The destroy play tore down the local disk image, wrapper script
and symlink but never told libvirt about it, so domains brought
up through the libvirt bringup path stayed defined and running
after make destroy reported success. Add virsh destroy and virsh
undefine under the same LIBVIRT_DEFAULT_URI the bringup play
uses, both failed_when: false so a partial state across the
guest inventory does not abort the rest of the teardown.

Fixes: 3089c3fec57 ("nixos: add NixOS support as third bringup option with libvirt integration")
Generated-by: Claude AI
Signed-off-by: Daniel Gomez <[email protected]>
---
 playbooks/nixos.yml | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/playbooks/nixos.yml b/playbooks/nixos.yml
index 26285520..d6aa2283 100644
--- a/playbooks/nixos.yml
+++ b/playbooks/nixos.yml
@@ -471,6 +471,20 @@
     - "{{ playbook_dir }}/../extra_vars.yaml"
   tags: [destroy, never]
   tasks:
+    - name: Shut down VMs via libvirt
+      ansible.builtin.command: virsh destroy "{{ item }}"
+      loop: "{{ groups['all'] | reject('equalto', 'localhost') | list }}"
+      failed_when: false
+      environment:
+        LIBVIRT_DEFAULT_URI: "{{ libvirt_uri }}"
+
+    - name: Remove VM definitions from libvirt
+      ansible.builtin.command: virsh undefine "{{ item }}"
+      loop: "{{ groups['all'] | reject('equalto', 'localhost') | list }}"
+      failed_when: false
+      environment:
+        LIBVIRT_DEFAULT_URI: "{{ libvirt_uri }}"
+
     - name: Stop VMs using wrapper scripts
       ansible.builtin.command: "{{ nixos_storage_dir }}/run-{{ item }}-wrapper.sh stop"
       loop: "{{ groups['all'] | reject('equalto', 'localhost') | list }}"

-- 
2.53.0