导入 Azure Active Directory 组到 Terraform

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

Importing Azure Active Directory groups on Terraform

问题

使用Terraform的azuread提供程序,我试图创建组,读取类似于此的CSV文件:

display_name
Group1
Group2
Group3

将其读取到本地变量中:

locals {
  departments      = csvdecode(file("${path.module}/aad_departments.csv"))
}

创建组:

resource "azuread_group" "groups" {
  for_each = { for group in local.departments : group.display_name => group }
  display_name = each.value.display_name
  prevent_duplicate_names = true
}

但我想导入一个已经存在的组,"Group2"。我已经使用了这个命令:

terraform import azuread_group.groups xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

但当我计划并应用此Terraform脚本时,它会抛出一个错误,说组已经存在:

"要通过Terraform进行管理,需要将此资源导入状态。请查看"azuread_group"的资源文档以获取更多信息。"

我该如何导入它?

非常感谢。

英文:

Using the azuread provider from Terraform, I am trying to create groups reading a CSV file like this:

display_name
Group1
Group2
Group3

Reading it in a local variable:

locals {
  departments      = csvdecode(file("${path.module}/aad_departments.csv"))
}
# Create groups
resource "azuread_group" "groups" {
  for_each = { for group in local.departments : group.display_name => group }
  display_name = each.value.display_name
  prevent_duplicate_names = true
}

But I would like to import an existing group, "Group2", that already exists. I have used this command:

terraform import azuread_group.groups xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

But when I plan and apply this terraform script it throws an error saying that the group already exist:

"To be managed via Terraform, this resource needs to be imported into the State. Please see the resource documentation for "azuread_group" for more information."

How can I import it?

Thank you very much,

答案1

得分: 0

因为您正在使用for_each元参数进行循环。

我期望 { for group in local.departments : group.display_name => group } 会得到以下结构。

{
  "group1" = {
    "display_name" = "group1"
  }
  "group2" = {
    "display_name" = "group2"
  }
  "group3" = {
    "display_name" = "group3"
  }
}

您还需要根据需求在您的terraform代码中添加security_enabledmail_enabled

参考错误信息:"security_enabled": 必须指定其中一个 'mail_enabled,security_enabled'

resource "azuread_group" "groups" {
  for_each = { for group in local.departments : group.display_name => group }

  display_name            = each.value.display_name
  prevent_duplicate_names = true
  security_enabled        = true
  #### OR #####
  # mail_enabled            = true
}

最后,您需要使用以下三个命令将这 3 个群组导入您的状态文件。

terraform import 'azuread_group.groups["group1"]' "<object_id>" # 导入 group1
terraform import 'azuread_group.groups["group2"]' "<object_id>" # 导入 group2
terraform import 'azuread_group.groups["group3"]' "<object_id>" # 导入 group3

为了更安全,您可以使用 terraform console 验证{ for group in local.departments : group.display_name => group }的构造值,并相应调整命令/代码。

希望对您有所帮助。

英文:

because you are using a for_each meta-argument to loop.

I am expecting { for group in local.departments : group.display_name =&gt; group } would result in the below construct.

{
  &quot;group1&quot; = {
    &quot;display_name&quot; = &quot;group1&quot;
  }
  &quot;group2&quot; = {
    &quot;display_name&quot; = &quot;group2&quot;
  }
  &quot;group3&quot; = {
    &quot;display_name&quot; = &quot;group3&quot;
  }
}

You also need to add either security_enabled or mail_enabled in your terraform code as per your requirements.

reference error message : &quot;security_enabled&quot;: one ofmail_enabled,security_enabled must be specified

resource &quot;azuread_group&quot; &quot;groups&quot; {
  for_each = { for group in local.departments : group.display_name =&gt; group }

  display_name            = each.value.display_name
  prevent_duplicate_names = true
  security_enabled        = true
   #### OR #####
  # mail_enabled            = true
}

Finally, you have to use the below three commands to import the 3 groups in your state file.

terraform import &#39;azuread_group.groups[&quot;group1&quot;]&#39; &quot;&lt;object_id&gt;&quot; # to import group1
terraform import &#39;azuread_group.groups[&quot;group2&quot;]&#39; &quot;&lt;object_id&gt;&quot; # to import group2
terraform import &#39;azuread_group.groups[&quot;group3&quot;]&#39; &quot;&lt;object_id&gt;&quot; # to import group3

Just for the safer side, you can use terraform console and verify the value of construct out of { for group in local.departments : group.display_name =&gt; group } and adapt the commands/code accordingly.

Hope it helped.

huangapple
  • 本文由 发表于 2023年2月14日 01:08:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75439062.html
匿名

发表评论

匿名网友

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

确定