[英]Running Azure AZ login command with Terraform
問題:我正在嘗試在 Terraform 中執行 bash 腳本並引發錯誤
Environment: I am running Terraform in VScode (terminal is bash) on Windows 10. I've also tried running in standard git bash command terminal and it throws same error. 我也嘗試用 'program = ["az",' 替換 'program = ["bash",' 但仍然拋出錯誤。
我的 bash 腳本
#!/bin/bash
# Exit if any of the intermediate steps fail
set -e
# Login
az login --service-principal -u "${ARM_CLIENT_ID}" -p "${ARM_CLIENT_SECRET}" --tenant "${ARM_TENANT_ID}" >/dev/null
# Extract the query json into variables
eval "$(jq -r '@sh "SUBSCRIPTION_NAME=\(.subscription_name)"')"
# Get the subscription id and pass back map
az account list --query "[?name == '${SUBSCRIPTION_NAME}'].id | {id: join(', ', @)}" --output json
我的 main.tf 文件
locals {
access_levels = ["read", "write"]
subscription_name = lower(var.subscription_name)
}
# lookup existing subscription
data "azurerm_subscription" "current" {}
# Lookup Subscription
data "external" "lookupByName" {
# Looks up a subscription by its display name and returns id
program = ["bash", "${path.module}/scripts/lookupByName.sh"]
query = {
subscription_name = local.subscription_name
}
}
運行“地形計划”后拋出錯誤
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.
data.external.lookupByName: Refreshing state...
data.azurerm_subscription.current: Refreshing state...
Error: failed to execute "bash": usage: az login [-h] [--verbose] [--debug] [--only-show-errors]
[--output {json,jsonc,yaml,yamlc,table,tsv,none}]
[--query JMESPATH] [--username USERNAME] [--password PASSWORD]
[--service-principal] [--tenant TENANT]
[--allow-no-subscriptions] [-i] [--use-device-code]
[--use-cert-sn-issuer]
az login: error: Expecting value: line 1 column 1 (char 0)
on main.tf line 10, in data "external" "lookupByName":
10: data "external" "lookupByName" {
我想您在 Windows 10 中使用 Linux (WSL) 的 Windows 子系統。根據您的推薦,無需硬編碼 WSL 之類的變量,您可以將憑證作為 Environment_SECRET 作為變量存儲在 WSL 中:
$ export ARM_CLIENT_ID="00000000-0000-0000-0000-000000000000"
$ export ARM_CLIENT_SECRET="00000000-0000-0000-0000-000000000000"
$ export ARM_TENANT_ID="00000000-0000-0000-0000-000000000000"
您可以閱讀Terraform 中的配置服務主體了解更多詳細信息。
但是通過這種方式,環境變量在當前的session中是暫時有效的。 如果要永久存儲其值,可以在 Windows 和 WSL 之間使用共享WSLENV
環境變量。 從 17063 年開始,開始支持WSLENV
。 WSLENV 區分大小寫。
例如,
首先,您可以在 Windows 10 中設置環境變量,
其次,在 CMD 中設置WSLENV
變量。
C:\WINDOWS\system32>setx WSLENV ARM_TENANT_ID/u:ARM_CLIENT_ID/u:ARM_CLIENT_SECRET/u
SUCCESS: Specified value was saved.
第三,重新啟動你的 VS 代碼,你可以使用export
檢查當前的 WSL 環境變量。
最后,您應該在 VScode 的 WSL 中運行terraform plan
而不會出現此類錯誤。
有關更多信息,您可以參考以下文檔,
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.