SQL Server IaaS Agent 拡張機能をインストールするためのコマンドレットとして、「Set-AzureRmVMSqlServerExtension」がありますが、IaaS Agent については、ARM テンプレートを使用してインストールすることもできます。
以前、ARM の仮想マシンに SQL Server IaaS Agent を導入してみる という投稿を書いたのですが、この際はコマンドレットを使用していましたので、今回は ARM テンプレートを使用して、「New-AzureRmResourceGroupDeployment」から登録してみたいと思います。
New-AzureRmResourceGroupDeployment では、JSON のテンプレートとパラメーターを渡すことで、デプロイができますので、最初にテンプレートを用意します。
今回は既に作成されている仮想マシンに対して IaaS Agent 拡張機能をインストールするため以下のようなテンプレートを用意します。
{ "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "parameters": { "virtualMachineName": { "type": "String" }, "location": { "type": "String" }, "StorageAccountName": { "type": "String" }, "sqlAutopatchingDayOfWeek": { "type": "String" }, "sqlAutopatchingStartHour": { "type": "String" }, "sqlAutopatchingWindowDuration": { "type": "String" }, "sqlAutobackupRetentionPeriod": { "type": "String" }, "sqlAutobackupEncryptionPassword": { "type": "SecureString" }, "sqlAkvCredentialName": { "type": "String" }, "sqlAkvUrl": { "type": "String" }, "sqlAkvPrincipalName": { "type": "String" }, "sqlAkvPrincipalSecret": { "type": "SecureString" } }, "variables": {}, "resources": [ { "type": "Microsoft.Compute/virtualMachines/extensions", "name": "[concat(parameters('virtualMachineName'), '/SqlIaasExtension')]", "apiVersion": "2015-06-15", "location": "[parameters('location')]", "properties": { "type": "SqlIaaSAgent", "publisher": "Microsoft.SqlServer.Management", "typeHandlerVersion": "1.2", "autoUpgradeMinorVersion": "true", "settings": { "AutoTelemetrySettings": { "Region": "[parameters('location')]" }, "AutoPatchingSettings": { "PatchCategory": "WindowsMandatoryUpdates", "Enable": true, "DayOfWeek": "[parameters('sqlAutopatchingDayOfWeek')]", "MaintenanceWindowStartingHour": "[parameters('sqlAutopatchingStartHour')]", "MaintenanceWindowDuration": "[parameters('sqlAutopatchingWindowDuration')]" }, "AutoBackupSettings": { "Enable": true, "RetentionPeriod": "[parameters('sqlAutobackupRetentionPeriod')]", "EnableEncryption": true }, "KeyVaultCredentialSettings": { "Enable": true, "CredentialName": "[parameters('sqlAkvCredentialName')]" } }, "protectedSettings": { "StorageUrl": "[reference(resourceId('Microsoft.Storage/storageAccounts', parameters('StorageAccountName')), '2015-06-15').primaryEndpoints['blob']]", "StorageAccessKey": "[listKeys(resourceId('Microsoft.Storage/storageAccounts', parameters('StorageAccountName')), '2015-06-15').key1]", "Password": "[parameters('sqlAutobackupEncryptionPassword')]", "PrivateKeyVaultCredentialSettings": { "AzureKeyVaultUrl": "[parameters('sqlAkvUrl')]", "ServicePrincipalName": "[parameters('sqlAkvPrincipalName')]", "ServicePrincipalSecret": "[parameters('sqlAkvPrincipalSecret')]" } } } } ], "outputs": {} }
IaaS Agent 拡張機能としては、以下の機能があるためそれぞれに必要となるパラメーターを指定できるようにしています。
- 自動修正
- 自動バックアップ
- Azure Key Vault の統合
上記のテンプレートに対して、パラメーターを指定する方法としては、
- パラメーターファイル
- ハッシュテーブルの変数
を使用する方法があります。
パラメーターファイルについては以下のようなファイルを使用します。
{ "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#", "contentVersion": "1.0.0.0", "parameters": { "virtualMachineName": { "value": "<仮想マシン名>" }, "location": { "value": "japanwest" }, "StorageAccountName": { "value": "<ストレージアカウント名>" }, "sqlAutopatchingDayOfWeek": { "value": "Sunday" }, "sqlAutopatchingStartHour": { "value": "2" }, "sqlAutopatchingWindowDuration": { "value": "60" }, "sqlAutobackupRetentionPeriod": { "value": "30" }, "sqlAutobackupEncryptionPassword": { "value": "<暗号化パスワードのパスワード>" }, "sqlAkvCredentialName": { "value": "<sql Server に登録する資格情報名>" }, "sqlAkvUrl": { "value": "https://<key Vault URL>.vault.azure.net" }, "sqlAkvPrincipalName": { "value": "<aad のアプリケーションのクライアント ID>" }, "sqlAkvPrincipalSecret": { "value": "<aad のアプリケーションのキー>" } } }
これらのファイルを以下のコマンドを使用してデプロイします。
New-AzureRmResourceGroupDeployment -ResourceGroupName <リソースグループ名> -TemplateFile .\template.json -TemplateParameterFile .\parameter.json
ハッシュテーブルの変数を使用する場合は、以下のようなコマンドを実行します。
$parameter = @{ virtualMachineName = "<仮想マシン名>" location = "japanwest" StorageAccountName = "<ストレージアカウント名>" sqlAutopatchingDayOfWeek = "Sunday" sqlAutopatchingStartHour = "2" sqlAutopatchingWindowDuration = "60" sqlAutobackupRetentionPeriod = "30" sqlAutobackupEncryptionPassword = "<暗号化パスワードのパスワード>" sqlAkvCredentialName = "<sql Server に登録する資格情報名>" sqlAkvUrl = "https://<key Vault URL>.vault.azure.net" sqlAkvPrincipalName = "<aad のアプリケーションのクライアント ID>" sqlAkvPrincipalSecret ="<aad のアプリケーションのキー>" } New-AzureRmResourceGroupDeployment -ResourceGroupName $resourcegroup.ResourceGroupName -TemplateFile .\template.json -TemplateParameterObject $parameter
このテンプレートについてはポータルから SQL Server をデプロイした際に使用されているものですので、SQL Server のイメージをポータルから展開する場合にもしようすることができるかと。