Azure の組み込みロールが組織の特定のニーズを満たさない場合は、独自のカスタム ロールを作成することができます。 この記事では、Azure Resource Manager テンプレート (ARM テンプレート) を使用して、カスタム ロールを作成または更新する方法について説明します。
Azure Resource Manager テンプレートは JavaScript Object Notation (JSON) ファイルであり、プロジェクトのインフラストラクチャと構成が定義されています。 このテンプレートでは、宣言型の構文が使用されています。 デプロイしようとしているものを、デプロイを作成する一連のプログラミング コマンドを記述しなくても記述できます。
カスタム ロールを作成するには、ロール名、アクセス許可、およびロールを使用できる場所を指定します。 この記事では、サブスクリプション スコープ以下で割り当てることができる、リソースのアクセス許可を持つ Custom Role - RG Reader という名前のロールを作成します。
環境が前提条件を満たしていて、ARM テンプレートの使用に慣れている場合は、 [Azure へのデプロイ] ボタンを選択します。 Azure portal でテンプレートが開きます。
前提条件
カスタム ロールを作成するには、以下が必要です。
- ユーザー アクセス管理者など、カスタム ロールを作成するためのアクセス許可。
次のバージョンを使用する必要があります。
2018-07-01
以降
詳細については、Azure RBAC REST API の API バージョンに関するページを参照してください。
テンプレートを確認する
この記事で使用されているテンプレートは Azure クイックスタート テンプレートからのものです。 テンプレートには、4 つのパラメーターとリソース セクションがあります。 4 つのパラメーターは次のとおりです。
- 既定値が
["Microsoft.Resources/subscriptions/resourceGroups/read"]
であるアクションの配列。 - 空の既定値が指定された
notActions
の配列。 - 既定値が
Custom Role - RG Reader
であるロールの名前。 - 既定値が
Subscription Level Deployment of a Role Definition
であるロールの説明。
このカスタム ロールを割り当てることができるスコープは、現在のサブスクリプションに設定されます。
{
"$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"metadata": {
"_generator": {
"name": "bicep",
"version": "0.25.53.49325",
"templateHash": "16704138909949665309"
}
},
"parameters": {
"actions": {
"type": "array",
"defaultValue": [
"Microsoft.Resources/subscriptions/resourceGroups/read"
],
"metadata": {
"description": "Array of actions for the roleDefinition"
}
},
"notActions": {
"type": "array",
"defaultValue": [],
"metadata": {
"description": "Array of notActions for the roleDefinition"
}
},
"roleName": {
"type": "string",
"defaultValue": "Custom Role - RG Reader",
"metadata": {
"description": "Friendly name of the role definition"
}
},
"roleDescription": {
"type": "string",
"defaultValue": "Subscription Level Deployment of a Role Definition",
"metadata": {
"description": "Detailed description of the role definition"
}
}
},
"variables": {
"roleDefName": "[guid(parameters('roleName'))]"
},
"resources": [
{
"type": "Microsoft.Authorization/roleDefinitions",
"apiVersion": "2022-04-01",
"name": "[variables('roleDefName')]",
"properties": {
"roleName": "[parameters('roleName')]",
"description": "[parameters('roleDescription')]",
"type": "customRole",
"permissions": [
{
"actions": "[parameters('actions')]",
"notActions": "[parameters('notActions')]"
}
],
"assignableScopes": [
"[subscription().id]"
]
}
}
]
}
テンプレート内に定義されているリソース:
テンプレートのデプロイ
前述のテンプレートをデプロイするには、次の手順に従います。
Azure portal にサインインします。
PowerShell 用の Azure Cloud Shell を開きます。
次のスクリプトをコピーして Cloud Shell に貼り付けます。
$___location = Read-Host -Prompt "Enter a ___location (i.e. centralus)" [string[]]$actions = Read-Host -Prompt "Enter actions as a comma-separated list (i.e. action1,action2)" $actions = $actions.Split(',') $templateUri = "https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/subscription-deployments/create-role-def/azuredeploy.json" New-AzDeployment -Location $___location -TemplateUri $templateUri -actions $actions
デプロイの場所を入力します (
centralus
など)。カスタム ロールのアクションの一覧をコンマ区切りリスト (
Microsoft.Resources/resources/read,Microsoft.Resources/subscriptions/resourceGroups/read
など) として入力します。必要に応じて、Enter キーを押して
New-AzDeployment
コマンドを実行します。New-AzDeployment コマンドを実行すると、テンプレートがデプロイされ、カスタム ロールが作成されます。
次のような出力が表示されます。
PS> New-AzDeployment -Location $___location -TemplateUri $templateUri -actions $actions Id : /subscriptions/{subscriptionId}/providers/Microsoft.Resources/deployments/azuredeploy DeploymentName : azuredeploy Location : centralus ProvisioningState : Succeeded Timestamp : 6/25/2020 8:08:32 PM Mode : Incremental TemplateLink : Uri : https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/subscription-deployments/create-role-def/azuredeploy.json ContentVersion : 1.0.0.0 Parameters : Name Type Value ================= ========================= ========== actions Array [ "Microsoft.Resources/resources/read", "Microsoft.Resources/subscriptions/resourceGroups/read" ] notActions Array [] roleName String Custom Role - RG Reader roleDescription String Subscription Level Deployment of a Role Definition Outputs : DeploymentDebugLogLevel :
デプロイされているリソースを確認する
カスタム ロールが作成されたことを確認するには、次の手順に従います。
Get-AzRoleDefinition コマンドを実行して、カスタム ロールを一覧表示します。
Get-AzRoleDefinition "Custom Role - RG Reader" | ConvertTo-Json
次のような出力が表示されます。
{ "Name": "Custom Role - RG Reader", "Id": "11111111-1111-1111-1111-111111111111", "IsCustom": true, "Description": "Subscription Level Deployment of a Role Definition", "Actions": [ "Microsoft.Resources/resources/read", "Microsoft.Resources/subscriptions/resourceGroups/read" ], "NotActions": [], "DataActions": [], "NotDataActions": [], "AssignableScopes": [ "/subscriptions/{subscriptionId}" ] }
Azure portal で、お使いのサブスクリプションを開きます。
左側のメニューで [アクセス制御 (IAM)] を選択します。
[ロール] タブを選択します。
[種類] リストを [CustomRole] に設定します。
Custom Role - RG Reader ロールが表示されていることを確認します。
カスタム ロールの更新
カスタム ロールを作成する場合と同じく、テンプレートを使用して既存のカスタム ロールを更新できます。 カスタム ロールを更新するには、更新するロールを指定する必要があります。
ここでは、カスタム ロールを更新するために、以前のクイック スタート テンプレートに対して行う必要がある変更について説明します。
ロール ID をパラメーターとして含めます。
... "roleDefName": { "type": "string", "metadata": { "description": "ID of the role definition" } ...
ロール定義にロール ID パラメーターを含めます。
... "resources": [ { "type": "Microsoft.Authorization/roleDefinitions", "apiVersion": "2022-04-01", "name": "[parameters('roleDefName')]", "properties": { ...
テンプレートをデプロイする方法の例を次に示します。
$___location = Read-Host -Prompt "Enter a ___location (i.e. centralus)"
[string[]]$actions = Read-Host -Prompt "Enter actions as a comma-separated list (i.e. action1,action2)"
$actions = $actions.Split(',')
$roleDefName = Read-Host -Prompt "Enter the role ID to update"
$templateFile = "rg-reader-update.json"
New-AzDeployment -Location $___location -TemplateFile $templateFile -actions $actions -roleDefName $roleDefName
リソースをクリーンアップする
カスタム ロールを削除するには、次の手順に従います。
次のコマンドを実行して、カスタム ロールを削除します。
Get-AzRoleDefinition -Name "Custom Role - RG Reader" | Remove-AzRoleDefinition
「Y」と入力して、カスタム ロールを削除することを確認します。