如何 inherit/pass 对 terraform 中的变量
How to inherit/pass on variables in terraform
我对 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"
}
在网络目录中,我想为虚拟机创建网络接口。因此网络接口取决于子网 ID。虚拟机(我想在计算中使用模板创建)取决于网络接口 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 中使用 output/variable 以便配置网络接口?
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 创建的,因此该依赖项是否有效?!
在您的 main.tf 根级别添加:
module "network" {
source = "./network"
subnet_id = "{data.azurerm_subnet.agw.id}"
}
在您的网络模块中添加变量引用。另外一定要声明变量:
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 = "${var.subnet_id}"
private_ip_address_allocation = "dynamic"
}
depends_on = ["azurerm_resource_group.test"]
}
查看间接和直接依赖关系 dependencies。在您的代码中,您可以使用来创建这样的间接依赖项 resource_group_name = "${azurerm_resource_group.test.id}"
。那么你不需要显式定义它。
resource "azurerm_virtual_network" "test" {
name = "${var.vnet}"
location = "${var.location}"
resource_group_name = "${var.resourcegroup}"
Terraform 本身不支持模块之间的依赖关系,所以它不会工作。您可以将该代码移至更干净的 main 中,或者查看像这样的解决方法 Module dependencies。
另一种选择是在一个模块中创建所有网络内容,输出子网或 NIC ID。然后将这些作为变量传递到计算模块中?
希望这对您有所帮助
我对 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"
}
在网络目录中,我想为虚拟机创建网络接口。因此网络接口取决于子网 ID。虚拟机(我想在计算中使用模板创建)取决于网络接口 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 中使用 output/variable 以便配置网络接口?
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 创建的,因此该依赖项是否有效?!
在您的 main.tf 根级别添加:
module "network" {
source = "./network"
subnet_id = "{data.azurerm_subnet.agw.id}"
}
在您的网络模块中添加变量引用。另外一定要声明变量:
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 = "${var.subnet_id}"
private_ip_address_allocation = "dynamic"
}
depends_on = ["azurerm_resource_group.test"]
}
查看间接和直接依赖关系 dependencies。在您的代码中,您可以使用来创建这样的间接依赖项 resource_group_name = "${azurerm_resource_group.test.id}"
。那么你不需要显式定义它。
resource "azurerm_virtual_network" "test" {
name = "${var.vnet}"
location = "${var.location}"
resource_group_name = "${var.resourcegroup}"
Terraform 本身不支持模块之间的依赖关系,所以它不会工作。您可以将该代码移至更干净的 main 中,或者查看像这样的解决方法 Module dependencies。
另一种选择是在一个模块中创建所有网络内容,输出子网或 NIC ID。然后将这些作为变量传递到计算模块中?
希望这对您有所帮助