【问题标题】:Copy a local file onto Windows Azure VM using Terraform使用 Terraform 将本地文件复制到 Windows Azure VM
【发布时间】:2018-07-20 15:36:28
【问题描述】:

在使用 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 连接尝试失败,因为连接方没有正确 一段时间后响应,或建立连接失败 因为连接的主机没有响应。

首先,我这样做是否正确?一切似乎都设置正确,但最后一步总是失败。

【问题讨论】:

    标签: azure terraform winrm


    【解决方案1】:

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

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

    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
    

    【讨论】:

    • 我认为他在他的配置中解释了这一点
    • 是的 - 我的印象是操作系统配置启用了 WinRM。我可以看到它在 VM 扩展中启用。此外,我宁愿避免将脚本上传到我预先存储的存储中,因为该脚本有可能发生更改,因此希望从源代码管理中获取它。
    • 我通过 CI 管道解决了这个问题:它收集脚本并将它们发布到 Blob 存储。 CI 由任何提交触发,它针对每个分支容器/文件夹。
    • @GiulioVian 啊,好吧,我现在就试试这个解决方法。然后如何将其复制到您的虚拟机?你能分享你的步骤/配置吗?
    • 谢谢。我有一个脚本,可以将文件上传到存储,但我在运行 Terraform 以从存储中获取脚本并执行它们。你有这个要分享吗?
    猜你喜欢
    • 1970-01-01
    • 2020-01-24
    • 2022-01-22
    • 2017-12-04
    • 2021-09-10
    • 2018-07-12
    • 1970-01-01
    • 2020-08-18
    • 2021-06-14
    相关资源
    最近更新 更多