Azure ARM 角色分配不同的资源组

标签 azure azure-rm-template rbac

我正在尝试创建一个具有虚拟机的 ARM 模板,我希望该虚拟机能够将 AcrPull 角色分配给位于不同资源组中的容器注册表。我将范围属性设置为 ACR 的 ID(我从 https://resources.azure.com 获得此值)。

{
      "apiVersion": "2017-09-01",
      "type": "Microsoft.Authorization/roleAssignments",
      "name": "[guid(resourceGroup().id, 'stuff')]",
      "properties": {
        "roleDefinitionId": "[variables(parameters('builtInRoleType'))]",
        "principalId": "[reference(variables('vmResourceId'), '2017-12-01', 'Full').identity.principalId]",
        "scope": "[concat(/subscriptions/abc123-x123-4bef-84ec-1716390f231b/resourceGroups/someresroucegroupname/providers/Microsoft.ContainerRegistry/registries/someacrname"   
      },
      "dependsOn": [
        "[concat('Microsoft.Compute/virtualMachines/', variables('vmName'))]"
      ]
    }

我不断收到错误:

"The request to create role assignment '694829aa-cee5-4fe3-bf94-3ae372c14f26' is not valid. Role 
assignment scope '/subscriptions/abc123-x123-4bef-84ec-1716390f231b' must match the scope specified on
the URI '/subscriptions/abc123-x123-4bef-84ec-1716390f231b/resourcegroups/myresourcegroupname'.

这可能吗?

最佳答案

您需要使用嵌套模板调用来包装它,如下所示:

{
    "type": "Microsoft.Resources/deployments",
    "name": "nested-role-assignment",
    "apiVersion": "2017-05-10",
    "resourceGroup": "your_resource_group",
    "subscriptionId": "your_subscription_id",
    "dependsOn": [
        "[concat('Microsoft.Compute/virtualMachines/', variables('vmName'))]"
    ],
    "properties": {
        "mode": "Incremental",
        "template": {
            "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
            "contentVersion": "1.0.0.0",
            "resources": [
                {
                    "apiVersion": "2017-09-01",
                    "type": "Microsoft.Authorization/roleAssignments",
                    "name": "[guid(resourceGroup().id, 'stuff')]",
                    "properties": {
                        "roleDefinitionId": "[variables(parameters('builtInRoleType'))]",
                        "principalId": "[reference(variables('vmResourceId'), '2017-12-01', 'Full').identity.principalId]",
                        "scope": "[concat(/subscriptions/abc123-x123-4bef-84ec-1716390f231b/resourceGroups/someresroucegroupname/providers/Microsoft.ContainerRegistry/registries/someacrname"
                    }
                }
            ]
        }
    }
}

因此,有效地将其直接部署到 ACR 所在的资源组。

ps。我的名字\类型组合:

"type": "Microsoft.ContainerRegistry/registries/providers/roleAssignments",
"name": "[concat('ACR_NAME_GOES_HERE/Microsoft.Authorization/', guid(resourceGroup().id, 'acr'))]",

关于Azure ARM 角色分配不同的资源组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59361077/

相关文章:

sql-server - 如何从本地计算机连接 Azure Sql Server 2012 数据库

image - Azure 存储 - 图像 url 变成二进制?

azure - 逻辑应用程序 : Use Dynamic content within the Logical functions

windows - 将参数传递到 Azure ARM 模板

Azure 部署槽 RBAC

python - 使用 %sql 语句设置 databricks python 变量

azure - CMS 在 Azure 表存储之上运行?

Azure 自动化帐户和 Runbooks ARM 模板,uri 问题?

c# - 如何向 Azure C# SecretClient 提供凭据 (DefaultAzureCredential)?它似乎忽略凭据和 RBAC 并抛出 403

c# - 基于角色的访问控制 (RBAC) - .Net 组件