azure Terraform失败了:操作系统类型“Windows”不支持每个处理程序多个VMExtensions

fnvucqvd  于 2023-10-22  发布在  Windows
关注(0)|答案(1)|浏览(116)

我正在Azure Cloud中编写一个Windows VM,使用Terraform安装3个不同程序的扩展。然而,我正面临着一个我无法解决的大问题。下面的脚本,错误和我的想法。

**

data "template_file" "testing_file_part1" {
    template = "${file("vm_adds_testing_part0.ps1")}"
} 
data "template_file" "testing_file_part2" {
    template = "${file("vm_adds_testing_part1.ps1")}"
} 
data "template_file" "testing_file_part3" {
    template = "${file("vm_adds_testing_part2.ps1")}"
} 

...... vm.tf omitted. 
# First extension
resource "azurerm_virtual_machine_extension" "extension1" {
  name                = "example-vm-extension1"
  virtual_machine_id  = azurerm_windows_virtual_machine.testing_vm.id
  publisher           = "Microsoft.Compute"
  type                = "CustomScriptExtension"
  type_handler_version = "1.10"

  settings = <<SETTINGS
    {
     "commandToExecute": "powershell -command \"[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String('${base64encode(data.template_file.testing_file_part1.rendered)}')) | Out-File -filepath vm_adds1.ps1\" && powershell -ExecutionPolicy Unrestricted -File vm_adds1.ps1 -username ${var.VIRTUAL_MACHINE_ADMIN_USERNAME} -password ${var.VIRTUAL_MACHINE_ADMIN_PASSWORD}"
    }
SETTINGS
}

# Second extension
resource "azurerm_virtual_machine_extension" "extension2" {
  name                = "example-vm-extension2"
  virtual_machine_id  = azurerm_windows_virtual_machine.testing_vm.id
  publisher           = "Microsoft.Compute"
  type                = "CustomScriptExtension"
  type_handler_version = "1.10"

  settings = <<SETTINGS
    {
     "commandToExecute": "powershell -command \"[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String('${base64encode(data.template_file.testing_file_part2.rendered)}')) | Out-File -filepath vm_adds2.ps1\" && powershell -ExecutionPolicy Unrestricted -File vm_adds2.ps1 -username ${var.VIRTUAL_MACHINE_ADMIN_USERNAME} -password ${var.VIRTUAL_MACHINE_ADMIN_PASSWORD}"
    }
SETTINGS
  depends_on = [azurerm_virtual_machine_extension.extension1]
}

# Third extension
resource "azurerm_virtual_machine_extension" "extension3" {
  name                = "example-vm-extension3"
  virtual_machine_id  = azurerm_windows_virtual_machine.testing_vm.id
  publisher           = "Microsoft.Compute"
  type                = "CustomScriptExtension"
  type_handler_version = "1.10"

  settings = <<SETTINGS
    {
     "commandToExecute": "powershell -command \"[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String('${base64encode(data.template_file.testing_file_part3.rendered)}')) | Out-File -filepath vm_adds3.ps1\" && powershell -ExecutionPolicy Unrestricted -File vm_adds3.ps1 -username ${var.VIRTUAL_MACHINE_ADMIN_USERNAME} -password ${var.VIRTUAL_MACHINE_ADMIN_PASSWORD}"
    }
SETTINGS
  depends_on = [azurerm_virtual_machine_extension.extension2]
}

错误:

Error: compute.VirtualMachineExtensionsClient#CreateOrUpdate: Failure sending request: StatusCode=400 -- Original Error: Code="BadRequest" Message="Multiple VMExtensions per handler not supported for OS type 'Windows'. VMExtension 'custom-script-extension-testing-part2' with handler 'Microsoft.Compute.CustomScriptExtension' already added or specified in input."
 with module.vm_extension_custom_script_performance_testing_part2.azurerm_virtual_machine_extension.vm_extension[0],

问题:

有没有一种方法可以使用Terraform执行多个扩展?如果是,如何进行?
场景:

  • 当我运行1个扩展时,它可以工作。
  • 当我有和扩展与计数它是失败的。
  • ps1脚本太大了,如果我试图把它们放在一个文件中,Terraform失败了,说Base64太大了。

我正在寻找解决这个问题的方法。

vh0rcniy

vh0rcniy1#

我尝试为每个处理程序添加多个VM扩展,这不支持操作系统类型“Windows”,因此我尝试了具有类似要求的单个VM扩展。
我同意@Thomas的评论,即不可能使用多个扩展名,也可以根据链接检查阻止访问
您遇到的错误是由于Azure平台的限制,该平台不允许在单个Windows VM上使用同一类型的多个扩展。鉴于您正在为每个扩展使用CustomScriptExtension,Azure不允许将其中一个以上的扩展应用于VM。
但是,我尝试使用合并合并为一个扩展中提到的Powershell脚本来实现基于您所要求的查询的需求。

我的地形配置:

main.tf:

provider "azurerm" {
    features {}
}

# Create a resource group
resource "azurerm_resource_group" "rg" {
  name     = "demorg-vk"
  location = "East US 2"
}

# Create a virtual network
resource "azurerm_virtual_network" "vnet" {
  name                = "examplevksb-vnet"
  resource_group_name = azurerm_resource_group.rg.name
  location            = azurerm_resource_group.rg.location
  address_space       = ["10.0.0.0/16"]
}

# Create a subnet
resource "azurerm_subnet" "subnet" {
  name                 = "examplevksb-subnet"
  resource_group_name  = azurerm_resource_group.rg.name
  virtual_network_name = azurerm_virtual_network.vnet.name
  address_prefixes     = ["10.0.1.0/24"]
}

# Create a public IP
resource "azurerm_public_ip" "pip" {
  name                = "examplevksb-publicip"
  resource_group_name = azurerm_resource_group.rg.name
  location            = azurerm_resource_group.rg.location
  allocation_method   = "Dynamic"
}

# Create a network interface
resource "azurerm_network_interface" "nic" {
  name                = "examplevksb-nic"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name

  ip_configuration {
    name                          = "internal"
    subnet_id                     = azurerm_subnet.subnet.id
    private_ip_address_allocation = "Dynamic"
    public_ip_address_id          = azurerm_public_ip.pip.id
  }
}

# Create a storage account
resource "azurerm_storage_account" "sa" {
  name                     = "examplestoraccvksb"
  resource_group_name      = azurerm_resource_group.rg.name
  location                 = azurerm_resource_group.rg.location
  account_tier             = "Standard"
  account_replication_type = "LRS"
}

# Create a blob container
resource "azurerm_storage_container" "sc" {
  name                  = "vksbscripts"
  storage_account_name  = azurerm_storage_account.sa.name
  container_access_type = "private"
}

# Upload PowerShell scripts to the blob container
resource "azurerm_storage_blob" "script1" {
  name                   = "vm_adds_testing_part0.ps1"
  storage_account_name   = azurerm_storage_account.sa.name
  storage_container_name = azurerm_storage_container.sc.name
  type                   = "Block"
  source                 = "scripts/vm_adds_testing_part0.ps1"
}

resource "azurerm_storage_blob" "script2" {
  name                   = "vm_adds_testing_part1.ps1"
  storage_account_name   = azurerm_storage_account.sa.name
  storage_container_name = azurerm_storage_container.sc.name
  type                   = "Block"
  source                 = "scripts/vm_adds_testing_part1.ps1"
}

resource "azurerm_storage_blob" "script3" {
  name                   = "vm_adds_testing_part2.ps1"
  storage_account_name   = azurerm_storage_account.sa.name
  storage_container_name = azurerm_storage_container.sc.name
  type                   = "Block"
  source                 = "scripts/vm_adds_testing_part2.ps1"
}

# VM Creation
resource "azurerm_windows_virtual_machine" "testing_vm" {
  name                = "exvksb-vkvm"
  resource_group_name = azurerm_resource_group.rg.name
  location            = azurerm_resource_group.rg.location
  size                = "Standard_D2_v3"
  admin_username      = var.VIRTUAL_MACHINE_ADMIN_USERNAME
  admin_password      = var.VIRTUAL_MACHINE_ADMIN_PASSWORD

  network_interface_ids = [
    # Assuming you have a network interface defined elsewhere
    # Replace with your actual network interface ID
    "/subscriptions/YOUR_SUBSCRIPTION_ID/resourceGroups/YOUR_RESOURCE_GROUP/providers/Microsoft.Network/networkInterfaces/YOUR_NIC_NAME "
  ]

  os_disk {
    caching              = "ReadWrite"
    storage_account_type = "Standard_LRS"
  }

  source_image_reference {
    publisher = "MicrosoftWindowsServer"
    offer     = "WindowsServer"
    sku       = "2019-Datacenter"
    version   = "latest"
  }
}

# Custom Script Extension to download and execute the scripts
resource "azurerm_virtual_machine_extension" "extension" {
  name                 = "examplevksb-vm-extension"
  virtual_machine_id   = azurerm_windows_virtual_machine.testing_vm.id
  publisher            = "Microsoft.Compute"
  type                 = "CustomScriptExtension"
  type_handler_version = "1.10"

  settings = <<SETTINGS
    {
      "scriptToRun": "scripts/master_script.ps1",
      "fileUris": [
        "${azurerm_storage_blob.script1.url}",
        "${azurerm_storage_blob.script2.url}",
        "${azurerm_storage_blob.script3.url}"
      ],
      "commandToExecute": "powershell -ExecutionPolicy Unrestricted -File scripts/master_script.ps1 -username ${var.VIRTUAL_MACHINE_ADMIN_USERNAME} -password ${var.VIRTUAL_MACHINE_ADMIN_PASSWORD}"
    }
SETTINGS
}

variable.tf:

variable "VIRTUAL_MACHINE_ADMIN_USERNAME" {
  description = "Admin username for the VM"
  type        = string
  default     = "adminUser"
}

variable "VIRTUAL_MACHINE_ADMIN_PASSWORD" {
  description = "Admin password for the VM"
  type        = string
  sensitive   = true
  default     = "Random password"  # Change this to a more secure password before deploying
}

output.tf:

output "vm_id" {
  value       = azurerm_windows_virtual_machine.testing_vm.id
  description = "The ID of the Virtual Machine."
}

输出:

相关问题