我对 Terraform 很陌生,并且在模块/子目录之间传递变量时遇到了一些问题。
我有一个像这样的结构:
.
|-- main.tf
|-- variables.tf
|-- terraform.tfvars
|-- data.tf
|-- compute
|-- main.tf
|-- variables.tf
|-- terraform.tfvars
|-- network
|-- main.tf
|-- variables.tf
|-- terraform.tfvars
我在根目录中的 main.tf 如下所示:
provider "azurerm" {
}
resource "azurerm_resource_group" "test" {
name = "${var.resourcegroup}"
location = "${var.location}"
tags {
costcenter = "costcenter_nr"
environment = "test"
}
}
resource "azurerm_virtual_network" "test" {
name = "${var.vnet}"
location = "${var.location}"
resource_group_name = "${var.resourcegroup}"
address_space = ["10.102.2.0/23"]
subnet {
name = "${var.subnet_agw}"
address_prefix = "10.102.3.128/28"
}
depends_on = ["azurerm_resource_group.test"]
}
module "compute" {
source = "./compute"
}
module "network" {
source = "./network"
}
在网络目录中,我想为 vm 创建网络接口。因此网络接口取决于子网 ID。vm(我想在计算中使用模板创建)取决于网络接口 ID。
在根目录的 data.tf 中,我输出了子网 ID:
data "azurerm_subnet" "agw" {
name = "${var.subnet_agw}"
virtual_network_name = "${var.vnet}"
resource_group_name = "${var.resourcegroup}"
depends_on = ["azurerm_virtual_network.test"]
}
output "subnet_ag" {
value = "${data.azurerm_subnet.agw.id}"
}
如何在 network/main.tf 中使用该输出/变量,以便配置网络接口?
network/main.tf 看起来像:
resource "azurerm_network_interface" "sql_server" {
name = "${var.sql_server}"
location = "${var.location}"
resource_group_name = "${var.resourcegroup}"
ip_configuration {
name = "${var.sql_server}"
subnet_id = "${????????}"
private_ip_address_allocation = "dynamic"
}
depends_on = ["azurerm_resource_group.test"]
}
另外,由于依赖项是由 main.tf 创建的,因此此依赖项会起作用吗?!