You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何查询通过azurerm_role_assignment分配的Microsoft Entra ID组角色

列出Microsoft Entra ID组通过azurerm_role_assignment分配的角色

一、通过Azure门户UI查看

  • 登录Azure门户,进入目标订阅或资源组
  • 左侧导航栏选择「访问控制(IAM)」
  • 切换到「角色分配」标签页
  • 在筛选条件中,选择「主体类型」为「组」,输入目标组名称进行筛选
  • 结果列表将显示该组被分配的所有角色,包括通过azurerm_role_assignment创建的条目

二、通过Azure CLI(Bash命令)查询

查询特定组的所有角色分配

先获取组的对象ID(已知ID可跳过此步):

az ad group show --group "users-group" --query id -o tsv

使用组对象ID查询角色分配:

az role assignment list --assignee <组对象ID> --all --query "[].{角色:roleDefinitionName, 作用域:scope, 分配名称:name}" -o table

筛选Terraform创建的组角色分配

如果Terraform部署时给azurerm_role_assignment添加了描述或标签(比如description = "Created by Terraform"),可以精准筛选:

az role assignment list --all --query "[?principalType=='Group' && contains(description, 'Created by Terraform')].{组名:principalName, 角色:roleDefinitionName, 作用域:scope}" -o table

三、通过Terraform代码/状态查询

查看本地Terraform状态文件

直接打开本地的terraform.tfstate文件,搜索azurerm_role_assignment资源,即可查看分配的角色、主体ID、作用域等信息。

添加Terraform输出块展示

在现有Terraform代码中添加输出配置,部署后直接查看:

output "group_role_assignments" {
  value = [
    for ra in azurerm_role_assignment : {
      组名        = data.azuread_group.example.display_name
      角色        = ra.role_definition_name
      作用域      = ra.scope
      分配ID      = ra.id
    }
  ]
}

执行terraform apply后,运行terraform output group_role_assignments即可获取结果。

用Terraform数据源查询现有分配

添加数据源查询指定组的角色分配:

data "azurerm_role_assignments" "group_assignments" {
  scope                = "/subscriptions/<订阅ID>"
  principal_id         = data.azuread_group.example.id
  role_definition_name = "Reader" # 可选,指定角色名称缩小范围
}

output "existing_group_roles" {
  value = data.azurerm_role_assignments.group_assignments.assignments
}

执行terraform refresh后,通过terraform output existing_group_roles查看结果。

内容的提问来源于stack exchange,提问作者Panda

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 11:22:41