Terraform null provider存在命令语法问题。

huangapple go评论65阅读模式
英文:

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 &quot;pending&quot; and then approve those.

My current code:

  resource &quot;null_resource&quot; &quot;endpoint_approval&quot; {
  depends_on = [azurerm_synapse_managed_private_endpoint.mpe_adls_blob]

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

Error I get:

 &#39;: exit status 2. Output: /bin/sh: syntax error: unexpected end of file (expecting &quot;done&quot;)

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 = &quot;hashicorp/azurerm&quot;
      version = &quot;~&gt;3.0&quot;
    }
  }
}

provider &quot;azurerm&quot; {
  features {}
}

resource &quot;azurerm_resource_group&quot; &quot;example&quot; {
  name     = &quot;example-resources-siliconrg&quot;
  location = &quot;West Europe&quot;
}

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

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

resource &quot;azurerm_synapse_workspace&quot; &quot;example&quot; {
  name                                 = &quot;example-siliconsy32&quot;
  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              = &quot;enter_your_user&quot;
  sql_administrator_login_password     = &quot;enter_your_password&quot;
  managed_virtual_network_enabled      = true
  depends_on = [ azurerm_storage_account.example ]

  identity {
    type = &quot;SystemAssigned&quot;
  }
}

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

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

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

  depends_on = [azurerm_synapse_firewall_rule.example]
}

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

Output:-

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

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

huangapple
  • 本文由 发表于 2023年6月8日 23:23:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76433408.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定