# Ansible deploy VMware a Windows VM Template

**URL:** <https://forum.ansible.com/t/ansible-deploy-vmware-a-windows-vm-template/27352>\
**Category:** Ansible Project\
**Tags:** windows, vmware\
**Created:** [April 23, 2018, 4:05pm UTC](https://forum.ansible.com/t/ansible-deploy-vmware-a-windows-vm-template/27352 "2018-04-23T16:05:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xavier\_Sanchez](https://avatars.discourse-cdn.com/v4/letter/x/49beb7/32.png) [@Xavier\_Sanchez](https://forum.ansible.com/u/Xavier_Sanchez)\
**Post date:** [April 23, 2018, 4:05pm UTC](https://forum.ansible.com/t/ansible-deploy-vmware-a-windows-vm-template/27352/1 "2018-04-23T16:05:06Z")

</div>

Hi experts,

One question please,

Actually I working and testing Ansible for deploy a specified VM in a VMware infrastructure (Windows Server 2016) and I heve a problem when this VM is deployed and started,  
I not have idea automate “press Next” and specify the Administrator password. I attach the images, (the Accept the language keyboard, License rights, and administrator password),

Any idea for help me please?. Thank you!!!

/Xavi

[![](https://lh3.googleusercontent.com/-0R827TZhbdc/Wt4DZAjjZfI/AAAAAAAACXQ/2ZFEOkoI2VADxZj2dVK3eTpxcjwUepfBgCLcBGAs/s320/Capture02.JPG)](https://lh3.googleusercontent.com/-0R827TZhbdc/Wt4DZAjjZfI/AAAAAAAACXQ/2ZFEOkoI2VADxZj2dVK3eTpxcjwUepfBgCLcBGAs/s1600/Capture02.JPG)

[![](https://lh3.googleusercontent.com/-z7y6ivF7zec/Wt4DzZgJSxI/AAAAAAAACXY/CgEAlu8ZyZIez8g1tW4x1NKBATHULKyJgCLcBGAs/s320/Capture04.JPG)](https://lh3.googleusercontent.com/-z7y6ivF7zec/Wt4DzZgJSxI/AAAAAAAACXY/CgEAlu8ZyZIez8g1tW4x1NKBATHULKyJgCLcBGAs/s1600/Capture04.JPG)

---

<div class="post-metadata">

**Author:** ![Sebastien\_Desbois](https://avatars.discourse-cdn.com/v4/letter/s/3ab097/32.png) [@Sebastien\_Desbois](https://forum.ansible.com/u/Sebastien_Desbois)\
**Post date:** [April 27, 2018, 4:49pm UTC](https://forum.ansible.com/t/ansible-deploy-vmware-a-windows-vm-template/27352/2 "2018-04-27T16:49:41Z")

</div>

Hello,

You have two options (from my point of view) to do that

- Create a Windows 2016 template with all infos already in it and deploy your VM with this template. Warning; deployment with ansible module vsphere\_guest and vmware\_guest will result in a “clone” meaning all your VMs deployed with this template will have the same SSID
- You create a generic Windows 2016 template (with VMTools installed), and after deployment through ansible modules, you use ansible to execute a powercli script to do a sysprep through the VMTools.

Example of the powercli script (this assume that you have Linux Powercli installed on your ansible server and that you have generated a unattended.xml file with the adequate Windows tool)  
The unattended.xml file will have all the info to skip the screens that aare bloking you.

> #Connect to the vCenter  
> connect-VIServer $vCenter -user $vCenterUser -password $vCenterPassword
> 
> #Building the credential object - Credentials of the Guest OS.  
> $SafeModePassword = ConvertTo-SecureString $VMPassword -AsPlainText -Force  
> $GuestCredentials = New-Object System.Management.Automation.PSCredential ($VMUser, $SafeModePassword)
> 
> #Delete the unattended file if it exists  
> #Need to be sure even if we delete it after sysprep  
> Invoke-VMScript -ScriptText “del C:\windows\temp\unattended.xml” -VM $VM -GuestCredential $GuestCredentials | out-null
> 
> #Creates setupcomplete.cmd file to delete sysprep XML file post-sysprep. File must not already exist.  
> #if the folder exist already, not a problem  
> $completescript = “mkdir c:\windows\setup\scripts; echo `"del c:\windows\temp\unattended.xml`” | out-file -force -encoding ASCII c:\windows\setup\scripts\setupcomplete.cmd"  
> Invoke-VMScript -ScriptText $completescript -VM $VM -GuestCredential $GuestCredentials
> 
> #Modify unattend file to change the default password in it  
> #Needed because in Windows 2016, the sysprep reinitiliaze the admin password…  
> $xml = [xml](Get-Content $xmlpath)  
> $node = $xml.unattend.settings.component.useraccounts.AdministratorPassword  
> $node.value = $VMPassword  
> $node.PlainText = “true”  
> $xml.Save($xmlpath)
> 
> #Copy the unattended file on the Windows server through the VMTools  
> copy-vmguestfile -source $xmlpath -destination c:\windows\temp -VM $VM -localtoguest -GuestCredential $GuestCredentials
> 
> #Builing Script  
> $Script = “C:\Windows\System32\Sysprep\Sysprep.exe /generalize /oobe /reboot /quiet /unattend:C:\windows\temp\unattended.xml”
> 
> #Running script on the VM through VMTools  
> Invoke-VMScript -ScriptText $Script -VM $VM -GuestCredential $GuestCredentials -runasync
> 
> Start-Sleep -s 120
> 
> #disconnect from vCenter  
> disconnect-VIServer -confirm:$false

Hope this can help you.

Best Regards,

Sebastien
