0
votes

I'm trying to use provisioner "file" to copy a local file onto a Windows Azure VM after creating it using Terraform.

I have enabled the custom script extension using:

resource "azurerm_virtual_machine_extension" "VM" {
  name                 = "WinRM"
  location             = "${azurerm_resource_group.VM.location}"
  resource_group_name  = "${azurerm_resource_group.VM.name}"
  virtual_machine_name = "${azurerm_virtual_machine.VM01.name}"
  publisher            = "Microsoft.Compute"
  type                 = "CustomScriptExtension"
  type_handler_version = "1.8"

I have port 5985 open in my NSG:

security_rule {
  name            = "AllowWinRM"
  priority          = 300
  direction         = "Inbound"
  access            = "Allow"
  protocol          = "Tcp"
  source_port_range           = "*"
  destination_port_range      = "5985"
  source_address_prefix       = "*"
  destination_address_prefix  = "*"
  }

My OS config is set to:

os_profile_windows_config {
    provision_vm_agent = true
    winrm {
       protocol="http"
     }
  }

Finally I try to copy the file:

resource "null_resource" "VM" {
  provisioner "file" {
    source      = "output.txt"
    destination = "c:\\temp\\output.txt"

    connection {
      type     = "winrm"
      user     = "${var.adminusername}"
      password = "${var.adminpassword}"
      host     = "${azurerm_public_ip.VM1_pip.ip_address}"
      port     = "5985"
      timeout  = "20m"
    }
  }
}

Every time I try to 'apply' this it hits the 20 minute timeout and fails with the following error (public ip removed):

azurerm_virtual_machine_extension.VM: compute.VirtualMachineExtensionsClient#CreateOrUpdate: Failure sending request: StatusCode=200 -- Original Error: Code="" Message="" * null_resource.buildagent: timeout - last error: unknown error Post http://PublicIP:5985/wsman: dial tcp PublicIP:5985: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

First of all, am I doing this correctly? Everything seems to be set up correctly but that last step keeps failing.

1

1 Answers

0
votes

WinRM is not enabled by default on the standard images. CustomScriptExtension works well if you upload in advance the script to a publicly accessible URL, like an Azure Storage Blob.

I do this using my TFS CI pipeline (see picture) which triggers at each commit: the files are collected and uploaded to Azure Blob.

CI steps

The Terraform azurerm_virtual_machine_extension has similar code

  settings = <<SETTINGS
    {
      "fileUris": [
        "${var.vm_customscript_baseurl}/UpgradePowershell.vbs",
        "${var.vm_customscript_baseurl}/Win8.1AndW2K12R2-KB3191564-x64.msu"
        ],
      "commandToExecute": "cmd /c cscript UpgradePowershell.vbs"
    }
SETTINGS

or, in case of the DSC extension

  settings = <<SETTINGS
    {
      "configuration": {
        "url": "${var.vm_dsc_package_url}",
        "script": "VotingWebConfiguration.ps1",
        "function": "VotingWebRoleConfiguration"
      }
    }
SETTINGS