110 lines
4.2 KiB
YAML
110 lines
4.2 KiB
YAML
---
|
|
- 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 if VM already exists on Proxmox
|
|
when: hypervisor_type == "proxmox"
|
|
delegate_to: localhost
|
|
become: false
|
|
community.proxmox.proxmox_vm_info:
|
|
api_host: "{{ hypervisor_cfg.url }}"
|
|
api_user: "{{ hypervisor_cfg.username }}"
|
|
api_password: "{{ hypervisor_cfg.password }}"
|
|
node: "{{ hypervisor_cfg.host }}"
|
|
vmid: "{{ system_cfg.id }}"
|
|
name: "{{ hostname }}"
|
|
type: qemu
|
|
register: system_check_proxmox_check_result
|
|
changed_when: false
|
|
|
|
- name: Abort if VM already exists on Proxmox
|
|
when: hypervisor_type == "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 if VM already exists in vCenter
|
|
when: hypervisor_type == "vmware"
|
|
delegate_to: localhost
|
|
community.vmware.vmware_guest_info:
|
|
hostname: "{{ hypervisor_cfg.url }}"
|
|
username: "{{ hypervisor_cfg.username }}"
|
|
password: "{{ hypervisor_cfg.password }}"
|
|
validate_certs: "{{ hypervisor_cfg.certs | bool }}"
|
|
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
|
|
|
|
- name: Fail if vCenter lookup failed unexpectedly
|
|
when: hypervisor_type == "vmware"
|
|
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
|
|
when: hypervisor_type == "vmware"
|
|
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:
|
|
- hostname not in system_check_xen_existing_vms.stdout | default('')
|
|
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
|