我正在使用一个模块的输出将其传递给另一个模块:
output "subnets_id_wan" {
value = "${azurerm_subnet.wan.*.id}"
depends_on = [
"azurerm_subnet.wan",
]
}
在输出中我得到
security_groups_id_wan = [
/subscriptions/111-222-333-4445/resourceGroups/default_resource_group/providers/Microsoft.Network/networkSecurityGroups/DF-DTAP-WAN-Subnet-Security-Group
]
我正在将此输出传递给网络接口资源
#Create network interface
resource "azurerm_network_interface" "public_ip" {
count = "${var.enable_public_ip ? 1 : 0}"
provider = "azurerm.base"
name = "Network_Inteface_Public_IP_${var.env_name}_${var.azure_instance_names}"
location = "${module.azure_vnet_dtap.resource_group_location}"
resource_group_name = "${module.azure_vnet_dtap.resource_group_name}"
network_security_group_id = "${var.enable_security_group ? element(concat(azurerm_network_security_group.nsg.*.id,list("")),0) : element(concat(list("")),0)}"
ip_configuration {
name = "${var.env_name}_${var.ip_configuration_name}"
#subnet_id = "${module.azure_vnet_dtap.subnets_id_single_wan}"
subnet_id = "${element(module.azure_vnet_dtap.subnets_id_wan,count.index)}"
private_ip_address_allocation = "${var.is_static_ip ? "static" : "dynamic"}"
private_ip_address = "${var.static_ip}"
public_ip_address_id = "${azurerm_public_ip.public_ip.id}"
}
tags = "${
merge(map("Name", format("%s %s %s",var.env_name,var.azure_instance_names, "Public_IP_Interface")),var.global_tags, var.vpc_tags)}"
}
和虚拟机代码:
resource "azurerm_virtual_machine" "one_volume" {
count = "${var.single_volume > 0 ? 1 : 0}"
provider = "azurerm.base"
name = "VM_${var.env_name}_${var.azure_instance_names}"
location = "${module.azure_vnet_dtap.resource_group_location}"
resource_group_name = "${module.azure_vnet_dtap.resource_group_name}"
availability_set_id = "${azurerm_availability_set.availability_set.id}"
network_interface_ids = ["${var.enable_public_ip ? element(concat(azurerm_network_interface.public_ip.*.id,list("")),0) : element(concat(azurerm_network_interface.no_public_ip.*.id,list("")),0)}"]
vm_size = "${var.vm_size}"
delete_os_disk_on_termination = "true"
delete_data_disks_on_termination = "true"
以上所有方法都很好
现在,我更改了输出以摆脱Azure订阅
output "subnets_id_wan" {
value = "${slice(split("/",join(",",azurerm_subnet.wan.*.id)), length(split("/",join(",",azurerm_subnet.wan.*.id)))-1, length(split("/",join(",",azurerm_subnet.wan.*.id))))}"
depends_on = [
"azurerm_subnet.wan",
]
输出:
security_groups_id_wan = [
DF-DTAP-WAN-Subnet-Security-Group
]
在运行terraform时申请获得:
* module.azure_vm.azurerm_virtual_machine.one_volume: 1 error(s) occurred:
* module.azure_vm.azurerm_virtual_machine.one_volume: Resource 'azurerm_network_interface.public_ip' does not have attribute 'id' for variable 'azurerm_network_interface.public_ip.*.id'
将此作为测试:
network_interface_ids = ["${element(azurerm_network_interface.public_ip.*.id,count.index)}"]
但有相同的错误,如果未在输出中使用切片,则一切正常