与通过 Terraform 部署 Azure 虚拟桌面主机池相关的问题

问题描述 投票:0回答:1

通过 Terraform 部署 Azure 虚拟桌面主机池

我正在编写 terraform 脚本来部署 Azure 虚拟桌面主机池。创建主机池时,我需要指定会话主机相关配置,例如虚拟机数量、虚拟机映像、本地管理员密码、加入 EntraID ...等。

在 terraform 提供程序文档中,它只这样说。也没有提供其他信息。

vm_template -(可选)用于主机池中会话主机配置的 VM 模板。这是一个 JSON 字符串。

没有官方文档。因此,我所做的是获取 Azure 虚拟桌面的 ARM 模板,并根据 ARM 模板中定义的参数以 JSON 形式传递所需的值。

resource "azurerm_virtual_desktop_host_pool" "example" {
  location            = var.rglocation
  resource_group_name = var.rgname
  name                = var.hostpoolname
  validate_environment = false
  preferred_app_group_type = "Desktop"
  type                = "Pooled"
  load_balancer_type  = var.loadbalncealgorithm

  vm_template = <<EOF
  {
    "vmNamePrefix": "${var.vmNamePrefix}",
    "vmLocation": "${var.rglocation}",
    "aadjoin": "true",
    "intune": "false",
    "vmImageType": "Gallery",
    "vmSize": "Standard_B2s",
    "vmNumberOfInstances": "${var.vmcount}",
    "vmDiskType": "StandardSSD_LRS",
    "vmDiskSizeGB": "128",
    "securityType": "TrustedLaunch",
    "existingVnetName": "${var.vnetname}",
    "existingSubnetName": "${var.privsubname}",
    "vmGalleryImageOffer": "windows-10",
    "vmGalleryImagePublisher": "microsoftwindowsdesktop",
    "vmGalleryImageSKU": "win10-21h2-avd-g2",
    "vmAdministratorAccountUsername": "${var.localadminusername}",
    "vmAdministratorAccountPassword": "PasswordForLocalAdmin"
  }
  EOF

  maximum_sessions_allowed = var.sessionllimit
}

但是,当 terraform apply run 时,它显示为成功,但未按预期创建虚拟机。仅创建了主机池。

任何人都知道如何解决这个问题。

azure terraform
1个回答
0
投票

与通过 Terraform 部署 Azure 虚拟桌面主机池相关的问题

由于没有其他方式指定指定信息的信息,因此创建了 Azure 虚拟桌面主机池,而不是指定会话主机相关配置(例如 VM 数量、VM 映像、本地管理员密码、加入 EntraID)的问题除了画廊图像或自定义图像之外。

为了定义

vm_templete
这些,我们需要定义
jsoncode
和具有上述格式版本的虚拟机映像。

演示地形代码:

variable "resource_group_name" {
  type        = string
  description = "The name of the resource group."
}

variable "location" {
  type        = string
  description = "The Azure location where the resources will be deployed."
}

variable "host_pool_name" {
  type        = string
  description = "The name of the Virtual Desktop Host Pool."
}

variable "admin_username" {
  type        = string
  description = "The admin username for the session hosts."
}

variable "admin_password" {
  type        = string
  description = "The admin password for the session hosts."
  sensitive   = true
}

variable "domain_join_username" {
  type        = string
  description = "The username for domain joining."
}

variable "domain_join_password" {
  type        = string
  description = "The password for domain joining."
  sensitive   = true
}

variable "domain_name" {
  type        = string
  description = "The domain name for the session hosts."
}

variable "virtual_desktop_host_pool_load_balancer_type" {
  type        = string
  description = "The load balancer type for the host pool."
}

variable "virtual_desktop_host_pool_location" {
  type        = string
  description = "The location of the virtual desktop host pool."
}

variable "virtual_desktop_host_pool_name" {
  type        = string
  description = "The name of the virtual desktop host pool."
}

variable "virtual_desktop_host_pool_resource_group_name" {
  type        = string
  description = "The resource group name of the virtual desktop host pool."
}

variable "virtual_desktop_host_pool_type" {
  type        = string
  description = "The type of the virtual desktop host pool."
}

variable "virtual_desktop_host_pool_custom_rdp_properties" {
  type        = string
  description = "Custom RDP properties for the virtual desktop host pool."
}

variable "virtual_desktop_host_pool_description" {
  type        = string
  description = "Description of the virtual desktop host pool."
}

variable "virtual_desktop_host_pool_friendly_name" {
  type        = string
  description = "Friendly name of the virtual desktop host pool."
}

variable "virtual_desktop_host_pool_maximum_sessions_allowed" {
  type        = number
  description = "Maximum sessions allowed for the virtual desktop host pool."
}

variable "virtual_desktop_host_pool_personal_desktop_assignment_type" {
  type        = string
  description = "Personal desktop assignment type for the virtual desktop host pool."
}

variable "virtual_desktop_host_pool_preferred_app_group_type" {
  type        = string
  description = "Preferred app group type for the virtual desktop host pool."
}

variable "virtual_desktop_host_pool_start_vm_on_connect" {
  type        = bool
  description = "Whether to start VM on connect for the virtual desktop host pool."
}

variable "virtual_desktop_host_pool_tags" {
  type        = map(string)
  description = "Tags for the virtual desktop host pool."
}

variable "virtual_desktop_host_pool_validate_environment" {
  type        = bool
  description = "Whether to validate the environment for the virtual desktop host pool."
}

variable "virtual_desktop_host_pool_vm_template" {
  type = object({
    namePrefix       = string
    count            = number
    vmSize           = string
    useManagedDisks  = bool
    adminUsername    = string
    adminPassword    = string
    osDisk           = object({
      createOption       = string
      caching            = string
      managedDisk        = object({
        storageAccountType = string
      })
    })
    sourceImage      = object({
      publisher      = string
      offer          = string
      sku            = string
      version        = string
    })
    domainJoin       = object({
      domainName     = string
      ouPath         = string
      accountType    = string
      username       = string
      password       = string
    })
    networkProfile   = object({
      networkInterfaces = list(object({
        id = string
      }))
    })
  })
  description = "A VM template for session hosts configuration within hostpool."
}

main.tf:

provider "azurerm" {
  features {}
}

data "azurerm_subscription" "current" {}

resource "azurerm_resource_group" "example" {
  name     = var.resource_group_name
  location = var.location
}

resource "azurerm_virtual_desktop_host_pool" "this" {
  load_balancer_type               = var.virtual_desktop_host_pool_load_balancer_type
  location                         = var.virtual_desktop_host_pool_location
  name                             = var.virtual_desktop_host_pool_name
  resource_group_name              = azurerm_resource_group.example.name
  type                             = var.virtual_desktop_host_pool_type
  custom_rdp_properties            = var.virtual_desktop_host_pool_custom_rdp_properties
  description                      = var.virtual_desktop_host_pool_description
  friendly_name                    = var.virtual_desktop_host_pool_friendly_name
  maximum_sessions_allowed         = var.virtual_desktop_host_pool_maximum_sessions_allowed
  personal_desktop_assignment_type = var.virtual_desktop_host_pool_personal_desktop_assignment_type
  preferred_app_group_type         = var.virtual_desktop_host_pool_preferred_app_group_type
  start_vm_on_connect              = var.virtual_desktop_host_pool_start_vm_on_connect
  tags                             = var.virtual_desktop_host_pool_tags
  validate_environment             = var.virtual_desktop_host_pool_validate_environment
  vm_template                      = local.vm_template

  depends_on = [azurerm_resource_group.example]
}

resource "azurerm_virtual_network" "example" {
  name                = "${var.host_pool_name}-vnet"
  address_space       = ["10.0.0.0/16"]
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name

  depends_on = [azurerm_resource_group.example]
}

resource "azurerm_subnet" "example" {
  name                 = "${var.host_pool_name}-subnet"
  resource_group_name  = azurerm_resource_group.example.name
  virtual_network_name = azurerm_virtual_network.example.name
  address_prefixes     = ["10.0.1.0/24"]

  depends_on = [azurerm_virtual_network.example]
}

resource "azurerm_network_interface" "example" {
  count               = var.virtual_desktop_host_pool_vm_template.count
  name                = "${var.host_pool_name}-nic-${count.index}"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name

  ip_configuration {
    name                          = "internal"
    subnet_id                     = azurerm_subnet.example.id
    private_ip_address_allocation = "Dynamic"
  }

  depends_on = [azurerm_subnet.example]
}

locals {
  vm_template = jsonencode({
    namePrefix       = var.virtual_desktop_host_pool_vm_template.namePrefix
    count            = var.virtual_desktop_host_pool_vm_template.count
    vmSize           = var.virtual_desktop_host_pool_vm_template.vmSize
    useManagedDisks  = var.virtual_desktop_host_pool_vm_template.useManagedDisks
    adminUsername    = var.virtual_desktop_host_pool_vm_template.adminUsername
    adminPassword    = var.virtual_desktop_host_pool_vm_template.adminPassword
    osDisk           = {
      createOption       = var.virtual_desktop_host_pool_vm_template.osDisk.createOption
      caching            = var.virtual_desktop_host_pool_vm_template.osDisk.caching
      managedDisk        = {
        storageAccountType = var.virtual_desktop_host_pool_vm_template.osDisk.managedDisk.storageAccountType
      }
    }
    sourceImage      = {
      publisher      = var.virtual_desktop_host_pool_vm_template.sourceImage.publisher
      offer          = var.virtual_desktop_host_pool_vm_template.sourceImage.offer
      sku            = var.virtual_desktop_host_pool_vm_template.sourceImage.sku
      version        = var.virtual_desktop_host_pool_vm_template.sourceImage.version
    }
    domainJoin       = {
      domainName     = var.virtual_desktop_host_pool_vm_template.domainJoin.domainName
      ouPath         = var.virtual_desktop_host_pool_vm_template.domainJoin.ouPath
      accountType    = var.virtual_desktop_host_pool_vm_template.domainJoin.accountType
      username       = var.virtual_desktop_host_pool_vm_template.domainJoin.username
      password       = var.virtual_desktop_host_pool_vm_template.domainJoin.password
    }
    networkProfile   = {
      networkInterfaces = [
        {
          id = "/subscriptions/${data.azurerm_subscription.current.subscription_id}/resourceGroups/${var.virtual_desktop_host_pool_resource_group_name}/providers/Microsoft.Network/networkInterfaces/${var.host_pool_name}-nic-0"
        },
        {
          id = "/subscriptions/${data.azurerm_subscription.current.subscription_id}/resourceGroups/${var.virtual_desktop_host_pool_resource_group_name}/providers/Microsoft.Network/networkInterfaces/${var.host_pool_name}-nic-1"
        }
      ]
    }
  })
}

部署:

enter image description here

参考:

terraform-azurerm-avm-res-desktopvirtualization-hostpool/main.tf 位于 main · Azure/terraform-azurerm-avm-res-desktopvirtualization-hostpool (github.com)

© www.soinside.com 2019 - 2024. All rights reserved.