0
votes

I'm trying to use Terraform to create a Windows 10 VM and use the "provisioner" feature to push to and execute configuration scripts on the new VM. This seems to be working fine as I've got winrm properly configured and the provisioner is connecting as the newly created local admin account. This is a pretty extensive configuration and takes quite a while to run (about 30 minutes for the entire process from start to finish). The last step in the script is to run Sysprep to generalize the VM so that it can be used to create an Azure managed Image (from which we'll create VMs for the actual required work). It seems that all the configuration steps are successfully executing, but Terraform is exiting with an HTTP 401 error:

Error: Error applying plan: 1 error(s) occurred: * azurerm_virtual_machine.newimage: error executing "C:/Temp/terraform_467018566.cmd": http response error: 401 - invalid content type

From what I can tell, it did all work, so I'm not sure what's triggering the exit status. Could it be that because the VM is shutting down after running Sysprep, Terraform (or maybe winrm) is thinking something went wrong? This is the last command in the script which remote-exec is running:

%WINDIR%\system32\sysprep\sysprep.exe /generalize /shutdown /oobe
2

2 Answers

0
votes

I'm agreed with your opinion. I think that the Terraform remote-exec is running and needs the response from the VM. When the script command shut down the VM, then there is no response from the VM. So the Terraform remote-exec exit with the 401 error.

0
votes

I've not been able to find any documented confirmation that this is what's happening, but it's my best guess at this point. The workaround I'm going to try is to make the last step of the script being run by remote-exec to instead schedule a task to run after, say, 15-30 seconds to perform the Sysprep/shutdown step which hopefully allows Terraform to see the provisioner process as having completed successfully.