├── _modules
│ ├── az-network
│ │ ├── main.tf
│ │ ├── output.tf
│ │ └── vars.tf
│ └── resource-group
│ ├── main.tf
│ ├── output.tf
│ └── vars.tf
├── builds
│ └── rg-and-vnet
│ ├── graph.svg
│ ├── main.tf
│ ├── output.tf
│ ├── terraform.tfstate
│ └── terraform.tfstate.backup
└── helper-scripts
我很难理解如何在代码的其他部分使用模块输出。我定义了一组模块,正在尝试在通用的构建模板中调用它们。我似乎无法弄清楚如何正确输出然后将值输入到代码的其他部分。
简单的示例如下,我有两个模块,第二个需要上一个模块的输入。
文件夹:/nc-automation/azure/builds/rg-and-vnet/main.tf
provider "azurerm" {}
module "basic-rg" {
source = "nc-automation/azure/_modules/resource-group"
resource-group-name = "azure-poc"
resource-group-location = "centralus"
}
module "basic-vnet" {
source = "nc-automation/azure/_modules/az-network"
address-space = "172.16.0.0/24"
subnet-prefixes = ["172.16.0.0/29", "172.16.0.16/29"]
subnet-names = ["fw-mgmt-net", "fw-untrust-net"]
resource-group-name = "${module.basic-rg.resource_group_name}"
location = "${module.basic-rg.resource-group-location}"
}
我尝试调用所示的basic-rg模块的输出,以及许多其他方法。我似乎无法弄清楚将输出发送到我的文件以及在第二个模块中引用它们的正确方法是什么。使用terraform_remote_state可以做到这一点,但我希望是使代码更加不变。任何帮助表示赞赏。
文件夹:/ nc-automation / azure / _modules / resource-group
provider "azurerm" {}
### Resources to create ###
resource "azurerm_resource_group" "azure-resource-group" {
location = "${var.resource-group-location}"
name = "${var.resource-group-name}"
}
### Variables ###
variable "resource-group-name" {
description = "Name of resource group to be created"
}
variable "resource-group-location" {
description = "Location of where resource group should be created"
}
我假设由于我从/ nc-automation / azure / builds / rg-and-vnet /位置运行terraform命令,因此我希望将output.tf文件放在同一位置。我注意到的另一件有趣的事情是,当我像往常一样去引用输出时,在运行计划时会收到错误消息。
文件夹:/nc-automation/azure/builds/rg-and-vnet/output.tf
output "rg-name" {
value = "${azurerm_resource_group.azure-resource-group.name}"
}
output "rg-location" {
value = "${azurerm_resource_group.azure-resource-group.location}"
}
计划错误:
Error: output 'rg-name': unknown resource 'azurerm_resource_group.azure-resource-group' referenced in variable azurerm_resource_group.azure-resource-group.name
Error: output 'rg-location': unknown resource 'azurerm_resource_group.azure-resource-group' referenced in variable azurerm_resource_group.azure-resource-group.location