43 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
			
		
		
	
	
			43 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
| ### Deploy-VM
 | |
| **Purpose**: Deploy an Ubuntu Server 20.04 Virtual Machine to Hyper-V from an ISO image located at a pre-defined location.
 | |
| ```jsx title="Playbooks/Windows/Hyper-V/Deploy-VM.yml"
 | |
| ---
 | |
| - name: Deploy Hyper-V Guest VM
 | |
|   hosts: all
 | |
|   gather_facts: no
 | |
| 
 | |
|   tasks:
 | |
|     - name: Set the randomized Hyper-V Guest VM Name Variable
 | |
|       set_fact:
 | |
|         vm_name_fact: "{{ vm_name }}"
 | |
| 
 | |
|     - name: Create VM folder
 | |
|       ansible.windows.win_file:
 | |
|         path: "{{ vm_folder }}"
 | |
|         state: directory
 | |
| 
 | |
|     - name: Create Guest VM
 | |
|       ansible.windows.win_shell: |
 | |
|         New-VM -Name "{{ vm_name_fact }}" -MemoryStartupBytes "{{ vm_memory }}" -NewVHDPath "{{ vm_folder }}\\{{ vm_name_fact }}.vhdx" -NewVHDSizeBytes "{{ vm_storage }}" -Path "{{ vm_folder }}" -Generation 2
 | |
|       register: vm_creation_result
 | |
| 
 | |
|     - name: Attach ISO to VM
 | |
|       ansible.windows.win_shell: |
 | |
|         Add-VMDvdDrive -VMName "{{ vm_name_fact }}" -Path "{{ iso_path }}"
 | |
|       when: vm_creation_result is changed
 | |
| 
 | |
|     - name: Configure VM to boot from DVD
 | |
|       ansible.windows.win_shell: |
 | |
|         Set-VMFirmware -VMName "{{ vm_name_fact }}" -FirstBootDevice (Get-VMDvdDrive -VMName "{{ vm_name }}")
 | |
|       when: vm_creation_result is changed
 | |
| 
 | |
|     - name: Disable VM Secure Boot
 | |
|       ansible.windows.win_shell: |
 | |
|         Set-VMFirmware -VMName "{{ vm_name_fact }}" -EnableSecureBoot Off
 | |
|       when: vm_creation_result is changed
 | |
| 
 | |
|     - name: Start Guest VM
 | |
|       ansible.windows.win_shell: |
 | |
|         Start-VM -Name "{{ vm_name_fact }}"
 | |
|       when: vm_creation_result is changed
 | |
| ``` |