【问题标题】:Terraform - custom_data not running when creating virtual machineTerraform - 创建虚拟机时未运行 custom_data
【发布时间】:2018-07-25 14:43:06
【问题描述】:

我正在尝试在使用 Terraform 创建 Windows 虚拟机期间配置 WinRM。

我的 os_profile 和 os_profile_windows_config 是:

os_profile {
    computer_name  = "server-name"
    admin_username = "${var.vm_username}"
    admin_password = "${var.vm_password}"
    custom_data = <<EOF
    <powershell>
    winrm quickconfig -q
    winrm set winrm/config/winrs '@{MaxMemoryPerShellMB="300"}'
    winrm set winrm/config '@{MaxTimeoutms="1800000"}'
    winrm set winrm/config/service '@{AllowUnencrypted="true"}'
    winrm set winrm/config/service/auth '@{Basic="true"}'

    netsh advfirewall firewall add rule name="WinRM 5985" protocol=TCP dir=in localport=5985 action=allow
    netsh advfirewall firewall add rule name="WinRM 5986" protocol=TCP dir=in localport=5986 action=allow

    net stop winrm
    sc.exe config winrm start=auto
    net start winrm
    </powershell>
    EOF
  }  



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

当我 rdp 到 VM 时,我可以在 C:\AzureData 中看到一个 CustomData.bin 文件,但它没有执行任何 Powershell。我是否认为应该执行此操作是正确的,还是我需要采取其他步骤来执行此操作?

【问题讨论】:

    标签: azure terraform azure-virtual-machine winrm


    【解决方案1】:

    不,不应该。它只是一个base64(据我记得它的base64编码,可能是错误的)编码字符串转储到文件中。您需要使用其他方式解码\执行它

    【讨论】:

    • 知道使用什么或如何执行吗?我在尝试将脚本放到 VM 上然后执行它时遇到了重大问题。
    • 使用 powershell dsc 或脚本扩展。脚本扩展相当简单
    • 我尝试了自定义脚本扩展,但它要求我的脚本位于公共容器中或在 git 上公开可用。由于脚本的性质,我不能这样做。无法像使用 ARM 模板那样使用自定义脚本扩展并传入帐户凭据。
    • 您可以在脚本中使用 sas 令牌从 azure 存储stackoverflow.com/questions/43752262/… 安全下载它
    • 刚刚对此进行了调查,但看不到我可以将 sas 令牌传递到 azurerm_virtual_machine_extension 资源的位置
    【解决方案2】:

    要在 Windows 机器上执行脚本,您需要使用 azurerm_virtual_machine_extension 资源执行自定义脚本扩展。这里可以参考CustomData.bin文件并执行。

    请查看link,它详细解释了如何引导 Linux 和 Windows 虚拟机

    【讨论】:

    • 我对该链接非常感兴趣,因为我正在研究如何引导 Linux VM。链接已失效。不要链接。在答案中解释。
    猜你喜欢
    • 2022-08-18
    • 2018-09-24
    • 1970-01-01
    • 2021-05-15
    • 2018-09-03
    • 1970-01-01
    • 2019-05-31
    • 2013-12-26
    • 2021-02-06
    相关资源
    最近更新 更多