英文:
Command syntax issue with Terraform null provider
问题
目标: 尝试在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
命令进行了格式化,但我不知道为什么会收到此错误。有人能指导我吗?提前感谢。
英文:
Objective: Trying to run multiple AZcli commands within Null provider resource definition in terraform. Trying to list all private endpoints, then loop through it for finding all private endpoints which has status of "pending"
and then approve those.
My current code:
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"]
}
}
Error I get:
': exit status 2. Output: /bin/sh: syntax error: unexpected end of file (expecting "done")
I have verified indents line by line, seems to be fine, and also used terraform fmt
command to format it before I execute, but I am clueless why I am getting this error. Can someone guide me ? Thanks in advance
答案1
得分: 1
以下是您提供的内容的中文翻译:
我尝试通过使用以下代码解决了语法问题,并且得到了无错误的输出:
我的main.tf代码:
我参考了来自这个官方Terraform文档的代码,并使用null资源块和我的变量进行了修改。
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
英文:
I tried resolving the Syntax issue by using the code below and got the output without any errors:-
My main.tf code:-
I have referred the code from this official Terraform document and modified it with null resource block and my variables
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"]
}
}
Output:-
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论