Azureでテンプレート仕様を使っている速度でARMテンプレートを届けてください


テンプレートspecは、azure環境マネージャ(ARM)テンプレートをazure環境で安全に保存するために使用されます.マイクロソフト.リソース/templatespecsはテンプレート仕様のリソースタイプです.
テンプレートの仕様を使用すると、あなたの組織のユーザーとアームテンプレートを共有することができます.また、このポストで後で表示するバージョン管理もサポートしています.

なぜテンプレート仕様を使用するか?


あなたの組織でARMテンプレートを使用してインフラ開発に取り組んでいるとします.これらのテンプレートを他のチームと共有しようとするとき、いくつかの問題があります.テンプレートが倉庫または保管口座に格納されるならば、彼らが必要な許可をしない限り、ユーザーはそれにアクセスすることができません.許可が与えられていても、テンプレートを更新し続けるにつれて最終的に時代遅れになるテンプレートの独自のローカルバージョンを使用して配置されます.
Azureのテンプレート仕様はAzure役割ベースのアクセスコントロール(RBAC)によって保護されます.それで、あなたは安全にグループを保存することができます、そして、Azure自体でRBACを使用しているコントロールアクセスで.あなたの組織の参照アーキテクチャが時間とともに変化するとき、あなたはより新しいバージョンでテンプレート仕様をアップデートすることができます.

リンクテンプレートを使用した新しいテンプレート仕様の作成


テンプレート仕様を作成するときは、メインテンプレートファイルをCLIコマンドに渡します.メインテンプレートファイルの中で参照されるリンクされたテンプレートがあるならば、それらは一つのテンプレートspecリソースで主なテンプレートとともにパッケージ化されます.
メインテンプレートを作成するazuredeploy.json 以下のJSONをワーキングディレクトリに入れてください.
{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "appServicePlanName": {
            "type": "string",
            "metadata": {
                "description": "Enter a name for app service plan"
            }
        },
        "appServiceName": {
            "type": "string",
            "metadata": {
                "description": "Enter a name for app service"
            }
        }
    },
    "functions": [],
    "variables": {},
    "resources": [
    {
        "name": "webappDeployment",
        "type": "Microsoft.Resources/deployments",
        "apiVersion": "2020-06-01",
        "properties": {
            "mode": "Incremental",
            "templateLink": {
                "relativePath": "artifacts/webapp.json"
            },
            "parameters": {
                "appServicePlanName": {
                    "value": "[parameters('appServicePlanName')]"
                },
                "appServiceName": {
                    "value": "[parameters('appServiceName')]"
                }
            }
        }
    }
    ],
    "outputs": {}
}
上記のテンプレート参照の展開リソースwebapp.json . 次のリンクを作成します.
作業ディレクトリで新しいディレクトリを作成しますartifacts テンプレートを作成するwebapp.json 次のJSONのディレクトリ内に
{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "appServicePlanName": {
            "type": "string",
            "metadata": {
                "description": "Name of app service plan"
            }
        },
        "appServiceName": {
            "type": "string",
            "metadata": {
                "description": "Name of app service"
            }
        }
    },
    "functions": [],
    "variables": {},
    "resources": [
        {
            "name": "[parameters('appServicePlanName')]",
            "type": "Microsoft.Web/serverfarms",
            "apiVersion": "2019-08-01",
            "kind": "app",
            "location": "[resourceGroup().location]",
            "sku": {
                "name": "S1"
            }
        },
        {
            "name": "[parameters('appServiceName')]",
            "type": "Microsoft.Web/sites",
            "apiVersion": "2019-08-01",
            "kind": "app",
            "location": "[resourceGroup().location]",
            "dependsOn": [
                "[resourceId('Microsoft.Web/serverfarms', parameters('appServicePlanName'))]"
            ],
            "properties": {
                "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', 'appServicePlan1')]"
            }
        }
    ],
    "outputs": {}
}

テンプレート仕様を展開する


テンプレートspecを展開するには、PowerShell、Azure CLI、Azureポータル、REST、および他のサポートされているSDKとクライアントのような様々なアジュールツールを使用できます.PowerShellを使ってテンプレート仕様を展開します.
ワーキングディレクトリにPowerShell端末を開き、以下のPowerShell CmdLetを実行して新しいリソースグループを作成します.
$rg = "TemplateSpecsRG"

New-AzResourceGroup `
  -Name $rg `
  -Location eastus
その後、New-AzTemplateSpec 上記のテンプレートに基づいて新しいテンプレートspecを作成するためのcmdlet
New-AzTemplateSpec `
  -Name webappSpec `
  -Version "1.0" `
  -ResourceGroupName $rg `
  -Location eastus `
  -TemplateFile "azuredeploy.json"
CMDREletが正常に実行されると、展開しているspecを表示できますGet-AzTemplateSpec cmdlet :
> Get-AzTemplateSpec -ResourceGroupName $rg -Name webappSpec


Id                    : /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/TemplateSpecsRG/providers/Microsoft.Resou
                        rces/templateSpecs/webappSpec
Name                  : webappSpec
ResourceGroupName     : TemplateSpecsRG
SubscriptionId        : xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Location              : eastus
Versions              : 1.0
CreationTime(UTC)     : 2/17/2021 5:42:57 AM
LastModifiedTime(UTC) : 2/17/2021 5:43:02 AM
ポータルでは、リソースグループに移動し、テンプレート仕様リソースを表示できます.

テンプレートSpecリソース画面に移動すると、メインテンプレートとリンク先のテンプレートが含まれていることがわかります.あなたが入力したバージョンも、最新バージョンとして表示されていることに注意してください.

また、テンプレートをドリルダウンすることができますし、ポータル自体のコンテンツを参照してください.

テンプレートを修正


現在アーキテクチャが更新され、新しいストレージアカウントがWebアプリケーションと一緒に追加する必要があると仮定します.既存のテンプレートspecを変更して、新しいストレージアカウントをリンクテンプレートとして追加します.
変更するazuredeploy.json ファイルを新規展開リソースを追加します.変更されたテンプレートは次のようになります.
{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "appServicePlanName": {
            "type": "string",
            "metadata": {
                "description": "Enter a name for app service plan"
            }
        },
        "appServiceName": {
            "type": "string",
            "metadata": {
                "description": "Enter a name for app service"
            }
        },
        "storageAccountName": {
            "type": "string",
            "metadata": {
                "description": "Enter a name for storage account"
            }
        },
        "storageAccountType": {
            "type": "string",
            "defaultValue": "Standard_LRS",
            "metadata": {
                "description": "Specify the storage account type"
            }
        }
    },
    "functions": [],
    "variables": {},
    "resources": [
        {
            "name": "webappDeployment",
            "type": "Microsoft.Resources/deployments",
            "apiVersion": "2020-06-01",
            "properties": {
                "mode": "Incremental",
                "templateLink": {
                    "relativePath": "artifacts/webapp.json"
                },
                "parameters": {
                    "appServicePlanName": {
                        "value": "[parameters('appServicePlanName')]"
                    },
                    "appServiceName": {
                        "value": "[parameters('appServiceName')]"
                    }
                }
            }
        },
        {
            "name": "storageAccountDeployment",
            "type": "Microsoft.Resources/deployments",
            "apiVersion": "2020-06-01",
            "properties": {
                "mode": "Incremental",
                "templateLink": {
                    "relativePath": "artifacts/storageAccount.json"
                },
                "parameters": {
                    "storageAccountName": {
                        "value": "[parameters('storageAccountName')]"
                    },
                    "storageAccountType": {
                        "value": "[parameters('storageAccountType')]"
                    }
                }
            }
        }
    ],
    "outputs": {}
}
新しいテンプレートを作るstorageAccount.jsonartifacts 次のJSONディレクトリ
{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "storageAccountName": {
            "type": "string",
            "metadata": {
            "description": "Storage Account Name"
            }
        },
        "storageAccountType": {
            "type": "string",
            "defaultValue": "Standard_LRS",
            "allowedValues": [
            "Standard_LRS",
            "Standard_GRS",
            "Standard_ZRS",
            "Premium_LRS"
            ],
            "metadata": {
            "description": "Storage Account type"
            }
        },
        "location": {
            "type": "string",
            "defaultValue": "[resourceGroup().location]",
            "metadata": {
            "description": "Location for all resources."
            }
        }
    },
    "variables": {},
    "resources": [
        {
            "type": "Microsoft.Storage/storageAccounts",
            "apiVersion": "2019-04-01",
            "name": "[parameters('storageAccountName')]",
            "location": "[parameters('location')]",
            "sku": {
            "name": "[parameters('storageAccountType')]"
            },
            "kind": "StorageV2",
            "properties": {}
        }
    ],
    "outputs": {}
}
今度は新しいテンプレートでテンプレートspecを展開しますVersion :
New-AzTemplateSpec `
  -Name webappSpec `
  -Version "1.1" `
  -ResourceGroupName $rg `
  -Location eastus `
  -TemplateFile "azuredeploy.json"
あなたが前のものと同じバージョンを入力するならば、それはテンプレート仕様を上書きします.
今、あなたはGet-AzTemplateSpec cmdletでは、テンプレートspecの2つのバージョンを出力します.
> Get-AzTemplateSpec -ResourceGroupName $rg -Name webappSpec


Id                    : /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/TemplateSpecsRG/providers/Microsoft.Resou
                        rces/templateSpecs/webappSpec
Name                  : webappSpec
ResourceGroupName     : TemplateSpecsRG
SubscriptionId        : xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Location              : eastus
Versions              : {1.0, 1.1}
CreationTime(UTC)     : 2/17/2021 5:42:57 AM
LastModifiedTime(UTC) : 2/17/2021 6:00:07 AM
cmdletの実行後、最新のバージョンがポータルと新しいアーティファクトで更新されることを確認しますstorageAccount.json がテンプレート

あなたが組織することができて、ARMテンプレートを共有することができるこの方法は、あなたの組織のユーザーと速くなります.
また、チャンネル上の下のYouTubeのビデオを参照することができます.
すぐにテンプレート仕様をレバレッジスタート!