环境文件夹设置:
rg.tf
文件,用于在Azure上创建资源组。vnet.tf
文件,它需要模块A中的资源组名称。如何将一个输出转换为另一个输出?
Terraform -v = Terraform v0.12.6
文件夹结构:
C:\Terraform\ResourceGroup
\rg.tf
\var.tf
\output.tf
C:\Terraform\Vnet
\vnet.tf
rg.tf
包含:
provider "azurerm" {
Subscription ID : xxxxxxxxxxxxxx
Subscription Name :xxxxxxxxxxxxx
Client ID : xxxxxxxxxxxxxxxxx
Client Secret: xxxxxxxxxxxx
}
terraform {
backend "azurerm" {
storage_account_name = xxxxxxxxxxxxx
resource_group_name = xxxxxxxxxxxxx
container_name = "versiontf"
key = "terraform.tfstate"
}
}
resource "azurerm_resource_group" "res_group" {
location = "${var.location}"
name = "${var.name}"
}
var.tf
包含
variable "location" {
default = "West US"
}
variable "name" {
default = "testing"
}
output.tf
包含:
output "rg_name" {
value = "${azurerm_resource_group.res_group.name}"
}
vnet.tf
包含:
provider "azurerm" {
Subscription ID : xxxxxxxxxxxxxx
Subscription Name :xxxxxxxxxxxxx
Client ID : xxxxxxxxxxxxxxxxx
Client Secret: xxxxxxxxxxxx
}
resource "azurerm_virtual_network" "test" {
name = "vnet"
location = "east us"
resource_group_name = "????????????" (How do i read the resource group name which i created using rg.tf)
address_space = ["10.0.0.0/16"]
}
仅供参考:我能够创建输出值并将其添加到后端状态文件。
答案 0 :(得分:0)
您只需要使用文件vnet.tf
中的模块,如下所示:
provider "azurerm" {
Subscription ID : xxxxxxxxxxxxxx
Subscription Name :xxxxxxxxxxxxx
Client ID : xxxxxxxxxxxxxxxxx
Client Secret: xxxxxxxxxxxx
}
module "resourceGroup" {
source = "../ResourceGroup"
# the variable that you set for your resource group
name = "rg_name"
location = "rg_location"
}
resource "azurerm_virtual_network" "test" {
name = "vnet"
location = "east us"
# use the module resource group
resource_group_name = "${module.resourceGroup.rg_name}"
address_space = ["10.0.0.0/16"]
}