我正在学习本教程: https://developer.hashicorp.com/terraform/tutorials/it-saas/entra-id
我的main.tf如下:
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0
# Configure the Azure Active Directory Provider
provider "azuread" {}
# Retrieve domain information
data "azuread_domains" "default" {
only_initial = true
}
locals {
domain_name = data.azuread_domains.default.domains.0.domain_name
users = csvdecode(file("${path.module}/users.csv"))
}
resource "random_pet" "suffix" {
length = 2
}
# Create users
resource "azuread_user" "users" {
for_each = { for user in local.users : user.first_name => user }
user_principal_name = format(
"%s%s-%s@%s",
substr(lower(each.value.first_name), 0, 1),
lower(each.value.last_name),
random_pet.suffix.id,
local.domain_name
)
password = format(
"%s%s%s!",
lower(each.value.last_name),
substr(lower(each.value.first_name), 0, 1),
length(each.value.first_name)
)
force_password_change = true
display_name = "${each.value.first_name} ${each.value.last_name}"
department = each.value.department
job_title = each.value.job_title
}
我能够完成“terraform apply”命令之前的所有步骤,但出现错误:
data.azuread_domains.default: Reading...
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create
Terraform planned the following actions, but then encountered a problem:
# random_pet.suffix will be created
+ resource "random_pet" "suffix" {
+ id = (known after apply)
+ length = 2
+ separator = "-"
}
Plan: 1 to add, 0 to change, 0 to destroy.
╷
│ Error: Could not list domains
│
│ with data.azuread_domains.default,
│ on main.tf line 8, in data "azuread_domains" "default":
│ 8: data "azuread_domains" "default" {
│
│ DomainsClient.BaseClient.Get(): unexpected status 403 with OData error: Authorization_RequestDenied: Insufficient privileges to complete the operation.
故障排除步骤:
az login
az login --service-principle
.profile
中,重新加载 Linux 配置文件并再次运行 terraform apply
。请告知错误原因以及如何解决。
问题的原因是我之前以与此处描述类似的方式配置了 ARM 环境变量: https://developer.hashicorp.com/terraform/tutorials/azure-get-started/azure-build
我的猜测是这些变量与
az login
命令加载的其他一些环境变量不兼容。
一旦我从 .profile 中删除了所有 ARM 环境变量,
terraform apply
命令就成功了。
您觉得这个解决方案怎么样?欢迎评论。