如何设置虚拟机映像详细信息,例如出版商、报价、sku 和版本

How to set the virtual machine image details e.g. publisher, offer, sku and version

我有一个 Terraform 脚本,它基于 Azure Marketplace 中的这个图像在 Azure 上创建一个虚拟机: https://azuremarketplace.microsoft.com/en-us/marketplace/apps/gitlab.gitlab-ce

但我不知道如何识别这些字段中的值:

这是 Terraform 脚本的一个片段

resource "azurerm_virtual_machine" "gitlab_vm" {
  # ... other configuration

  storage_image_reference {
    publisher = "GitLab"
    offer     = "GitLab Community Edition"
    # sku       = "???"
    version   = "latest"
  }

  # ... other configuration
}

如果我 运行 使用 terraform plan 之前的详细信息,例如publisher = "GitLabXXX"(不存在的东西),那么 Terraform 不会引发任何错误。当我 运行 terraform apply 一段时间后出现错误并且未创建 VM 资源(尽管创建了所有其他资源,例如网络资源)。

这与 Azure ARM 模板的应用方式类似:

"imageReference": {
    "publisher": "[variables('pubName')]",
    "offer": "[variables('offerName')]",
    "sku" : "[parameters('skuName')]",
    "version":"latest"
},

我有一些启动的虚拟机和 运行 我在互联网上找到的类似 Terraform 配置(参见下面的 Ubuntu 示例),但是转换信息的规则是什么从 Azure 市场网页到脚本?

  storage_image_reference {
    publisher = "Canonical"
    offer     = "UbuntuServer"
    sku       = "16.04-LTS"
    version   = "latest"
  }

可能有更有效的方法来执行此操作,但我通常使用 Azure CLI 来获取发布者、产品、SKU 和版本。首先,如果您执行以下操作:

az vm image list --offer GitLab -o table --all

您将得到一个包含报价、发布者、SKU、URN 和版本的列表。在你的情况下,gitlab-ce 应该是这样的:

  • 发布者:gitlab
  • 报价:gitlab-ce
  • SKU:gitlab-ce
  • 版本:1.0.4

我很确定 azurerm 提供程序只是使用这些值调用 ARM,因此它应该与您从 CLI 获得的值相匹配,但如果我弄错了,我肯定可以在这一点上得到更正。

您还需要添加 plan block。可以通过 CLI 使用如下内容检索其详细信息:

az vm image show --location westus --urn gitlab:gitlab-ce:gitlab-ce:1.0.4 -o json

所以你的计划块看起来像:

plan {
  name = "gitlab-ce" 
  publisher = "gitlab"
  product = "gitlab-ce"
}