refactor(virt): adopt module_defaults for hypervisor credentials

This commit is contained in:
2026-02-20 21:16:33 +01:00
parent 5ca1c7f570
commit ba6be037ac
5 changed files with 273 additions and 261 deletions

View File

@@ -3,12 +3,20 @@
when: hypervisor_type == "proxmox" when: hypervisor_type == "proxmox"
delegate_to: localhost delegate_to: localhost
become: false become: false
block: module_defaults:
- name: Cleanup Setup Disks
community.proxmox.proxmox_disk: community.proxmox.proxmox_disk:
api_host: "{{ hypervisor_cfg.url }}" api_host: "{{ hypervisor_cfg.url }}"
api_user: "{{ hypervisor_cfg.username }}" api_user: "{{ hypervisor_cfg.username }}"
api_password: "{{ hypervisor_cfg.password }}" api_password: "{{ hypervisor_cfg.password }}"
community.proxmox.proxmox_kvm:
api_host: "{{ hypervisor_cfg.url }}"
api_user: "{{ hypervisor_cfg.username }}"
api_password: "{{ hypervisor_cfg.password }}"
node: "{{ hypervisor_cfg.host }}"
no_log: true
block:
- name: Cleanup Setup Disks
community.proxmox.proxmox_disk:
name: "{{ hostname }}" name: "{{ hostname }}"
vmid: "{{ system_cfg.id }}" vmid: "{{ system_cfg.id }}"
disk: "{{ item }}" disk: "{{ item }}"
@@ -17,14 +25,8 @@
- ide0 - ide0
- ide2 - ide2
failed_when: false failed_when: false
no_log: true
- name: Start the VM - name: Start the VM
community.proxmox.proxmox_kvm: community.proxmox.proxmox_kvm:
api_host: "{{ hypervisor_cfg.url }}"
api_user: "{{ hypervisor_cfg.username }}"
api_password: "{{ hypervisor_cfg.password }}"
node: "{{ hypervisor_cfg.host }}"
vmid: "{{ system_cfg.id }}" vmid: "{{ system_cfg.id }}"
state: restarted state: restarted
no_log: true

View File

@@ -3,14 +3,23 @@
when: hypervisor_type == "vmware" when: hypervisor_type == "vmware"
delegate_to: localhost delegate_to: localhost
become: false become: false
block: module_defaults:
- name: Remove CD-ROM from VM in vCenter
community.vmware.vmware_guest: community.vmware.vmware_guest:
hostname: "{{ hypervisor_cfg.url }}" hostname: "{{ hypervisor_cfg.url }}"
username: "{{ hypervisor_cfg.username }}" username: "{{ hypervisor_cfg.username }}"
password: "{{ hypervisor_cfg.password }}" password: "{{ hypervisor_cfg.password }}"
validate_certs: "{{ hypervisor_cfg.certs | bool }}" validate_certs: "{{ hypervisor_cfg.certs | bool }}"
datacenter: "{{ hypervisor_cfg.datacenter }}" datacenter: "{{ hypervisor_cfg.datacenter }}"
vmware.vmware.vm_powerstate:
hostname: "{{ hypervisor_cfg.url }}"
username: "{{ hypervisor_cfg.username }}"
password: "{{ hypervisor_cfg.password }}"
validate_certs: "{{ hypervisor_cfg.certs | bool }}"
datacenter: "{{ hypervisor_cfg.datacenter }}"
no_log: true
block:
- name: Remove CD-ROM from VM in vCenter
community.vmware.vmware_guest:
name: "{{ hostname }}" name: "{{ hostname }}"
cdrom: >- cdrom: >-
{{ {{
@@ -40,15 +49,8 @@
) )
}} }}
failed_when: false failed_when: false
no_log: true
- name: Start VM in vCenter - name: Start VM in vCenter
vmware.vmware.vm_powerstate: vmware.vmware.vm_powerstate:
hostname: "{{ hypervisor_cfg.url }}"
username: "{{ hypervisor_cfg.username }}"
password: "{{ hypervisor_cfg.password }}"
validate_certs: "{{ hypervisor_cfg.certs | bool }}"
datacenter: "{{ hypervisor_cfg.datacenter }}"
name: "{{ hostname }}" name: "{{ hostname }}"
state: powered-on state: powered-on
no_log: true

View File

@@ -33,24 +33,27 @@
Please choose a different hostname or remove the existing VM manually before proceeding. Please choose a different hostname or remove the existing VM manually before proceeding.
quiet: true quiet: true
- name: Check if VM already exists on Proxmox - name: Check VM existence on Proxmox
when: hypervisor_type == "proxmox" when: hypervisor_type == "proxmox"
delegate_to: localhost delegate_to: localhost
become: false become: false
module_defaults:
community.proxmox.proxmox_vm_info: community.proxmox.proxmox_vm_info:
api_host: "{{ hypervisor_cfg.url }}" api_host: "{{ hypervisor_cfg.url }}"
api_user: "{{ hypervisor_cfg.username }}" api_user: "{{ hypervisor_cfg.username }}"
api_password: "{{ hypervisor_cfg.password }}" api_password: "{{ hypervisor_cfg.password }}"
no_log: true
block:
- name: Query Proxmox for existing VM
community.proxmox.proxmox_vm_info:
node: "{{ hypervisor_cfg.host }}" node: "{{ hypervisor_cfg.host }}"
vmid: "{{ system_cfg.id }}" vmid: "{{ system_cfg.id }}"
name: "{{ hostname }}" name: "{{ hostname }}"
type: qemu type: qemu
register: system_check_proxmox_check_result register: system_check_proxmox_check_result
changed_when: false changed_when: false
no_log: true
- name: Abort if VM already exists on Proxmox - name: Abort if VM already exists on Proxmox
when: hypervisor_type == "proxmox"
ansible.builtin.assert: ansible.builtin.assert:
that: that:
- system_check_proxmox_check_result.proxmox_vms | default([]) | length == 0 - system_check_proxmox_check_result.proxmox_vms | default([]) | length == 0
@@ -60,24 +63,27 @@
Please choose a different hostname or VM ID, or remove the existing VM manually before proceeding. Please choose a different hostname or VM ID, or remove the existing VM manually before proceeding.
quiet: true quiet: true
- name: Check if VM already exists in vCenter - name: Check VM existence in vCenter
when: hypervisor_type == "vmware" when: hypervisor_type == "vmware"
delegate_to: localhost delegate_to: localhost
module_defaults:
community.vmware.vmware_guest_info: community.vmware.vmware_guest_info:
hostname: "{{ hypervisor_cfg.url }}" hostname: "{{ hypervisor_cfg.url }}"
username: "{{ hypervisor_cfg.username }}" username: "{{ hypervisor_cfg.username }}"
password: "{{ hypervisor_cfg.password }}" password: "{{ hypervisor_cfg.password }}"
validate_certs: "{{ hypervisor_cfg.certs | bool }}" 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 }}" datacenter: "{{ hypervisor_cfg.datacenter }}"
name: "{{ hostname }}" name: "{{ hostname }}"
folder: "{{ system_cfg.path if system_cfg.path | length > 0 else omit }}" folder: "{{ system_cfg.path if system_cfg.path | length > 0 else omit }}"
register: system_check_vmware_check_result register: system_check_vmware_check_result
failed_when: false failed_when: false
changed_when: false changed_when: false
no_log: true
- name: Fail if vCenter lookup failed unexpectedly - name: Fail if vCenter lookup failed unexpectedly
when: hypervisor_type == "vmware"
ansible.builtin.assert: ansible.builtin.assert:
that: that:
- not system_check_vmware_check_result.failed - not system_check_vmware_check_result.failed
@@ -88,7 +94,6 @@
quiet: true quiet: true
- name: Abort if VM already exists in vCenter - name: Abort if VM already exists in vCenter
when: hypervisor_type == "vmware"
ansible.builtin.assert: ansible.builtin.assert:
that: that:
- system_check_vmware_check_result.instance is not defined - system_check_vmware_check_result.instance is not defined

View File

@@ -1,6 +1,14 @@
--- ---
- name: Deploy VM on Proxmox - name: Deploy VM on Proxmox
delegate_to: localhost delegate_to: localhost
module_defaults:
community.proxmox.proxmox_kvm:
api_host: "{{ hypervisor_cfg.url }}"
api_user: "{{ hypervisor_cfg.username }}"
api_password: "{{ hypervisor_cfg.password }}"
node: "{{ hypervisor_cfg.host }}"
block:
- name: Create VM on Proxmox
vars: vars:
virtualization_proxmox_scsi: >- virtualization_proxmox_scsi: >-
{%- set out = {} -%} {%- set out = {} -%}
@@ -31,13 +39,9 @@
{%- endfor -%} {%- endfor -%}
{{ out }} {{ out }}
community.proxmox.proxmox_kvm: community.proxmox.proxmox_kvm:
api_host: "{{ hypervisor_cfg.url }}"
api_user: "{{ hypervisor_cfg.username }}"
api_password: "{{ hypervisor_cfg.password }}"
ciuser: "{{ system_cfg.users[0].name }}" ciuser: "{{ system_cfg.users[0].name }}"
cipassword: "{{ system_cfg.users[0].password }}" cipassword: "{{ system_cfg.users[0].password }}"
ciupgrade: false ciupgrade: false
node: "{{ hypervisor_cfg.host }}"
vmid: "{{ system_cfg.id }}" vmid: "{{ system_cfg.id }}"
name: "{{ hostname }}" name: "{{ hostname }}"
cpu: host cpu: host
@@ -76,20 +80,15 @@
state: present state: present
no_log: true no_log: true
- name: Start VM on Proxmox - name: Start VM on Proxmox
delegate_to: localhost
community.proxmox.proxmox_kvm: community.proxmox.proxmox_kvm:
api_host: "{{ hypervisor_cfg.url }}"
api_user: "{{ hypervisor_cfg.username }}"
api_password: "{{ hypervisor_cfg.password }}"
node: "{{ hypervisor_cfg.host }}"
name: "{{ hostname }}" name: "{{ hostname }}"
vmid: "{{ system_cfg.id }}" vmid: "{{ system_cfg.id }}"
state: started state: started
no_log: true no_log: true
register: virtualization_proxmox_start_result register: virtualization_proxmox_start_result
- name: Set VM created fact - name: Set VM created fact
ansible.builtin.set_fact: ansible.builtin.set_fact:
virtualization_vm_created_in_run: true virtualization_vm_created_in_run: true
when: virtualization_proxmox_start_result is defined and virtualization_proxmox_start_result.changed | bool when: virtualization_proxmox_start_result is defined and virtualization_proxmox_start_result.changed | bool

View File

@@ -11,9 +11,30 @@
loop_control: loop_control:
label: "{{ item | to_json }}" label: "{{ item | to_json }}"
# community.vmware: full-featured guest management - name: Deploy VM in vCenter
- name: Create VM in vCenter
delegate_to: localhost delegate_to: localhost
module_defaults:
community.vmware.vmware_guest:
hostname: "{{ hypervisor_cfg.url }}"
username: "{{ hypervisor_cfg.username }}"
password: "{{ hypervisor_cfg.password }}"
validate_certs: "{{ hypervisor_cfg.certs | bool }}"
datacenter: "{{ hypervisor_cfg.datacenter }}"
community.vmware.vmware_guest_tpm:
hostname: "{{ hypervisor_cfg.url }}"
username: "{{ hypervisor_cfg.username }}"
password: "{{ hypervisor_cfg.password }}"
validate_certs: "{{ hypervisor_cfg.certs | bool }}"
datacenter: "{{ hypervisor_cfg.datacenter }}"
vmware.vmware.vm_powerstate:
hostname: "{{ hypervisor_cfg.url }}"
username: "{{ hypervisor_cfg.username }}"
password: "{{ hypervisor_cfg.password }}"
validate_certs: "{{ hypervisor_cfg.certs | bool }}"
datacenter: "{{ hypervisor_cfg.datacenter }}"
block:
# community.vmware: full-featured guest management
- name: Create VM in vCenter
vars: vars:
virtualization_vmware_networks: >- virtualization_vmware_networks: >-
{%- set ns = namespace(out=[]) -%} {%- set ns = namespace(out=[]) -%}
@@ -26,11 +47,6 @@
{%- endfor -%} {%- endfor -%}
{{ ns.out }} {{ ns.out }}
community.vmware.vmware_guest: community.vmware.vmware_guest:
hostname: "{{ hypervisor_cfg.url }}"
username: "{{ hypervisor_cfg.username }}"
password: "{{ hypervisor_cfg.password }}"
validate_certs: "{{ hypervisor_cfg.certs | bool }}"
datacenter: "{{ hypervisor_cfg.datacenter }}"
cluster: "{{ hypervisor_cfg.cluster }}" cluster: "{{ hypervisor_cfg.cluster }}"
folder: "{{ system_cfg.path if system_cfg.path | string | length > 0 else omit }}" folder: "{{ system_cfg.path if system_cfg.path | string | length > 0 else omit }}"
name: "{{ hostname }}" name: "{{ hostname }}"
@@ -69,7 +85,7 @@
no_log: true no_log: true
register: virtualization_vmware_create_result register: virtualization_vmware_create_result
- name: Set VM created fact when VM was powered on during creation - name: Set VM created fact when VM was powered on during creation
ansible.builtin.set_fact: ansible.builtin.set_fact:
virtualization_vm_created_in_run: true virtualization_vm_created_in_run: true
when: when:
@@ -77,36 +93,24 @@
- not virtualization_tpm2_enabled | bool - not virtualization_tpm2_enabled | bool
- virtualization_vmware_create_result.changed | bool - virtualization_vmware_create_result.changed | bool
- name: Ensure vTPM2 is enabled when required - name: Ensure vTPM2 is enabled when required
when: virtualization_tpm2_enabled | bool when: virtualization_tpm2_enabled | bool
delegate_to: localhost
community.vmware.vmware_guest_tpm: community.vmware.vmware_guest_tpm:
hostname: "{{ hypervisor_cfg.url }}"
username: "{{ hypervisor_cfg.username }}"
password: "{{ hypervisor_cfg.password }}"
validate_certs: "{{ hypervisor_cfg.certs | bool }}"
datacenter: "{{ hypervisor_cfg.datacenter }}"
folder: "{{ system_cfg.path if system_cfg.path | string | length > 0 else omit }}" folder: "{{ system_cfg.path if system_cfg.path | string | length > 0 else omit }}"
name: "{{ hostname }}" name: "{{ hostname }}"
state: present state: present
no_log: true no_log: true
# vmware.vmware: modern collection for power operations # vmware.vmware: modern collection for power operations
- name: Start VM in vCenter - name: Start VM in vCenter
when: virtualization_tpm2_enabled | bool when: virtualization_tpm2_enabled | bool
delegate_to: localhost
vmware.vmware.vm_powerstate: vmware.vmware.vm_powerstate:
hostname: "{{ hypervisor_cfg.url }}"
username: "{{ hypervisor_cfg.username }}"
password: "{{ hypervisor_cfg.password }}"
validate_certs: "{{ hypervisor_cfg.certs | bool }}"
datacenter: "{{ hypervisor_cfg.datacenter }}"
name: "{{ hostname }}" name: "{{ hostname }}"
state: powered-on state: powered-on
no_log: true no_log: true
register: virtualization_vmware_start_result register: virtualization_vmware_start_result
- name: Set VM created fact when VM was started separately (TPM2 case) - name: Set VM created fact when VM was started separately (TPM2 case)
ansible.builtin.set_fact: ansible.builtin.set_fact:
virtualization_vm_created_in_run: true virtualization_vm_created_in_run: true
when: when: