This commit is contained in:
2026-04-29 09:37:32 -04:00
parent 1717081550
commit fe11468547
4 changed files with 66 additions and 20 deletions

View File

@@ -24,18 +24,58 @@
gather_facts: false
vars:
# Use defaults from group_vars if not specified
vm_cpu_count: "{{ vm_cpu_count | default(default_vm_cpu_count) }}"
vm_memory_gb: "{{ vm_memory_gb | default(default_vm_memory_gb) }}"
vm_disk_size_gb: "{{ vm_disk_size_gb | default(default_vm_disk_size_gb) }}"
vm_switch: "{{ vm_switch | default(default_vm_switch) }}"
# Derived paths
vm_path: "{{ vm_storage_path }}\\{{ vm_name }}"
vm_vhd_path: "{{ vm_storage_path }}\\{{ vm_name }}\\{{ vm_name }}.vhdx"
autounattend_path: "{{ vm_storage_path }}\\{{ vm_name }}\\autounattend.xml"
pre_tasks:
- name: Set VM configuration with defaults
ansible.builtin.set_fact:
vm_cpu_count: "{{ vm_cpu_count | default(default_vm_cpu_count) }}"
vm_memory_gb: "{{ vm_memory_gb | default(default_vm_memory_gb) }}"
vm_disk_size_gb: "{{ vm_disk_size_gb | default(default_vm_disk_size_gb) }}"
vm_switch: "{{ vm_switch | default(default_vm_switch) }}"
- name: Get available Hyper-V virtual switches
ansible.windows.win_shell: |
Get-VMSwitch | Select-Object Name, SwitchType | ConvertTo-Json
register: available_switches
changed_when: false
tags: [create, verify]
- name: Parse available switches
ansible.builtin.set_fact:
switch_list: "{{ available_switches.stdout | from_json }}"
when: available_switches.stdout | trim | length > 0
tags: [create, verify]
- name: Display available switches
ansible.builtin.debug:
msg:
- "Available Hyper-V switches:"
- "{{ switch_list | default([]) | map(attribute='Name') | list }}"
- ""
- "Configured switch: {{ vm_switch }}"
tags: [create, verify]
- name: Validate virtual switch exists
ansible.builtin.assert:
that:
- switch_list is defined
- switch_list | selectattr('Name', 'equalto', vm_switch) | list | length > 0
fail_msg: |
Virtual switch '{{ vm_switch }}' not found on Hyper-V host.
Available switches: {{ switch_list | default([]) | map(attribute='Name') | list | join(', ') }}
To fix this:
1. Update the vm_switch variable: -e vm_switch="<switch_name>"
2. Or update default in group_vars/hyperv/vars.yml
3. Or create the switch on Hyper-V host:
New-VMSwitch -Name "{{ vm_switch }}" -SwitchType Internal
success_msg: "Virtual switch '{{ vm_switch }}' is available"
tags: [create]
- name: Validate required variables
ansible.builtin.assert:
that: