使用Terraform将本地文件复制到Windows Azure VM

时间:2018-07-20 15:36:29

标签: azure terraform winrm

在使用Terraform创建本地文件后,我尝试使用预配器“文件”将本地文件复制到Windows Azure VM。

我已使用以下方式启用了自定义脚本扩展名:

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"

我在NSG中打开了端口5985:

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  = "*"
  }

我的操作系统配置设置为:

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

最后,我尝试复制文件:

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"
    }
  }
}

每次我尝试“应用”时,都会达到20分钟超时,并失败并显示以下错误(删除了公共IP):

  

azurerm_virtual_machine_extension.VM:   compute.VirtualMachineExtensionsClient#CreateOrUpdate:发送失败   请求:StatusCode = 200-原始错误:Code =“” Message =“”   * null_resource.buildagent:超时-上一个错误:未知错误发布http://PublicIP:5985/wsman:拨打tcp PublicIP:5985:connectex:A   连接尝试失败,因为连接方未正确   一段时间后响应,或建立的连接失败   因为连接的主机无法响应。

首先,我是否正确执行此操作?一切似乎都已正确设置,但最后一步一直失败。

1 个答案:

答案 0 :(得分:0)

默认情况下,标准映像上未启用WinRM。如果您事先将脚本上传到可访问的URL(例如Azure存储Blob),则CustomScriptExtension效果很好。

我使用TFS CI管道(参见图片)来执行此操作,每次提交时都会触发该管道:文件被收集并上传到Azure Blob。

CI steps

Terraform azurerm_virtual_machine_extension具有相似的代码

  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

或DSC扩展名

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