Terraform是否可以根据Azure订阅选择要使用的操作系统镜像?

huangapple go评论63阅读模式
英文:

Can Terraform choose which OS image to use according to Azure Subscription?

问题

You can select the OS Image ID based on your subscription using conditional statements. Here's a possible approach:

我可以使用条件语句基于你的订阅来选择操作系统镜像 ID。以下是可能的方法:

main.tf

resource "azurerm_linux_virtual_machine" "vm" {
for_each = var.vm_template
...(省略)
source_image_id = each.value.prd_source_image_id
...(省略)
when = var.subscription_type == "production"
}

resource "azurerm_linux_virtual_machine" "vm" {
for_each = var.vm_template
...(省略)
source_image_id = each.value.dev_source_image_id
...(省略)
when = var.subscription_type == "dev"
}


在这个示例中,使用了 `when` 参数来根据 `var.subscription_type` 的值选择不同的镜像 ID。如果订阅类型是 "production",将使用 `prd_source_image_id`,如果是 "dev",将使用 `dev_source_image_id`。

<details>
<summary>英文:</summary>

I am using Map variable for Terraform Variables as below. When I run Terraform, I want to select prd_source_image_id if my current subscription is production, and dev_source_image_id if my current subscription is dev. Is it possible?

var.tf

variable "vm_template" {
type = map(any)
default = {
vm1 = {
prd_source_image_id = "prd_image"
dev_source_image_id = "dev_image"
}
}
}


main.tf

resource "azurerm_linux_virtual_machine" "vm" {
for_each = var.vm_template
...(omission)
source_image_id = each.value.source_image_id
...(omission)
}



You must be able to select OS Image id based on your subscription using conditional statements or regular expressions.

</details>


# 答案1
**得分**: 1

你可以使用 [azurerm_client_config][1] 和 [lookup function][2] 数据源:

```hcl
variable "vm_template" {
  type = map(any)
  default = {
    vm1 = {
      prd_source_image_id = "prd_image",
      dev_source_image_id = "dev_image",
      default_image       = "default_image"
    }
  }
}

variable "subscriptions_to_env" {
  type = map(string)
  default = {
    "aaa-111-555-etc" = "dev",
    "111-aaa-999-etc" = "prd"
  }
}

data "azurerm_client_config" "current" {}

locals {
  current_env = lookup(var.subscriptions_to_env, data.azurerm_client_config.current.subscription_id, "None")
}

resource "azurerm_linux_virtual_machine" "vm" {
  for_each = var.vm_template
  ...(omission)
  source_image_id     = local.current_env == "dev" ? each.value.dev_source_image_id : (
                          local.current_env == "prd" ? each.value.prd_source_image_id : each.value.default_image
                        )
  ...(omission)
}

注:
我建议你使用默认镜像(prd、dev或其他)。
条件可以按照以下方式阅读:

  • 如果环境是dev:使用dev镜像
  • 否则,如果环境是prd:使用prd镜像
  • 否则,使用默认镜像

如果这对你有帮助,请告诉我。

英文:

you can use azurerm_client_config and lookup function data source :

    variable &quot;vm_template&quot; {
      type = map(any)
      default = {
        vm1 = {
          prd_source_image_id = &quot;prd_image&quot;
          dev_source_image_id = &quot;dev_image&quot;
          default_image       = &quot;default_image&quot;
          }
      }
    }

    variable &quot;subscriptions_to_env&quot; {
      type = map(string)
      default = {
        &quot;aaa-111-555-etc&quot; = &quot;dev&quot;,
        &quot;111-aaa-999-etc&quot; = &quot;prd&quot;
      }
    }

    data &quot;azurerm_client_config&quot; &quot;current&quot; {}

    locals {
      current_env = lookup(var.subscriptions_to_env, data.azurerm_client_config.current.subscription_id, &quot;None&quot;)
    }


    resource &quot;azurerm_linux_virtual_machine&quot; &quot;vm&quot; {
      for_each = var.vm_template
    ...(omission)
      source_image_id     = local.current_env == &quot;dev&quot; ? each.value.dev_source_image_id : (
                              local.current_env == &quot;prd&quot; ? each.value.prd_source_image_id : each.value.default_image
                              )
    ...(omission)
    }

Notes :
I suggest you use a default image (prd, dev or other).
The condition can be read as follow :

If env is dev: image dev
else if env is prd: image prd
else image default

Let me know if it helped you

huangapple
  • 本文由 发表于 2023年3月23日 10:38:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75818829.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定