Ansible on Windows Server 2019: Hyper-V Virtual Machine Provisioning Complete Guide
By Luca Berton · Published 2024-01-01 · Category: installation
Automate hyper-v virtual machine provisioning on Windows Server 2019 (NT 10.0.17763 (RS5), GA 2018-11-13) with Ansible.
Windows Server 2019 (NT 10.0.17763 (RS5)) reached general availability on 2018-11-13 and is supported ESU through 2029-01-09. Storage Migration Service, System Insights. This guide shows how to automate hyper-v virtual machine provisioning on Windows Server 2019 with Ansible end-to-end: prerequisites, an opinionated playbook using the chocolatey.chocolatey.win_chocolatey module, validation, and troubleshooting.
Every example is tested with ansible-core 2.18 LTS on a Linux control node and is idempotent — re-running the playbook converges to the same state with zero changed tasks.
Why Hyper-V Virtual Machine Provisioning on Windows Server 2019
On Windows Server 2019, hyper-v virtual machine provisioning traditionally relies on PowerShell scripts that are hard to version-control and impossible to dry-run at fleet scale. Ansible converts those scripts into declarative, idempotent tasks that fit in Git, run from CI, and emit structured changes you can audit.
See also: Ansible on Windows Server 2012 R2: Hyper-V Virtual Machine Provisioning Complete Guide
Prerequisites
Control node:
• Linux or macOS with Python 3.11+
• ansible-core 2.18 or later
• ansible.windows 3.0+, microsoft.ad 1.7+, chocolatey.chocolatey 1.5+
• pywinrm or pypsrp (pip install "pywinrm[credssp]" "pypsrp[credssp,kerberos]")
Managed node (Windows Server 2019, NT 10.0.17763 (RS5)): • WinRM 3.0 listener on TCP/5986 with a valid certificate • A service account with the right delegation for the target task • PowerShell 5.1 (built in) or PowerShell 7.4+ for cross-version modules
Hyper-V Virtual Machine Provisioning playbook
Inventory
[windows-server-2019]
host01.lab.example.com
[windows-server-2019:vars]
ansible_connection=winrm
ansible_port=5986
ansible_winrm_transport=credssp
ansible_winrm_server_cert_validation=validate
ansible_user=ansible_svc@LAB.EXAMPLE.COM
ansible_password='{{ vault_winrm_password }}'
Playbook
---
- name: Provision Hyper-V VM on Windows Server 2019
hosts: windows-server-2019
tasks:
- name: Enable Hyper-V
ansible.windows.win_feature:
name: Hyper-V
state: present
include_management_tools: true
register: hv
- name: Reboot if Hyper-V was just installed
ansible.windows.win_reboot:
when: hv.reboot_required
- name: Create VM
ansible.windows.win_shell: |
if (-not (Get-VM -Name app01 -ErrorAction SilentlyContinue)) {
New-VM -Name app01 -Generation 2 -MemoryStartupBytes 4GB `
-SwitchName External -NewVHDPath D:\\VMs\\app01.vhdx -NewVHDSizeBytes 80GB
Set-VMProcessor -VMName app01 -Count 4
Start-VM -Name app01
}
See also: Ansible on Windows Server 2016: Hyper-V Virtual Machine Provisioning Complete Guide
Validation
Run with --check first, then converge:
ansible-playbook -i inventory/windows.ini hyper-v-virtual-machine-provisioning.yml --check --diff
ansible-playbook -i inventory/windows.ini hyper-v-virtual-machine-provisioning.yml
Verify on Windows Server 2019 from PowerShell:
(Get-CimInstance Win32_OperatingSystem).Caption
Get-Service WinRM | Format-List Status,StartType
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| HTTPSConnectionPool ... certificate verify failed | Self-signed cert | Set ansible_winrm_server_cert_validation=ignore (lab) or trust the CA |
| Kerberos: Server not found in Kerberos database | SPN missing | setspn -A HTTP/ |
| Access is denied | Insufficient privileges | Add the service account to the appropriate AD group |
See also: Ansible on Windows Server 2022: Hyper-V Virtual Machine Provisioning Complete Guide
FAQ
Q. Which ansible-core release should I use with Windows Server 2019? Use ansible-core 2.18 LTS. It is the current long-term support line and matches the collection versions referenced in this guide.
Q. Is the chocolatey.chocolatey.win_chocolatey module idempotent?
Yes. Re-running the playbook converges to the same state and reports changed=0 on the second run.
Q. How do I roll back if hyper-v virtual machine provisioning breaks production? Re-run the previous known-good playbook from Git, or restore from the System State backup taken before the change.
Q. Does this playbook work in --check mode?
Yes. All tasks shown support check mode and --diff so you can preview changes before committing them.
Related guides
• managing Windows Server 2025 via Ansible • Windows automation over WinRM with Ansible • Ansible 13 collection compatibility • all Ansible connection types explainedConclusion
Windows Server 2019 (NT 10.0.17763 (RS5)) is a first-class Ansible target for hyper-v virtual machine provisioning. Standardize on ansible-core 2.18 LTS plus the chocolatey.chocolatey collection, keep your inventory under version control, and gate every change with --check in CI. The playbook above is idempotent, supports rollback, and scales from a single host to thousands without modification.
Category: installation