如何将输出从一个模块传递到另一个模块?
How to pass Outputs from One Module to another?
我有两个模块。
- 一个叫
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}"
}
Error: resource 'azurerm_network_interface.no_public_ip' config: unknown resource 'azurerm_subnet.wan' referenced in variable azurerm_subnet.wan.id
Error: resource 'azurerm_network_security_rule.security_rule_default' config: unknown resource 'azurerm_network_security_group.wan' referenced in variable azurerm_network_security_group.wan.name
Error: module "module-azure-vm-instance-zabbix-proxy": "azurerm_subnet" is not a valid argument
在模块 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",
]
}
你应该像这样引用输出:
"${module.MODULE_NAME.OUTPUT_NAME}"
在你的情况下应该是:
"${module.module-azure-vnet-dtap.subnets_id_wan}"
此外,您只能访问输出,不能访问模块中的资源 (afaik)。
我自己找到了解决方案,这是连接两个模块所需的整个结构。
在 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 proxy 模块中的变量来接收值>
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"
}
我有两个模块。
- 一个叫
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}"
}
Error: resource 'azurerm_network_interface.no_public_ip' config: unknown resource 'azurerm_subnet.wan' referenced in variable azurerm_subnet.wan.id
Error: resource 'azurerm_network_security_rule.security_rule_default' config: unknown resource 'azurerm_network_security_group.wan' referenced in variable azurerm_network_security_group.wan.name
Error: module "module-azure-vm-instance-zabbix-proxy": "azurerm_subnet" is not a valid argument
在模块 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",
]
}
你应该像这样引用输出:
"${module.MODULE_NAME.OUTPUT_NAME}"
在你的情况下应该是:
"${module.module-azure-vnet-dtap.subnets_id_wan}"
此外,您只能访问输出,不能访问模块中的资源 (afaik)。
我自己找到了解决方案,这是连接两个模块所需的整个结构。
在 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 proxy 模块中的变量来接收值>
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"
}