可以省略许多资源相同价值的重复声明

时间:2019-06-26 10:45:56

标签: terraform terraform-provider-azure

我必须在Terraform文件中多次重复相同的参数值。是否有可能声明一些变量,该变量将自动为当前文件的所有资源设置这些参数(如果有一个具有该名称的参数)?

这是我目前写的:

resource "azurerm_resource_group" "rg" {
  name     = "${var.resourceGroupName}"
  location = "${var.location}"
  tags     = "${var.tags}"
}

resource "azurerm_storage_account" "storage" {
  name                     = "myuniquestorageaccount"
  resource_group_name      = "${azurerm_resource_group.rg.name}"
  tags                     = "${azurerm_resource_group.rg.tags}"
  location                 = "${azurerm_resource_group.rg.location}"
  account_tier             = "Standard"
  account_kind             = "StorageV2"
  account_replication_type = "GRS"
}

resource "azurerm_storage_queue" "queue" {
  name                 = "events"
  storage_account_name = "${azurerm_storage_account.storage.name}"
  resource_group_name  = "${azurerm_resource_group.rg.name}"
}

resource "azurerm_storage_table" "sessions" {
  name                 = "sessions"
  storage_account_name = "${azurerm_storage_account.storage.name}"
  resource_group_name  = "${azurerm_resource_group.rg.name}"
}

resource "azurerm_storage_table" "streams" {
  name                 = "streams"
  storage_account_name = "${azurerm_storage_account.storage.name}"
  resource_group_name  = "${azurerm_resource_group.rg.name}"
}

请注意必须多次应用resource_group_nametagslocation,并且始终使用相同的值。

我想这样写代码:

autoparameter {
  resource_group_name  = "${azurerm_resource_group.rg.name}"
  storage_account_name = "${azurerm_storage_account.storage.name}"
  tags                 = "${var.tags}"
  location             = "${var.location}"
}


resource "azurerm_resource_group" "rg" {
  name = "${var.resourceGroupName}"
}

resource "azurerm_storage_account" "storage" {
  name                     = "myuniquestorageaccount"
  account_tier             = "Standard"
  account_kind             = "StorageV2"
  account_replication_type = "GRS"
}

resource "azurerm_storage_queue" "queue" {
  name = "events"
}

resource "azurerm_storage_table" "sessions" {
  name = "sessions"
}

resource "azurerm_storage_table" "streams" {
  name = "streams"
}

0 个答案:

没有答案