我有两个模块。
module-azure-vnet
module-azure-vm-instance-zabbix-proxy
我想将输出从模块module-azure-vnet
传递到模块module-azure-vm-instance-zabbix-proxy
。第一个模块正常工作,但是第二个模块需要子网,安全组和路由的输入。
main.tf
module "module-azure-vnet-dtap" {
source = "/Users/username/project/module-azure-vnet-dtap"
}
variable "subnets_id_wan" {}
module "module-azure-vm-instance-zabbix-proxy" {
source = "/Users/username/project/module-azure-vm-instance-zabbix-proxy"
azurerm_subnet = "${module.module-azure-vnet-dtap.azurerm_subnet.wan.id}"
}
错误:资源'azurerm_network_interface.no_public_ip'配置:变量azurerm_subnet.wan.id中引用的未知资源'azurerm_subnet.wan'
错误:资源'azurerm_network_security_rule.security_rule_default'配置:变量azurerm_network_security_group.wan.name中引用的未知资源'azurerm_network_security_group.wan
错误:模块“ module-azure-vm-instance-zabbix-proxy”:“ azurerm_subnet”不是有效的参数
在module-azure-vnet-dtap
模块中
resource "azurerm_subnet" "wan" {
count = "${var.enable_wan_subnet ? 1 : 0}"
provider = "azurerm.base"
name = "${format("%s-%s-%s", var.environment_name, "WAN", "Subnet")}"
virtual_network_name = "${azurerm_virtual_network.this.name}"
resource_group_name = "${azurerm_resource_group.this.name}"
address_prefix = "${cidrsubnet(var.cidr_block,5,count.index)}"
route_table_id = "${azurerm_route_table.wan.id}"
network_security_group_id = "${azurerm_network_security_group.wan.id}"
}
resource "azurerm_network_security_group" "wan" {
count = "${var.enable_wan_subnet ? 1 : 0}"
provider = "azurerm.base"
name = "${format("%s-%s", var.environment_name, "WAN-Subnet-Security-Group")}"
location = "${azurerm_resource_group.this.location}"
resource_group_name = "${azurerm_resource_group.this.name}"
}
output "subnets_id_wan" {
value = "${azurerm_subnet.wan.*.id}"
depends_on = [
"azurerm_subnet.wan",
]
}
答案 0 :(得分:0)
您应该参考如下输出:
"${module.MODULE_NAME.OUTPUT_NAME}"
在您的情况下应该是:
"${module.module-azure-vnet-dtap.subnets_id_wan}"
此外,您只能访问输出,而不能访问模块(afaik)的资源。
答案 1 :(得分:0)
我自己找到了解决方案,这是连接两个模块所需的整体结构。
在VNET模块示例中设置变量:
variable "resource_group_name" {
default = "default_resource_group"
}
variable "region_name" {
default = "ukwest"
}
声明输出:
output "security_groups_id_wan" {
value = "${element(concat(azurerm_network_security_group.wan.*.id, list("")),0)}"
depends_on = [
"azurerm_subnet.wan",
]
}
您需要zabbix代理模块中的变量才能接收值>
variable "resource_group_name" {
default = ""
}
variable "resource_group_location" {
default = ""
}
以及在外部文件夹中,模块的main.tf在哪里>
module "module-azure-vm-instance-zabbix-proxy" {
source = "/Users/username/project/module-azure-vm-instance-zabbix-proxy"
resource_group_name = "${module.module-azure-vnet-dtap.resource_group_name}"
resource_group_location = "${module.module-azure-vnet-dtap.resource_group_location}"
subnets_id_wan = "${module.module-azure-vnet-dtap.subnets_id_wan}"
security_groups_name = "${module.module-azure-vnet-dtap.security_groups_name_dmz}"
environment_name = "${module.module-azure-vnet-dtap.environment_name}"
}
module module-azure-vnet-dtap {
source = "/Users/username/project/module-azure-vnet-dtap"
}