我有 2 个 Azure 订阅。 Subscription_1 用于创建 VNET/SUBNET。 Subscription_2 用于在该 VNET 内创建虚拟机。
为了进行部署,我创建了 2 个服务主体。 SPN_1 将部署 VNET/SUBNET。 SPN_2 将部署虚拟机。
az ad sp create-for-rbac --name SP_1 --role contributor --scopes /subscriptions/mySubscriptionID_of_Subscription_1
az ad sp create-for-rbac --name SP_2 --role contributor --scopes /subscriptions/mySubscriptionID_of_Subscription_2
但是,对于 SPN_2 部署虚拟机,我需要 SP_2 对 Subscription_1 具有“网络贡献者”RBAC。
我查看了文档,它非常差并且没有给出更新的示例:https://learn.microsoft.com/en-us/cli/azure/ad/sp?view=azure-cli-latest#az-ad-sp-update
如何更新 SP_1 才能执行此操作?有哪些 CLI 可以实现此目的?
最佳答案
您不需要创建多个服务主体:
- 使用
az ad sp create-for-rbac
创建一个服务主体。 - 使用
az role assignment create
分配角色。
这是一个 powershell 示例:
# Create service principal
$spCredentials = az ad sp create-for-rbac --name SP_1 | ConvertFrom-Json
# Get SP details
$spDetails = az ad sp show --id $spCredentials.appId | ConvertFrom-Json
# Create contributor role assignment for subscription1
az role assignment create --assignee-principal-type ServicePrincipal --assignee-object-id $spDetails.id --role "contributor" --scope /subscriptions/mySubscriptionID_of_Subscription_1
# Create network contributor role assignment for subscription1
az role assignment create --assignee-principal-type ServicePrincipal --assignee-object-id $spDetails.id --role "network contributor" --scope /subscriptions/mySubscriptionID_of_Subscription_1
# Create contributor role assignment for subscription2
az role assignment create --assignee-principal-type ServicePrincipal --assignee-object-id $spDetails.id --role "contributor"--scope /subscriptions/mySubscriptionID_of_Subscription_2
关于服务主体的 Azure 多订阅 RBAC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76431019/