From deb14d2c9462f77be4f526d3a08a7f404589b5b7 Mon Sep 17 00:00:00 2001 From: Sandwich Date: Fri, 20 Feb 2026 22:25:10 +0100 Subject: [PATCH] fix(virtualization): add xen VM existence check and improve changed_when --- roles/virtualization/tasks/xen.yml | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/roles/virtualization/tasks/xen.yml b/roles/virtualization/tasks/xen.yml index 78b7523..8155c97 100644 --- a/roles/virtualization/tasks/xen.yml +++ b/roles/virtualization/tasks/xen.yml @@ -49,6 +49,16 @@ dest: /tmp/xen-{{ hostname }}.cfg mode: "0644" + - name: Check if Xen VM already exists + delegate_to: localhost + ansible.builtin.command: + argv: + - xl + - list + register: virtualization_xen_pre_check + changed_when: false + failed_when: false + - name: Create Xen VM delegate_to: localhost ansible.builtin.command: @@ -58,8 +68,11 @@ - /tmp/xen-{{ hostname }}.cfg register: virtualization_xen_create_result changed_when: virtualization_xen_create_result.rc == 0 + when: >- + not (virtualization_xen_pre_check.stdout | default('') + is search('(?m)^' ~ (hostname | ansible.builtin.regex_escape) ~ '\\s+\\d+\\s')) - - name: Ensure VM is running + - name: Verify VM is running delegate_to: localhost ansible.builtin.command: argv: @@ -67,13 +80,11 @@ - list register: virtualization_xen_list_result changed_when: false - failed_when: false + failed_when: >- + not (virtualization_xen_list_result.stdout | default('') + is search('(?m)^' ~ (hostname | ansible.builtin.regex_escape) ~ '\\s+\\d+\\s')) - name: Set VM created fact ansible.builtin.set_fact: virtualization_vm_created_in_run: true - when: - - virtualization_xen_list_result is defined - - >- - virtualization_xen_list_result.stdout | default('') - is search('(?m)^' ~ (hostname | ansible.builtin.regex_escape) ~ '\\s+\\d+\\s') + when: virtualization_xen_create_result is changed