refactor(virt): adopt module_defaults for hypervisor credentials
This commit is contained in:
@@ -33,70 +33,75 @@
|
||||
Please choose a different hostname or remove the existing VM manually before proceeding.
|
||||
quiet: true
|
||||
|
||||
- name: Check if VM already exists on Proxmox
|
||||
- name: Check VM existence 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
|
||||
module_defaults:
|
||||
community.proxmox.proxmox_vm_info:
|
||||
api_host: "{{ hypervisor_cfg.url }}"
|
||||
api_user: "{{ hypervisor_cfg.username }}"
|
||||
api_password: "{{ hypervisor_cfg.password }}"
|
||||
no_log: true
|
||||
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
|
||||
|
||||
- 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: 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 if VM already exists in vCenter
|
||||
- name: Check VM existence 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
|
||||
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 }}"
|
||||
no_log: true
|
||||
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
|
||||
|
||||
- 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: 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
|
||||
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: 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"
|
||||
|
||||
Reference in New Issue
Block a user