admin管理员组文章数量:1122846
Is it possible to use count.index in a variable name, so I can use a single map variable for multiple resources? For example
variables.tf:
variable "disk_share_sizes" {
type = map(number)
description = "Sizes for VM Disks and File Shares"
default = {
fileshare = 2048
pvs0 = 20
pvs1 = 10
cca0 = 50
cca1 = 80
}
}
compute.tf
resource "azurerm_managed_disk" "vm_pvs" {
count = 2
name = "pvs-disk-${format("%02d", count.index + 1)}"
location = "useast"
resource_group_name = "MyRGName"
storage_account_type = "MyStorAccount"
create_option = "Empty"
disk_size_gb = var.vm_disk_sizes.pvs[count.index]
}
resource "azurerm_managed_disk" "vm_cca" {
count = 2
name = "cca-disk-${format("%02d", count.index + 1)}"
location = "useast"
resource_group_name = "MyRGName"
storage_account_type = "MyStorAccount"
create_option = "Empty"
disk_size_gb = var.vm_disk_sizesa[count.index]
}
This produces the below error (repeated for both resources):
│ Error: Invalid index
│
│ on compute.tf line 8, in resource "azurerm_managed_disk" "my_vms":
│ 8: disk_size_gb = var.vm_disk_sizes.vm[count.index]
│ ├────────────────
│ │ count.index is a number
│ │ var.vm_disk_sizes.vm is a number
│
│ This value does not have any indices.
I can't use "var.vm_disk_sizes.vm${count.index}"
as this is a variable name and putting it in double quotes will just make it a string. I did try using ${var.vm_disk_sizes.vm${count.index}}
but it doesn't like this either
Can this be done without making the variable a list rather than a map? I'm using a map as my actual map is larger, and it would be difficult to split this into multiple lists for all the different VMs and disk sizes.
Is it possible to use count.index in a variable name, so I can use a single map variable for multiple resources? For example
variables.tf:
variable "disk_share_sizes" {
type = map(number)
description = "Sizes for VM Disks and File Shares"
default = {
fileshare = 2048
pvs0 = 20
pvs1 = 10
cca0 = 50
cca1 = 80
}
}
compute.tf
resource "azurerm_managed_disk" "vm_pvs" {
count = 2
name = "pvs-disk-${format("%02d", count.index + 1)}"
location = "useast"
resource_group_name = "MyRGName"
storage_account_type = "MyStorAccount"
create_option = "Empty"
disk_size_gb = var.vm_disk_sizes.pvs[count.index]
}
resource "azurerm_managed_disk" "vm_cca" {
count = 2
name = "cca-disk-${format("%02d", count.index + 1)}"
location = "useast"
resource_group_name = "MyRGName"
storage_account_type = "MyStorAccount"
create_option = "Empty"
disk_size_gb = var.vm_disk_sizes.cca[count.index]
}
This produces the below error (repeated for both resources):
│ Error: Invalid index
│
│ on compute.tf line 8, in resource "azurerm_managed_disk" "my_vms":
│ 8: disk_size_gb = var.vm_disk_sizes.vm[count.index]
│ ├────────────────
│ │ count.index is a number
│ │ var.vm_disk_sizes.vm is a number
│
│ This value does not have any indices.
I can't use "var.vm_disk_sizes.vm${count.index}"
as this is a variable name and putting it in double quotes will just make it a string. I did try using ${var.vm_disk_sizes.vm${count.index}}
but it doesn't like this either
Can this be done without making the variable a list rather than a map? I'm using a map as my actual map is larger, and it would be difficult to split this into multiple lists for all the different VMs and disk sizes.
Share Improve this question edited Nov 21, 2024 at 18:02 Dragonsys asked Nov 21, 2024 at 16:34 DragonsysDragonsys 271 silver badge5 bronze badges2 Answers
Reset to default 2Ideally, you would use the for_each
meta-argument instead (since your variable is defined as a map):
resource "azurerm_managed_disk" "my_vms" {
for_each = var.vm_disk_sizes
name = "vm-disk-${each.key}"
location = "useast"
resource_group_name = "MyRGName"
storage_account_type = "MyStorAccount"
create_option = "Empty"
disk_size_gb = each.value
}
I did find a way to do this, using part of Marko E's answer, for anyone else who might come along later. I had to make lists for each item, but I can keep them groups by using an object mapping
I changed my variable map to an object, and then var.vm_disk_sizes.pvs[count.index]
worked as expected
variables.tf
variable "disk_share_sizes" {
description = "Sizes for VM Disks and File Shares"
type = object({
pvs = list(number)
cca = list(number)
})
default = {
fileshare = 2048
pvs = [20,10]
cca = [50,80]
}
}
compute.tf
resource "azurerm_managed_disk" "vm_pvs" {
count = 2
name = "pvs-disk-${format("%02d", count.index + 1)}"
location = "useast"
resource_group_name = "MyRGName"
storage_account_type = "MyStorAccount"
create_option = "Empty"
disk_size_gb = var.disk_share_sizes.pvs[count.index]
}
resource "azurerm_managed_disk" "vm_cca" {
count = 2
name = "cca-disk-${format("%02d", count.index + 1)}"
location = "useast"
resource_group_name = "MyRGName"
storage_account_type = "MyStorAccount"
create_option = "Empty"
disk_size_gb = var.disk_share_sizes.cca[count.index]
}
本文标签: azureTerraform Use countindex in variable nameStack Overflow
版权声明:本文标题:azure - Terraform: Use count.index in variable name - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736308874a1933813.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论