azure Terraform -将PowerShell脚本上传到创建的第一个容器中

djmepvbi  于 2023-03-31  发布在  Shell
关注(0)|答案(1)|浏览(70)

我需要将PowerShell脚本上传到创建的第一个存储帐户上的容器中。我在www.example.com上有var.tf:

locals {
 sta_ctn = {
   "exploitation" = { sta = azurerm_storage_account.terra_sta[0].name, access_type = "private" }
}

如果有一天我需要创建另一个容器,我使用了foreach。我在storage_account.tf中有这个:

resource "azurerm_storage_container" "terra_sta_ctn" {
  for_each              = local.sta_ctn

  name                  = each.key
  storage_account_name  = each.value.sta
  container_access_type = each.value.access_type
}

resource "azurerm_storage_blob" "terra_sta_ctn_ansible" {
  name                   = "ConfigureRemotingForAnsible.ps1"
  storage_account_name   = azurerm_storage_account.terra_sta[0].name
  storage_container_name = azurerm_storage_container.terra_sta_ctn[0].name
  type                   = "Block"
  source                 = "${path.root}/_scripts/ansible/ConfigureRemotingForAnsible.ps1"
}

错误是:

│ Error: Invalid index
│
│   on storage_account.tf line 26, in resource "azurerm_storage_blob" "terra_sta_ctn_ansible":
│   26:   storage_container_name = azurerm_storage_container.terra_sta_ctn[0].name
│     ├────────────────
│     │ azurerm_storage_container.terra_sta_ctn is object with 1 attribute "exploitation"
│
│ The given key does not identify an element in this collection value. An object only supports looking up attributes by name, not by numeric index.

“0”在这一行“26:storage_container_name = azurerm_storage_container.terra_sta_ctn[0].name”带下划线。但“exploitation”是我的容器的名称。需要的正是这个名称。
我还需要做什么?

ozxc1zmp

ozxc1zmp1#

由于您在terra_sta_ctn中使用for_each,因此您也应该在terra_sta_ctn_ansible中使用for_each

resource "azurerm_storage_blob" "terra_sta_ctn_ansible" {
 
  for_each = azurerm_storage_container.terra_sta_ctn

  name                   = "ConfigureRemotingForAnsible.ps1"
  storage_account_name   = azurerm_storage_account.terra_sta[0].name
  storage_container_name = each.value.name
  type                   = "Block"
  source                 = "${path.root}/_scripts/ansible/ConfigureRemotingForAnsible.ps1"
}

附言
你还没有展示terra_sta是什么,所以不可能推测azurerm_storage_account.terra_sta[0].name是否正确。

相关问题