使用 ARM 範本進行管理群組部署

隨著您的組織逐漸成熟,您可以部署 Azure Resource Manager 範本 (ARM 範本),以在管理群組層級建立資源。 例如,您可能需要為管理群組定義和指派原則Azure 角色型存取控制 (Azure RBAC)。 使用管理群組層級範本,您可以在管理群組層級以宣告方式套用原則及指派角色。

提示

我們建議使用 Bicep,因為其提供的功能與 ARM 範本相同,而且語法更易於使用。 若要深入了解,請參閱管理群組部署

支援的資源

並非所有的資源類型都可部署到管理群組層級。 此節將列出支援的資源類型。

針對 Azure 藍圖,請使用:

針對 Azure 原則,請使用:

針對存取控制,請使用:

針對部署至訂用帳戶或資源群組的巢狀範本,請使用:

針對管理您的資源,請使用:

管理群組是租用戶層級的資源。 不過,您可以將新管理群組的範圍設為租用戶,藉以在管理群組部署中建立管理群組。 請參閱管理群組 (部分機器翻譯)。

結構描述

您所用於管理群組部署的結構描述與用於資源群組部署的結構描述不同。

針對範本,請使用:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-08-01/managementGroupDeploymentTemplate.json#",
  ...
}

所有部署範圍的參數檔案結構描述都相同。 針對參數檔案,請使用:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
  ...
}

部署命令

若要部署至管理群組,請使用管理群組部署命令。

針對 Azure CLI,請使用 az deployment mg create (部分機器翻譯):

az deployment mg create \
  --name demoMGDeployment \
  --location WestUS \
  --management-group-id myMG \
  --template-uri "https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/management-level-deployment/azuredeploy.json"

有關用於部署 ARM 範本的部署命令和選項,如需詳細資訊,請參閱:

部署位置和名稱

針對管理群組層級部署,您必須提供部署的位置。 部署的位置與您部署的資源位置不同。 部署位置會指定部署資料的儲存位置。 訂用帳戶 (部分機器翻譯) 和租用戶 (部分機器翻譯) 部署也需要位置。 針對資源群組 (部分機器翻譯) 部署,資源群組的位置會用來儲存部署資料。

您可以提供部署的名稱,或使用預設的部署名稱。 預設名稱是範本檔案的名稱。 例如,部署名為 azuredeploy.json 的範本會建立預設的部署名稱 azuredeploy

對於每個部署名稱而言,此位置是不可變的。 當某個位置已經有名稱相同的現有部署時,您無法在其他位置建立部署。 例如,如果您在 centralus 中建立名為 deployment1 的管理群組部署,稍後即便您是在 westus 的位置建立另一個部署,該部署仍然無法使用 deployment1 作為名稱。 如果您收到錯誤代碼 InvalidDeploymentLocation,請使用不同的名稱或與先前該名稱部署相同的位置。

部署範圍

部署至管理群組時,您可以將資源部署至:

  • 作業的目標管理群組
  • 租用戶中的另一個管理群組
  • 管理群組中的訂用帳戶
  • 管理群組中的資源群組
  • 資源群組的租用戶

延伸模組資源的範圍可以設為與部署目標不同的目標。

部署範本的使用者必須能夠存取指定的範圍。

本節說明如何指定不同的範圍。 您可以將這些不同範圍結合在單一範本中。

將範圍設為目標管理群組

在範本的資源區段中定義的資源會以部署命令套用至管理群組。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-08-01/managementGroupDeploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "resources": [
    management-group-resources-default
  ],
  "outputs": {}
}

將範圍設為另一個管理群組

若要將另一個管理群組設為目標,請新增巢狀部署並指定 scope 屬性。 將 scope 屬性設為 Microsoft.Management/managementGroups/<mg-name> 格式的值。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-08-01/managementGroupDeploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "mgName": {
      "type": "string"
    }
  },
  "variables": {
    "mgId": "[format('Microsoft.Management/managementGroups/{0}', parameters('mgName'))]"
  },
  "resources": [
    {
      "type": "Microsoft.Resources/deployments",
      "apiVersion": "2022-09-01",
      "name": "nestedDeployment",
      "scope": "[variables('mgId')]",
      "location": "eastus",
      "properties": {
        "mode": "Incremental",
        "template": {
          management-group-resources-non-default
        }
      }
    }
  ],
  "outputs": {}
}

將範圍設為訂閱

您也可以將管理群組內的訂用帳戶設為目標。 部署範本的使用者必須有指定範圍的存取權。

若要將管理群組內的訂閱設為目標,請使用巢狀部署和 subscriptionId 屬性。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-08-01/managementGroupDeploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "resources": [
    {
      "type": "Microsoft.Resources/deployments",
      "apiVersion": "2022-09-01",
      "name": "nestedSub",
      "location": "westus2",
      "subscriptionId": "00000000-0000-0000-0000-000000000000",
      "properties": {
        "mode": "Incremental",
        "template": {
          "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
          "contentVersion": "1.0.0.0",
          "resources": [
            {
              subscription-resources
            }
          ]
        }
      }
    }
  ]
}

將範圍設為資源群組

您也可以將管理群組內的資源群組設為目標。 部署範本的使用者必須有指定範圍的存取權。

若要將管理群組內的資源群組設為目標,請使用巢狀部署。 設定 subscriptionIdresourceGroup 屬性。 請勿設定巢狀部署的位置,因其是部署在資源群組的位置。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-08-01/managementGroupDeploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "resources": [
    {
      "type": "Microsoft.Resources/deployments",
      "apiVersion": "2022-09-01",
      "name": "nestedRGDeploy",
      "subscriptionId": "00000000-0000-0000-0000-000000000000",
      "resourceGroup": "demoResourceGroup",
      "properties": {
        "mode": "Incremental",
        "template": {
          "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
          "contentVersion": "1.0.0.0",
          "resources": [
            {
              resource-group-resources
            }
          ]
        }
      }
    }
  ]
}

若要使用管理群組部署來建立訂閱內的資源群組,並將儲存體帳戶部署到該資源群組,請參閱部署至訂閱和資源群組

將範圍設為租用戶

若要在租用戶建立資源,請將 scope 設為 /。 部署範本的使用者必須擁有在租用戶部署的必要存取權

若要使用巢狀部署,請設定 scopelocation

{
  "$schema": "https://schema.management.azure.com/schemas/2019-08-01/managementGroupDeploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "resources": [
    {
      "type": "Microsoft.Resources/deployments",
      "apiVersion": "2022-09-01",
      "name": "nestedDeployment",
      "location": "centralus",
      "scope": "/",
      "properties": {
        "mode": "Incremental",
        "template": {
          tenant-resources
        }
      }
    }
  ],
  "outputs": {}
}

或者,您也可以為了某些資源類型將範圍設為 /,例如管理群組。 下一節將描述如何建立新的管理群組。

管理群組

若要在管理群組部署中建立管理群組,您必須將管理群組的範圍設為 /

下列範例會在根管理群組中建立新的管理群組。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-08-01/managementGroupDeploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "mgName": {
      "type": "string",
      "defaultValue": "[concat('mg-', uniqueString(newGuid()))]"
    }
  },
  "resources": [
    {
      "type": "Microsoft.Management/managementGroups",
      "apiVersion": "2021-04-01",
      "name": "[parameters('mgName')]",
      "scope": "/",
      "location": "eastus",
      "properties": {}
    }
  ],
  "outputs": {
    "output": {
      "type": "string",
      "value": "[parameters('mgName')]"
    }
  }
}

下一個範例會在指定為父代的管理群組中建立新的管理群組。 請注意,範圍是設為 /

{
  "$schema": "https://schema.management.azure.com/schemas/2019-08-01/managementGroupDeploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "mgName": {
      "type": "string",
      "defaultValue": "[concat('mg-', uniqueString(newGuid()))]"
    },
    "parentMG": {
      "type": "string"
    }
  },
  "resources": [
    {
      "name": "[parameters('mgName')]",
      "type": "Microsoft.Management/managementGroups",
      "apiVersion": "2021-04-01",
      "scope": "/",
      "location": "eastus",
      "properties": {
        "details": {
          "parent": {
            "id": "[tenantResourceId('Microsoft.Management/managementGroups', parameters('parentMG'))]"
          }
        }
      }
    }
  ],
  "outputs": {
    "output": {
      "type": "string",
      "value": "[parameters('mgName')]"
    }
  }
}

訂用帳戶

若要使用 ARM 範本在管理群組中建立新的 Azure 訂用帳戶,請參閱:

若要部署會將現有 Azure 訂用帳戶移至新管理群組的範本,請參閱在 ARM 範本中移動訂用帳戶 (部分機器翻譯)

Azure 原則

部署至管理群組的自訂原則定義是管理群組的延伸模組。 若要取得自訂原則定義的識別碼,請使用 extensionResourceId() (部分機器翻譯) 函式。 內建原則定義是租用戶層級的資源。 若要取得內建原則定義的識別碼,請使用 tenantResourceId() (部分機器翻譯) 函式。

下列範例顯示如何在管理群組層級定義原則,並加以指派。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-08-01/managementGroupDeploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "targetMG": {
      "type": "string",
      "metadata": {
        "description": "Target Management Group"
      }
    },
    "allowedLocations": {
      "type": "array",
      "defaultValue": [
        "australiaeast",
        "australiasoutheast",
        "australiacentral"
      ],
      "metadata": {
        "description": "An array of the allowed locations, all other locations will be denied by the created policy."
      }
    }
  },
  "variables": {
    "mgScope": "[tenantResourceId('Microsoft.Management/managementGroups', parameters('targetMG'))]",
    "policyDefinition": "LocationRestriction"
  },
  "resources": [
    {
      "type": "Microsoft.Authorization/policyDefinitions",
      "name": "[variables('policyDefinition')]",
      "apiVersion": "2020-09-01",
      "properties": {
        "policyType": "Custom",
        "mode": "All",
        "parameters": {
        },
        "policyRule": {
          "if": {
            "not": {
              "field": "location",
              "in": "[parameters('allowedLocations')]"
            }
          },
          "then": {
            "effect": "deny"
          }
        }
      }
    },
    {
      "type": "Microsoft.Authorization/policyAssignments",
      "name": "location-lock",
      "apiVersion": "2020-09-01",
      "dependsOn": [
        "[variables('policyDefinition')]"
      ],
      "properties": {
        "scope": "[variables('mgScope')]",
        "policyDefinitionId": "[extensionResourceId(variables('mgScope'), 'Microsoft.Authorization/policyDefinitions', variables('policyDefinition'))]"
      }
    }
  ]
}

部署至訂閱和資源群組

從管理群組層級的部署中,您可以將目標設為管理群組內的訂閱。 下列範例會在訂閱內建立資源群組,並將儲存體帳戶部署到該資源群組。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-08-01/managementGroupDeploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "nestedsubId": {
      "type": "string"
    },
    "nestedRG": {
      "type": "string"
    },
    "storageAccountName": {
      "type": "string"
    },
    "nestedLocation": {
      "type": "string"
    }
  },
  "resources": [
    {
      "type": "Microsoft.Resources/deployments",
      "apiVersion": "2021-04-01",
      "name": "nestedSub",
      "location": "[parameters('nestedLocation')]",
      "subscriptionId": "[parameters('nestedSubId')]",
      "properties": {
        "mode": "Incremental",
        "template": {
          "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
          "contentVersion": "1.0.0.0",
          "parameters": {
          },
          "variables": {
          },
          "resources": [
            {
              "type": "Microsoft.Resources/resourceGroups",
              "apiVersion": "2021-04-01",
              "name": "[parameters('nestedRG')]",
              "location": "[parameters('nestedLocation')]"
            }
          ]
        }
      }
    },
    {
      "type": "Microsoft.Resources/deployments",
      "apiVersion": "2021-04-01",
      "name": "nestedRG",
      "subscriptionId": "[parameters('nestedSubId')]",
      "resourceGroup": "[parameters('nestedRG')]",
      "dependsOn": [
        "nestedSub"
      ],
      "properties": {
        "mode": "Incremental",
        "template": {
          "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
          "contentVersion": "1.0.0.0",
          "resources": [
            {
              "type": "Microsoft.Storage/storageAccounts",
              "apiVersion": "2021-04-01",
              "name": "[parameters('storageAccountName')]",
              "location": "[parameters('nestedLocation')]",
              "kind": "StorageV2",
              "sku": {
                "name": "Standard_LRS"
              }
            }
          ]
        }
      }
    }
  ]
}

下一步