Terraform Null Provider中AZcli命令语法问题求助
Terraform local-exec 执行AZ CLI脚本报错:unexpected end of file (expecting "done")
问题原因
报错核心是here-doc的结束标记EOT被缩进,bash将其视为脚本内容的一部分,导致循环的done没有对应闭合标记,触发语法错误。另外原脚本的for循环处理TSV输出存在隐患(端点ID含空格时会被错误拆分)。
修正后的代码
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) while IFS=$'\t' read -r each_endpoint; do if [ -n "$each_endpoint" ]; then az network private-endpoint-connection approve --id "$each_endpoint" --description "Approved in Terraform" fi done <<< "$pending_endpoints" EOT interpreter = ["/bin/bash", "-c"] } }
关键修复点
- here-doc结束标记顶格:
EOT必须无任何前置缩进,让bash正确识别为脚本结束标记,避免截断错误。 - 改用
while read处理输出:通过IFS=$'\t'指定按制表符分割TSV内容,-r防止反斜杠被转义,[ -n "$each_endpoint" ]过滤空行(无Pending端点时避免无效执行)。 - 统一换行符格式:确保文件使用LF(Unix格式)换行符,Windows环境下需调整编辑器设置,避免bash解析CRLF换行符时出错。
内容的提问来源于stack exchange,提问作者asp
相关产品推荐
相关产品推荐

