我们正在开发一个模块来创建基于Linux的NVA。每个NVA需要连接3个NIC,每个NIC都位于不同的子网上。目前,该模块能够成功创建3个NIC,但是NIC创建的输出为tuple
类型,我们还没有弄清楚如何使用tuple
中存储的NIC资源ID。作为创建VM的输入。
我们正在尝试使模块具有足够的通用性,以便能够使用它创建可能没有多个NIC的其他类型的Linux VM。
调用模块以创建NIC的工作代码:
module "linux_vm" {
source = "../modules/test_module"
resource_group_name = azurerm_resource_group.saca_rg.name
nic_names = ["nic-mgmt","nic-int","nic-ext"]
subnet_id = ["${local.subnet_ids[4]}","${local.subnet_ids[3]}","${local.subnet_ids[2]}"]
ip_allocation = ["Static","Static","Static"]
static_ip = ["${cidrhost(local.static_ips[4], 4)}","${cidrhost(local.static_ips[3], 4)}","${cidrhost(local.static_ips[2], 4)}"]
}
创建NIC的模块代码:
resource "azurerm_network_interface" "vm_nic" {
count = length(var.nic_names)
name = var.nic_names[count.index]
resource_group_name = data.azurerm_resource_group.rg.name
location = data.azurerm_resource_group.rg.location
ip_configuration {
name = "${var.nic_names[count.index]}-ipconfig"
subnet_id = var.subnet_id[count.index]
private_ip_address_allocation = var.ip_allocation[count.index]
private_ip_address = var.static_ip[count.index]
}
}
此输出为tuple
,类似于以下输出:
"outputs": {
"nic_ids": {
"value": [
"/subscriptions/<sub_id>/resourceGroups/rg/providers/Microsoft.Network/networkInterfaces/nic-mgmt",
"/subscriptions/<sub_id>/resourceGroups/rg/providers/Microsoft.Network/networkInterfaces/nic-int",
"/subscriptions/<sub_id>/resourceGroups/rg/providers/Microsoft.Network/networkInterfaces/nic-ext"
],
"type": [
"tuple",
[
"string",
"string",
"string"
]
]
}
},
当我们尝试使用以下参数将此输出用作VM创建块的输入时:
network_interface_ids = [azurerm_network_interface.vm_nic.*.id]
我们收到以下错误:
Error: Incorrect attribute value type
on ../modules/test_module/main.tf line 27, in resource "azurerm_linux_virtual_machine" "vm":
27: network_interface_ids = [azurerm_network_interface.vm_nic.*.id]
|----------------
| azurerm_network_interface.vm_nic is tuple with 3 elements
Inappropriate value for attribute "network_interface_ids": element 0: string
required.
答案 0 :(得分:0)
发现了问题所在。
我们正在尝试使用以下行:
network_interface_ids = [azurerm_network_interface.vm_nic.*.id]
但是,由于tuple
就像一个列表...这基本上是将列表嵌套在列表内。当我们切换到下面时,所有3个NIC均按预期分配:
network_interface_ids = azurerm_network_interface.vm_nic.*.id