shell Terraform空提供程序的命令语法问题

eblbsuwk  于 2023-06-24  发布在  Shell
关注(0)|答案(1)|浏览(139)

目的:尝试在terraform中的Null提供程序资源定义中运行多个AZcli命令。尝试列出所有私有端点,然后循环查找状态为"pending"的所有私有端点,然后批准这些端点。
我的当前编码:

resource "null_resource" "endpoint_approval" {
  depends_on = [azurerm_synapse_managed_private_endpoint.mpe_adls_blob]

  provisioner "local-exec" {
    command = <<EOT
      pending_endpoints=$(az network private-endpoint-connection list --id "${var.syn_adls_id}" --query "[?properties.privateLinkServiceConnectionState.status=='Pending'].id" -o tsv)
      for each_endpoint in $pending_endpoints
      do
        az network private-endpoint-connection approve --id "$each_endpoint" --description "Approved in Terraform"
      done
    EOT
    interpreter = ["/bin/bash", "-c"]
  }
}

我得到的错误:

': exit status 2. Output: /bin/sh: syntax error: unexpected end of file (expecting "done")

我已经逐行验证了缩进,似乎没有问题,并且在执行之前还使用了terraform fmt命令对其进行格式化,但我不知道为什么会出现此错误。有人能给我指路吗?先谢谢你了

cpjpxq1n

cpjpxq1n1#

我尝试使用下面的代码来解决语法问题,并得到了没有任何错误的输出:

我的main.tf编码:-

我引用了这个official Terraform document中的代码,并用空资源块和我的变量修改了它。

terraform {
  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"
      version = "~>3.0"
    }
  }
}

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "example" {
  name     = "example-resources-siliconrg"
  location = "West Europe"
}

resource "azurerm_storage_account" "example" {
  name                     = "siliconstrgacc"
  resource_group_name      = azurerm_resource_group.example.name
  location                 = azurerm_resource_group.example.location
  account_tier             = "Standard"
  account_replication_type = "LRS"
  account_kind             = "StorageV2"
  is_hns_enabled           = "true"
  depends_on = [ azurerm_resource_group.example ]
}

resource "azurerm_storage_data_lake_gen2_filesystem" "example" {
  name               = "example-siliconadls2"
  storage_account_id = azurerm_storage_account.example.id
  depends_on = [ azurerm_storage_account.example ]
}

resource "azurerm_synapse_workspace" "example" {
  name                                 = "example-siliconsy32"
  resource_group_name                  = azurerm_resource_group.example.name
  location                             = azurerm_resource_group.example.location
  storage_data_lake_gen2_filesystem_id = azurerm_storage_data_lake_gen2_filesystem.example.id
  sql_administrator_login              = "enter_your_user"
  sql_administrator_login_password     = "enter_your_password"
  managed_virtual_network_enabled      = true
  depends_on = [ azurerm_storage_account.example ]

  identity {
    type = "SystemAssigned"
  }
}

resource "azurerm_synapse_firewall_rule" "example" {
  name                 = "AllowAll"
  synapse_workspace_id = azurerm_synapse_workspace.example.id
  start_ip_address     = "0.0.0.0"
  end_ip_address       = "255.255.255.255"
  depends_on = [ azurerm_synapse_workspace.example ]
}

resource "azurerm_storage_account" "example_connect" {
  name                     = "siliconstrg54"
  resource_group_name      = azurerm_resource_group.example.name
  location                 = azurerm_resource_group.example.location
  account_tier             = "Standard"
  account_replication_type = "LRS"
  account_kind             = "BlobStorage"
  depends_on = [ azurerm_synapse_workspace.example ]
}

resource "azurerm_synapse_managed_private_endpoint" "example" {
  name                 = "example-endpoint-silion32"
  synapse_workspace_id = azurerm_synapse_workspace.example.id
  target_resource_id   = azurerm_storage_account.example_connect.id
  subresource_name     = "blob"

  depends_on = [azurerm_synapse_firewall_rule.example]
}

resource "null_resource" "resourcecli" {
  provisioner "local-exec" {
    command = <<EOT
      $pending_endpoints = $(az network private-endpoint-connection list --id "${azurerm_storage_data_lake_gen2_filesystem.example.id}" --query "[?properties.privateLinkServiceConnectionState.status=='Pending'].id" -o tsv)
      foreach ($each_endpoint in $pending_endpoints) {
        az network private-endpoint-connection approve --id $each_endpoint --description "Approved in Terraform"
      }
    EOT
    interpreter = ["PowerShell", "-Command"]
  }
}

输出:-

https://i.imgur.com/xZFxvDv.png
https://i.imgur.com/dR7FFOq.png

相关问题