运行 使用 Terraform 的 Azure AZ 登录命令
Running Azure AZ login command with Terraform
问题:
我正在尝试在 Terraform 中执行 bash 脚本并抛出错误
环境:我是 运行 Terraform 在 VScode(终端是 bash)在 Windows 10。
我还在标准 git bash 命令终端中尝试了 运行 并且它抛出相同的错误。
我也尝试用 '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
}
}
在 运行 'terraform plan'
之后抛出错误
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 中使用 Windows 子系统用于 Linux (WSL)。根据您的推荐,无需将 ARM_CLIENT_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"
您可以阅读 Configuring the Service Principal in Terraform 了解更多详情。
但是这样环境变量在当前session中是暂时有效的。如果你想永久存储它的值,你可以在 Windows 和 WSL 之间使用共享 WSLENV
环境变量。从 17063 开始,开始支持 WSLENV
。 WSLENV 区分大小写。
例如,
首先,你可以在Windows10,
中设置环境变量
其次,在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环境变量。
最后,你应该 运行 terraform plan
在 VScode 的 WSL 中没有这样的错误。
更多信息,您可以参考以下文档,
问题: 我正在尝试在 Terraform 中执行 bash 脚本并抛出错误
环境:我是 运行 Terraform 在 VScode(终端是 bash)在 Windows 10。 我还在标准 git bash 命令终端中尝试了 运行 并且它抛出相同的错误。 我也尝试用 '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
}
}
在 运行 'terraform plan'
之后抛出错误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 中使用 Windows 子系统用于 Linux (WSL)。根据您的推荐,无需将 ARM_CLIENT_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"
您可以阅读 Configuring the Service Principal in Terraform 了解更多详情。
但是这样环境变量在当前session中是暂时有效的。如果你想永久存储它的值,你可以在 Windows 和 WSL 之间使用共享 WSLENV
环境变量。从 17063 开始,开始支持 WSLENV
。 WSLENV 区分大小写。
例如,
首先,你可以在Windows10,
中设置环境变量其次,在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环境变量。
最后,你应该 运行 terraform plan
在 VScode 的 WSL 中没有这样的错误。
更多信息,您可以参考以下文档,