gpt4 book ai didi

azure - 使用 terraform 创建 Azure VM 时版本是否是必需的?

转载 作者:行者123 更新时间:2023-12-03 05:20:29 27 4
gpt4 key购买 nike

因此,自过去 3 周以来,我一直在使用 terraform,并一直尝试使用它在我们的 Azure 帐户中创建自托管的 GitHub Actions 运行器。

我们在 Azure 计算库中有一个共享的 Windows VM 镜像,我计划将其用作 GA 运行程序的基础镜像。我注意到这些共享 Windows 虚拟机镜像通常没有附加任何版本,它们只附加了发​​布商、报价和 SKU。

我还通过从虚拟机创建新镜像来检查是否有人错过将版本附加到虚拟机来进行验证,但没有共享镜像实际上没有附加版本。

是的,他们确实有版本,但它没有像 Microsoft 平台镜像那样附加。

共享图像示例:

enter image description here

现在我发现在 terraform 中,可以使用以下资源创建运行程序:azurerm_windows_virtual_machineazurerm_virtual_machine 资源。

我使用它们来测试运行者的创建,下面是使用的 terraform 代码:​​

data "azurerm_shared_image" "win19_gold_image" {
provider = azurerm.gi
name = "Windows-2019_base"
gallery_name = data.azurerm_shared_image_gallery.cap_win_gold_image_gallery.name
resource_group_name = "gi-rg"
}

resource "azurerm_virtual_machine" "win_runners_gold_image_based" {
provider = azurerm.og
name = "ga-win-gold-1"
location = "East US"
count = "1" # if I need to increase the number of VMs.
resource_group_name = data.azurerm_resource_group.dts_rg.name
network_interface_ids = [azurerm_network_interface.azure_win_runner_gold_nic[count.index].id,]
vm_size = "Standard_D4ads_v5"

delete_os_disk_on_termination = true
delete_data_disks_on_termination = true

storage_image_reference {
publisher = data.azurerm_shared_image.win19_gold_image.identifier[0].publisher
offer = data.azurerm_shared_image.win19_gold_image.identifier[0].offer
sku = data.azurerm_shared_image.win19_gold_image.identifier[0].sku
# Here I get the error: Error: compute.VirtualMachinesClient#CreateOrUpdate: Failure sending request: StatusCode=400 -- Original Error: Code="InvalidParameter" Message="The value of parameter imageReference.version is invalid." Target="imageReference.version"
}

storage_os_disk {
name = "ga-win-gold-os-disk-1"
caching = "None"
create_option = "FromImage"
managed_disk_type = "StandardSSD_LRS"
}

os_profile {
computer_name = "ga-win-gold-1"
admin_username = "svc"
admin_password = var.WINDOWS_ADMIN_PASS
}

os_profile_windows_config {
enable_automatic_upgrades = true
provision_vm_agent = true
}

storage_data_disk {
name = "ga-win-gold-data-disk-1"
caching = "None"
create_option = "Empty"
disk_size_gb = var.disk_size_gb
lun = 0
managed_disk_type = "StandardSSD_LRS"
}
}

或者

data "azurerm_shared_image" "win19_gold_image" {
provider = azurerm.gi
name = "Windows-2019_base"
gallery_name = data.azurerm_shared_image_gallery.cap_win_gold_image_gallery.name
resource_group_name = "gi-rg"
}

resource "azurerm_windows_virtual_machine" "azure_win_runner" {
provider = azurerm.og
name = "vm-github-actions-win-${count.index}"
resource_group_name = data.azurerm_resource_group.dts_rg.name
location = "East US"
size = var.windows-vm-size
count = "${var.number_of_win_az_instances}"
network_interface_ids = [
azurerm_network_interface.azure_win_runner_nic[count.index].id,
]
computer_name = "vm-ga-win-${count.index}"
admin_username = var.windows-admin-username
admin_password = var.WINDOWS_ADMIN_PASS

os_disk {
name = "vm-github-actions-win-${count.index}-os-disk"
caching = "None"
storage_account_type = "StandardSSD_LRS"
}

source_image_reference {
publisher = data.azurerm_shared_image.win19_gold_image.identifier[0].publisher
offer = data.azurerm_shared_image.win19_gold_image.identifier[0].offer
sku = data.azurerm_shared_image.win19_gold_image.identifier[0].sku
version = data.azurerm_shared_image.win19_gold_image.identifier[0].version # says this object does not have a version attached to it.
# or version = "latest" or any other correct version string will throw error at time of apply that such a version does not exist.
}

enable_automatic_updates = true
provision_vm_agent = true
}

如果我使用 azurerm_virtual_machine,那么如果我忽略 storage_image_reference 中的版本,我会收到错误:

Error: compute.VirtualMachinesClient#CreateOrUpdate: Failure sending request: StatusCode=400 -- Original Error: Code="InvalidParameter" Message="The value of parameter imageReference.version is invalid." Target="imageReference.version"

如果我添加版本,则会收到错误

Error: Unsupported attribute. 
This object does not have an attribute named "version".

使用azurerm_windows_virtual_machine时,如果我删除版本参数,terraform会提示版本是必需的,并且在提供诸如1.0.0latest之类的字符串时>,在应用(terraform apply)时它会提示这样的版本不存在。

如果我从 data.azurerm_shared_image.cap_win19_gold_image 中提取版本,它会提示该对象没有版本。

如果版本是强制性的,但如果版本不可用于 azure 共享镜像,我对如何使用共享镜像使用 terraform 创建虚拟机感到困惑。请告知我缺少什么?

如有任何帮助,我们将不胜感激。

谢谢,塞卡尔

最佳答案

似乎获得了需要使用另一个资源[1]和另一个数据源[2]的图像版本:

data "azurerm_image" "win19_gold_image" {
name = "Windows-2019_base"
resource_group_name = "gi-rg"
}

resource "azurerm_shared_image_version" "win19_gold_image" {
name = "0.0.1"
gallery_name = data.azurerm_shared_image.win19_gold_image.gallery_name
image_name = data.azurerm_shared_image.win19_gold_image.name
resource_group_name = data.azurerm_shared_image.win19_gold_image.resource_group_name
location = data.azurerm_shared_image.win19_gold_image.location
managed_image_id = data.azurerm_image.win19_gold_image.id
}

然后在 azurerm_windows_virtual_machine 资源的 source_image_reference block 中:

  source_image_reference {
publisher = data.azurerm_shared_image.win19_gold_image.identifier[0].publisher
offer = data.azurerm_shared_image.win19_gold_image.identifier[0].offer
sku = data.azurerm_shared_image.win19_gold_image.identifier[0].sku
version = azurerm_shared_image_version.win19_gold_image.name
}

看起来name参数实际上是图像的版本[3]:

name - (Required) The version number for this Image Version, such as 1.0.0. Changing this forces a new resource to be created.

<小时/>

[1] https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/shared_image_version

[2] https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/image

[3] https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/shared_image_version#name

关于azure - 使用 terraform 创建 Azure VM 时版本是否是必需的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72690209/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com