ARM テンプレート用のスコープ関数

Resource Manager では、Azure Resource Manager テンプレート (ARM テンプレート) でデプロイ スコープの値を取得するために、次の関数が提供されています。

パラメーター、変数、現在のデプロイから値を取得する方法については、「 デプロイの値関数」を参照してください。

ヒント

ARM テンプレートと同じ機能を備え、構文も使いやすいため、Bicep をお勧めします。 詳細については、scope 関数に関する記事をご覧ください。

managementGroup

managementGroup()

現在のデプロイの管理グループからプロパティを持つオブジェクトを返します。

Bicep では、managementGroup スコープ関数を用します。

解説

managementGroup() は、管理グループのデプロイ上でのみ使用できます。 これは、デプロイ操作の現在の管理グループを返します。 現在の管理グループのプロパティを取得するために使用します。

戻り値

現在の管理グループのプロパティを持つオブジェクト。

管理グループの例

次の例では、現在の管理グループのプロパティが返されます。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-08-01/managementGroupDeploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "variables": {
    "mgInfo": "[managementGroup()]"
  },
  "resources": [],
  "outputs": {
    "mgResult": {
      "type": "object",
      "value": "[variables('mgInfo')]"
    }
  }
}

戻り値は次のとおりです。

"mgResult": {
  "type": "Object",
  "value": {
    "id": "/providers/Microsoft.Management/managementGroups/examplemg1",
    "name": "examplemg1",
    "properties": {
      "details": {
        "parent": {
          "displayName": "Tenant Root Group",
          "id": "/providers/Microsoft.Management/managementGroups/00000000-0000-0000-0000-000000000000",
          "name": "00000000-0000-0000-0000-000000000000"
        },
        "updatedBy": "00000000-0000-0000-0000-000000000000",
        "updatedTime": "2020-07-23T21:05:52.661306Z",
        "version": "1"
      },
      "displayName": "Example MG 1",
      "tenantId": "00000000-0000-0000-0000-000000000000"
    },
    "type": "/providers/Microsoft.Management/managementGroups"
  }
}

次の例では、新しい管理グループを作成し、この関数を使用して親管理グループを設定します。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-08-01/managementGroupDeploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "mgName": {
      "type": "string",
      "defaultValue": "[format('mg-{0}', uniqueString(newGuid()))]"
    }
  },
  "resources": [
    {
      "type": "Microsoft.Management/managementGroups",
      "apiVersion": "2020-05-01",
      "scope": "/",
      "name": "[parameters('mgName')]",
      "properties": {
        "details": {
          "parent": {
            "id": "[managementGroup().id]"
          }
        }
      }
    }
  ],
  "outputs": {
    "newManagementGroup": {
      "type": "string",
      "value": "[parameters('mgName')]"
    }
  }
}

resourceGroup

resourceGroup()

現在のリソース グループを表すオブジェクトを返します。

Bicep では、resourceGroup スコープ関数を使用します。

戻り値

返されるオブジェクトの形式は次のとおりです。

{
  "id": "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}",
  "name": "{resourceGroupName}",
  "type":"Microsoft.Resources/resourceGroups",
  "location": "{resourceGroupLocation}",
  "managedBy": "{identifier-of-managing-resource}",
  "tags": {
  },
  "properties": {
    "provisioningState": "{status}"
  }
}

managedBy プロパティは、別のサービスによって管理されているリソースを含むリソース グループに対してのみ返されます。 Managed Applications、Databricks、および AKS の場合、プロパティの値は管理しているリソースのリソース ID になります。

解説

resourceGroup() 関数は、サブスクリプション レベルでデプロイされたテンプレートで使用できません。 リソース グループにデプロイされているテンプレートでのみ使用できます。 親テンプレートがサブスクリプションにデプロイされていても、リソース グループをターゲットとするリンク済みまたは入れ子になったテンプレート (内側のスコープを持つ)resourceGroup() 関数を使用することができます。 そのシナリオでは、リンク済みまたは入れ子になったテンプレートはリソース グループ レベルでデプロイされます。 サブスクリプション レベル デプロイでのリソース グループのターゲット設定の詳細については、「複数のサブスクリプションまたはリソース グループに Azure リソースをデプロイする」を参照してください。

resourceGroup 関数の一般的な用途では、リソース グループと同じ場所にリソースを作成します。 次の例では、既定のパラメーター値にリソース グループの場所を使用します。

"parameters": {
  "location": {
    "type": "string",
    "defaultValue": "[resourceGroup().location]"
  }
}

resourceGroup 関数を使用して、リソース グループからリソースにタグを適用することもできます。 詳細については、「リソース グループからタグを適用する」を参照してください。

入れ子になったテンプレートを使用して複数のリソース グループにデプロイするときは、resourceGroup 関数を評価するためのスコープを指定できます。 詳細については、「複数のサブスクリプションまたはリソース グループに Azure リソースをデプロイする」を参照してください。

リソース グループの例

次の例では、リソース グループのプロパティが返されます。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "resources": [],
  "outputs": {
    "resourceGroupOutput": {
      "type": "object",
      "value": "[resourceGroup()]"
    }
  }
}

前の例では、次の形式のオブジェクトが返されます。

{
  "id": "/subscriptions/{subscription-id}/resourceGroups/examplegroup",
  "name": "examplegroup",
  "type":"Microsoft.Resources/resourceGroups",
  "location": "southcentralus",
  "properties": {
    "provisioningState": "Succeeded"
  }
}

subscription

subscription()

現在のデプロイのサブスクリプションの詳細を返します。

Bicep では、subscription スコープ関数を使用 します。

戻り値

この関数は次の形式を返します。

{
  "id": "/subscriptions/{subscription-id}",
  "subscriptionId": "{subscription-id}",
  "tenantId": "{tenant-id}",
  "displayName": "{name-of-subscription}"
}

解説

入れ子になっているテンプレートを使用して複数のサブスクリプションにデプロイするとき、サブスクリプション関数を評価するためのスコープを指定できます。 詳細については、「複数のサブスクリプションまたはリソース グループに Azure リソースをデプロイする」を参照してください。

サブスクリプションの例

次の例は、outputs セクションで呼び出される subscription 関数を示しています。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "resources": [],
  "outputs": {
    "subscriptionOutput": {
      "type": "object",
      "value": "[subscription()]"
    }
  }
}

tenant

tenant()

ユーザーのテナントを返します。

Bicep では、tenant スコープ関数を使用します。

解説

tenant() は、任意のデプロイ スコープに使用できます。 常に、現在のテナントが返されます。 この関数を使用して、現在のテナントのプロパティを取得します。

リンクされたテンプレートまたは拡張リソースのスコープを設定する場合は、構文 "scope": "/" を使用します。

戻り値

現在のテナントに関するプロパティを持つオブジェクト。

テナントの例

次の例では、テナントのプロパティが返されます。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "variables": {
    "tenantInfo": "[tenant()]"
  },
  "resources": [],
  "outputs": {
    "tenantResult": {
      "type": "object",
      "value": "[variables('tenantInfo')]"
    }
  }
}

戻り値は次のとおりです。

"tenantResult": {
  "type": "Object",
  "value": {
    "countryCode": "US",
    "displayName": "Contoso",
    "id": "/tenants/00000000-0000-0000-0000-000000000000",
    "tenantId": "00000000-0000-0000-0000-000000000000"
  }
}

次のステップ