我是terraform的新手,所以我将尽我所能尝试进行解释。 Terraform不会从状态文件中读取变量/输出,而不会在另一个文件中使用该值。
我尝试在互联网上搜索所有我能找到的东西,看看是否有人遇到此问题以及如何解决。
###vnet.tf
#Remote State pulling data from bastion resource group state
data "terraform_remote_state" "network" {
backend = "azurerm"
config = {
storage_account_name = "terraformstatetracking"
container_name = "bastionresourcegroups"
key = "terraform.terraformstate"
}
}
#creating virtual network and putting that network in resource group created by bastion.tf file
module "quannetwork" {
source = "Azure/network/azurerm"
resource_group_name = "data.terraform_remote_state.network.outputs.quan_netwk"
location = "centralus"
vnet_name = "quan"
address_space = "10.0.0.0/16"
subnet_prefixes = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
subnet_names = ["subnet1", "subnet2", "subnet3"]
tags = {
environment = "quan"
costcenter = "it"
}
}
terraform {
backend "azurerm" {
storage_account_name = "terraformstatetracking"
container_name = "quannetwork"
key = "terraform.terraformstate"
}
}
###resourcegroups.tf
# Create a resource group
#Bastion
resource "azurerm_resource_group" "cm" {
name = "${var.prefix}cm.RG"
location = "${var.location}"
tags = "${var.tags}"
}
#Bastion1
resource "azurerm_resource_group" "network" {
name = "${var.prefix}network.RG"
location = "${var.location}"
tags = "${var.tags}"
}
#bastion2
resource "azurerm_resource_group" "storage" {
name = "${var.prefix}storage.RG"
location = "${var.location}"
tags = "${var.tags}"
}
terraform {
backend "azurerm" {
storage_account_name = "terraformstatetracking"
container_name = "bastionresourcegroups"
key = "terraform.terraformstate"
}
}
###outputs.tf
output "quan_netwk" {
description = "Quan Network Resource Group"
value = "${azurerm_resource_group.network.id}"
}
运行vnet.tf
代码时,它应该从存储在azure后端存储帐户状态文件文件中的outputs.tf
的输出中读取,并将该值用于{ {1}}模块。而是创建一个名为resource_group_name
的资源组。任何帮助将不胜感激。
答案 0 :(得分:0)
首先,您需要在模块resource_group_name
中为quannetwork
输入一个字符串,而不是资源组ID。
第二,如果要在远程状态下引用某些内容,则不要仅将其放在双引号中,正确的格式如下:
resource_group_name = "${data.terraform_remote_state.network.outputs.quan_netwk}"