--- - name: Physical install safety confirmation when: system_cfg.type == "physical" ansible.builtin.assert: that: - physical_install_confirmed | default(false) | bool fail_msg: >- DANGER: Physical install will WIPE {{ install_drive }} on {{ inventory_hostname }}. Set physical_install_confirmed=true in inventory to proceed. quiet: true - name: VM existence protection check when: system_cfg.type == "virtual" block: - name: Check if VM already exists on libvirt when: hypervisor_type == "libvirt" delegate_to: localhost become: false community.libvirt.virt: command: list_vms register: system_check_libvirt_existing_vms changed_when: false failed_when: false - name: Abort if VM already exists on libvirt when: hypervisor_type == "libvirt" ansible.builtin.assert: that: - hostname not in system_check_libvirt_existing_vms.domains | default([]) fail_msg: | VM {{ hostname }} already exists on libvirt hypervisor. To avoid data loss, the playbook will not overwrite or delete existing VMs. Please choose a different hostname or remove the existing VM manually before proceeding. quiet: true - name: Check VM existence on Proxmox when: hypervisor_type == "proxmox" delegate_to: localhost become: false module_defaults: community.proxmox.proxmox_vm_info: api_host: "{{ hypervisor_cfg.url }}" api_user: "{{ hypervisor_cfg.username }}" api_password: "{{ hypervisor_cfg.password }}" block: - name: Query Proxmox for existing VM community.proxmox.proxmox_vm_info: node: "{{ hypervisor_cfg.host }}" vmid: "{{ system_cfg.id }}" name: "{{ hostname }}" type: qemu register: system_check_proxmox_check_result changed_when: false no_log: true - name: Abort if VM already exists on Proxmox ansible.builtin.assert: that: - system_check_proxmox_check_result.proxmox_vms | default([]) | length == 0 fail_msg: | VM {{ hostname }} (ID: {{ system_cfg.id }}) already exists on Proxmox hypervisor. To avoid data loss, the playbook will not overwrite or delete existing VMs. Please choose a different hostname or VM ID, or remove the existing VM manually before proceeding. quiet: true - name: Check VM existence in vCenter when: hypervisor_type == "vmware" delegate_to: localhost module_defaults: community.vmware.vmware_guest_info: hostname: "{{ hypervisor_cfg.url }}" username: "{{ hypervisor_cfg.username }}" password: "{{ hypervisor_cfg.password }}" validate_certs: "{{ hypervisor_cfg.certs | bool }}" block: - name: Query vCenter for existing VM community.vmware.vmware_guest_info: datacenter: "{{ hypervisor_cfg.datacenter }}" name: "{{ hostname }}" folder: "{{ system_cfg.path if system_cfg.path | length > 0 else omit }}" register: system_check_vmware_check_result failed_when: false changed_when: false no_log: true - name: Fail if vCenter lookup failed unexpectedly ansible.builtin.assert: that: - not system_check_vmware_check_result.failed or (system_check_vmware_check_result.msg is search('non-existing VM')) fail_msg: | Unable to verify VM existence in vCenter. {{ system_check_vmware_check_result.msg | default('Unknown error') }} quiet: true - name: Abort if VM already exists in vCenter ansible.builtin.assert: that: - system_check_vmware_check_result.instance is not defined fail_msg: | VM {{ hostname }} already exists in vCenter. To avoid data loss, the playbook will not overwrite or delete existing VMs. Please choose a different hostname or remove the existing VM manually before proceeding. quiet: true - name: Check if VM already exists on Xen when: hypervisor_type == "xen" delegate_to: localhost ansible.builtin.command: argv: - xl - list register: system_check_xen_existing_vms changed_when: false failed_when: false - name: Abort if VM already exists on Xen when: hypervisor_type == "xen" ansible.builtin.assert: that: - >- not ( system_check_xen_existing_vms.stdout | default('') is search('(?m)^' ~ (hostname | ansible.builtin.regex_escape) ~ '\\s+\\d+\\s') ) fail_msg: | VM {{ hostname }} already exists on Xen hypervisor. To avoid data loss, the playbook will not overwrite or delete existing VMs. Please choose a different hostname or remove the existing VM manually before proceeding. quiet: true