英文:
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 "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)
}
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论