【问题标题】:Terraform Enable-PSRemoting on Azure Windows VMAzure Windows VM 上的 Terraform Enable-PSRemoting
【发布时间】:2020-03-30 07:49:07
【问题描述】:

我在Azure cloud 上使用Terraform v0.12.9 配置Windows VM。在该 VM 上,我想使用 Terraform 执行以下任务。基本上是为了避免RDP到VM并手动执行srcipts。

1. Enable PSRemoting
2. Create a new FirewallRule
3. Create a SelfSignedCertificate

我有一个 vm_provisioning.tf 如下:

resource "azurerm_virtual_machine" "vm" {
    #count               = "${var.env == "dev" ? 0 : 1}"
    count               = "${var.env == "dev"  ? 0 : 1}"
    name                  = var.vm_name
    location              = "${azurerm_resource_group.rg.location}"
    resource_group_name   = "${azurerm_resource_group.rg.name}"
    network_interface_ids = ["${azurerm_network_interface.network-interface[count.index].id}"]
    vm_size               = "Standard_D13_v2"

    storage_image_reference {
        publisher = "MicrosoftWindowsDesktop"
        offer     = "Windows-10"
        sku       = "rs4-pro"
        version   = "latest"
    }
    storage_os_disk {
        name              = "Primary-disk"
        caching           = "ReadWrite"
        create_option     = "FromImage"
        managed_disk_type = "Standard_LRS"
        disk_size_gb      = "127"
    }
    os_profile {
        computer_name  = var.vm_name
        admin_username = "${var.vm-username}"
        admin_password = "${random_password.vm_password.result}"
    }

    os_profile_windows_config {                   
    }
    provisioner "remote-exec" {
        connection {
            host        = "${element(azurerm_public_ip.PublicIP.*.ip_address, count.index)}"
            type        = "winrm"
            user        = var.vm-username
            password    = "${random_password.vm_password.result}"
            agent       = "false"
            insecure    = "true"
        }
        **inline = [
            "powershell.exe Set-ExecutionPolicy Bypass -force",
            "powershell.exe $DNSName = $env:COMPUTERNAME",
            "powershell.exe Enable-PSRemoting -Force",
            "powershell.exe New-NetFirewallRule -Name 'WinRM HTTPS' -DisplayName 'WinRM HTTPS' -Enabled True -Profile 'Any' -Action 'Allow' -Direction 'Inbound' -LocalPort 5986 -Protocol 'TCP'",
            "powershell.exe $thumbprint = (New-SelfSignedCertificate -DnsName $DNSName -CertStoreLocation Cert:/LocalMachine/My).Thumbprint",
            "powershell.exe $cmd = 'winrm create winrm/config/Listener?Address=*+Transport=HTTPS @{Hostname=''$DNSName''; CertificateThumbprint=''$thumbprint''}'",
            "powershell.exe cmd.exe /C $cmd"
        ]**
    }
}

我也试过azurerm_virtual_machine_extension。

resource "azurerm_virtual_machine_extension" "winrm" {
  name                 = var.name
  location             = "${azurerm_resource_group.rg.location}"
  resource_group_name  = "${azurerm_resource_group.rg.name}"
  virtual_machine_name = var.vm_name
  publisher            = "Microsoft.Azure.Extensions"
  type                 = "CustomScriptExtension"
  type_handler_version = "2.0"

  settings = <<SETTINGS
    {
        "commandToExecute": "hostname && uptime"
    }
    SETTINGS
}

azurerm_virtual_machine_extension 我遇到了错误。

##[error]Terraform command 'apply' failed with exit code '1'.:  compute.VirtualMachineExtensionsClient#CreateOrUpdate: Failure sending request: StatusCode=0 -- Original Error: autorest/azure: Service returned an error. Status=<nil> Code="OperationNotAllowed" Message="This operation cannot be performed when extension operations are disallowed. To allow, please ensure VM Agent is installed on the VM and the osProfile.allowExtensionOperations property is true."

【问题讨论】:

    标签: terraform azure-virtual-machine powershell-remoting terraform-provider-azure


    【解决方案1】:

    根据错误消息,您需要包含一个os_profile_windows_config 块。它支持以下内容:

    provision_vm_agent -(可选)Azure 虚拟机来宾 代理要安装在这个虚拟机上吗?默认为 false。

      os_profile_windows_config {
        provision_vm_agent  = true
    
      }
    

    编辑

    This example 使用公共 IP 地址配置运行 Windows Server 2016 的虚拟机,并通过 WinRM 运行 remote-exec 配置器。

    main.tf

    locals {
      custom_data_params  = "Param($ComputerName = \"${local.virtual_machine_name}\")"
      custom_data_content = "${local.custom_data_params} ${file("./files/winrm.ps1")}"
    }
    

    winrm.ps1

    【讨论】:

    • 我尝试在os_profile_windows_config 中设置provision_vm_agent = true,但这似乎不起作用,出现同样的错误。实际上,我正在实现的是使用 terraform 配置 Windows VM 并远程执行一些 powershell 脚本。除非启用PSSession,否则 Windows 将不允许远程执行 powershell。
    • 您可以参考我编辑中的示例吗?如果您有任何问题,请告诉我。
    猜你喜欢
    • 1970-01-01
    • 2021-06-14
    • 2018-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-21
    相关资源
    最近更新 更多