2016年6月5日日曜日

githubのAzure Resource Manager テンプレートをカスタマイズしてみた - RDP接続できるVMテンプレート

githubのAzure Resource Manager テンプレートをカスタマイズしてみた - Azure Virtual Machine with RDP connection 記事訂正
から、ストレージアカウントと仮想ネットワークは新規作成することなく、既存のものを使うようにテンプレートをカスタマイズしてみましたので、公開します。

まず、Bash on Ubuntu on Windowsによるdiffです。
※参照個所をValiablesからParametersに変えるなど細かい調整もしてます。

10a11,16
>     "dnsLabelPrefix": {
>       "type": "string",
>       "metadata": {
>         "description": "Unique DNS Name for the Public IP used to access the Virtual Machine."
>       }
>     },
23,28d28
<     "dnsLabelPrefix": {
<       "type": "string",
<       "metadata": {
<         "description": "Unique DNS Name for the Public IP used to access the Virtual Machine."
<       }
<     },
31c31
<       "defaultValue": "2012-R2-Datacenter",
---
>       "defaultValue": "Windows-Server-Technical-Preview",
35c35,36
<         "2012-R2-Datacenter"
---
>         "2012-R2-Datacenter",
>         "Windows-Server-Technical-Preview"
40a42,55
>     "vmSize": {
>       "type": "string",
>       "defaultValue": "Standard_A2",
>       "allowedValues": [
>         "Standard_A2",
>         "Standard_D1",
>         "Standard_D1_v2",
>         "Standard_DS1"
>       ]
>     },
>     "vmDataDiskSize": {
>       "type": "int",
>       "defaultValue": "100"
>     },
46a62,73
>     },
>     "VNetName": {
>       "type": "string",
>       "defaultValue": "ArmVNet1"
>     },
>     "VSubnetName": {
>       "type": "string",
>       "defaultValue": "Subnet1"
>     },
>     "storageAccountName": {
>       "type": "string",
>       "defaultValue": "armstorageact01"
50,52c77
<     "storageAccountName": "armstorageact01",
<     "sizeOfDiskInGB": "100",
<     "dataDisk1VhdName": "[concat(variables('vmName'),'-data1')]",
---
>     "sizeOfDiskInGB": "[parameters('vmDataDiskSize')]",
56c81
<     "vmSize": "Standard_D1",
---
>     "vmSize": "[parameters('vmSize')]",
58,63c83,87
<     "nicName": "[concat(variables('vmName'),'-nic1')]",
<     "addressPrefix": "10.0.0.0/16",
<     "subnetName": "Subnet1",
<     "subnetPrefix": "10.0.0.0/24",
<     "storageAccountType": "Standard_LRS",
<     "publicIPAddressName": "[concat(variables('vmName'),'-PublicIP')]",
---
>     "dataDisk1VhdName": "[concat(variables('vmName'),'-data1')]",
>     "nicName": "[concat(variables('vmName'),'-nic01')]",
>     "existingVNetName": "[parameters('VNetName')]",
>     "subnetName": "[parameters('VSubnetName')]",
>     "publicIPAddressName": "[concat(variables('vmName'),'-PublicIP01')]",
65,67c89,90
<     "vmStorageAccountContainerName": "vhds",
<     "virtualNetworkName": "ArmVNet1",
<     "vnetID": "[resourceId('Microsoft.Network/virtualNetworks',variables('virtualNetworkName'))]",
---
>     "vmStorageAccountContainerName": "[concat(variables('vmName'),'-vhds')]",
>     "vnetID": "[resourceId('Microsoft.Network/virtualNetworks',variables('existingVNetName'))]",
69,72c92,95
<     "lbName": "[concat(parameters('vmName'),'-LB')]",
<     "lbInboundNatRulesName": "[concat(parameters('vmName'),'-lbInboundNatRules01')]",
<     "lbFrontEndName": "[concat(parameters('vmName'),'-lbFrontEnd')]",
<     "lbBackendAddressPoolName": "[concat(parameters('vmName'),'-lbBackendPool1')]",
---
>     "lbName": "[concat(parameters('vmName'),'-lb01')]",
>     "lbInboundNatRulesName": "[concat(parameters('vmName'),variables('lbName'),'-InboundNatRules01')]",
>     "lbFrontEndName": "[concat(parameters('vmName'),variables('lbName'),'-FrontEnd01')]",
>     "lbBackendAddressPoolName": "[concat(parameters('vmName'),variables('lbName'),'-BackendPool01')]",
82,90d104
<       "type": "Microsoft.Storage/storageAccounts",
<       "name": "[variables('storageAccountName')]",
<       "apiVersion": "[variables('apiVersion')]",
<       "location": "[resourceGroup().location]",
<       "properties": {
<         "accountType": "[variables('storageAccountType')]"
<       }
<     },
<     {
144,164d157
<       "type": "Microsoft.Network/virtualNetworks",
<       "name": "[variables('virtualNetworkName')]",
<       "location": "[resourceGroup().location]",
<       "properties": {
<         "addressSpace": {
<           "addressPrefixes": [
<             "[variables('addressPrefix')]"
<           ]
<         },
<         "subnets": [
<           {
<             "name": "[variables('subnetName')]",
<             "properties": {
<               "addressPrefix": "[variables('subnetPrefix')]"
<             }
<           }
<         ]
<       }
<     },
<     {
<       "apiVersion": "[variables('apiVersion')]",
169d161
<         "[concat('Microsoft.Network/virtualNetworks/', variables('virtualNetworkName'))]",
202d193
<         "[concat('Microsoft.Storage/storageAccounts/', variables('storageAccountName'))]",
222c213
<             "name": "osdisk",
---
>             "name": "[variables('OSDiskName')]",
224c215
<               "uri": "[concat('http://',variables('storageAccountName'),'.blob.core.windows.net/',variables('vmStorageAccountContainerName'),'/',variables('OSDiskName'),'.vhd')]"
---
>               "uri": "[concat('http://',parameters('storageAccountName'),'.blob.core.windows.net/',variables('vmStorageAccountContainerName'),'/',variables('OSDiskName'),'.vhd')]"
231c222
<               "name": "datadisk1",
---
>               "name": "[variables('dataDisk1VhdName')]",
235c226
<                 "uri": "[concat('http://',variables('storageAccountName'),'.blob.core.windows.net/',variables('vmStorageAccountContainerName'),'/',variables('dataDisk1VhdName'),'.vhd')]"
---
>                 "uri": "[concat('http://',parameters('storageAccountName'),'.blob.core.windows.net/',variables('vmStorageAccountContainerName'),'/',variables('dataDisk1VhdName'),'.vhd')]"
251c242
<             "storageUri": "[concat('http://',variables('storageAccountName'),'.blob.core.windows.net')]"
---
>             "storageUri": "[concat('http://',parameters('storageAccountName'),'.blob.core.windows.net')]"

違いとしては
  • 仮想マシンのdependsOnにストレージアカウントを指定しない。
  • ネットワークインタフェースdependsOnに仮想ネットワークを指定しない。
となります。
では、どうやって関連付けるのかということになります。
  • ディスクのURIで指定する。具体的なコード例は下記の赤字部分。
    "osDisk": {
                "name": "[variables('OSDiskName')]",
                "vhd": {
                  "uri": "[concat('http://',parameters('storageAccountName'),'.blob.core.windows.net/',variables('vmStorageAccountContainerName'),'/',variables('OSDiskName'),'.vhd')]"
                },
                "caching": "ReadWrite",
                "createOption": "FromImage"
              },
              "dataDisks": [
                {
                  "name": "[variables('dataDisk1VhdName')]",
                  "diskSizeGB": "[variables('sizeOfDiskInGB')]",
                  "lun": 0,
                  "vhd": {
                    "uri": "[concat('http://',parameters('storageAccountName'),'.blob.core.windows.net/',variables('vmStorageAccountContainerName'),'/',variables('dataDisk1VhdName'),'.vhd')]"
                  },
                  "createOption": "Empty"
                }
              ]
            },
    
    
  • ネットワークインタフェースのpropertiesの中で、指定。具体的なコード例は下記の赤字部分。
    "properties": {
            "ipConfigurations": [
              {
                "name": "ipconfig1",
                "properties": {
                  "privateIPAllocationMethod": "Dynamic",
                  "subnet": {
                    "id": "[variables('subnetRef')]"
                  },
    
    
これが、わかるまでに結構試行錯誤しました。
あと、OSバージョンは、実際にWindows Server 2015 TP5をデプロイして、エクスポートしたテンプレートから調べたところ、"Windows-Server-Technical-Preview"だとわかりました。ただ、正式リリースされたらまた変更されるはずですね。


Azure Resource Manager Template Visualizerで読み込んでみると下記のような感じになります。


ということで、コード全体を下記に記載しておきます。

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "vmName": {
      "type": "string",
      "metadata": {
        "description": "name for the Virtual Machine."
      }
    },
    "dnsLabelPrefix": {
      "type": "string",
      "metadata": {
        "description": "Unique DNS Name for the Public IP used to access the Virtual Machine."
      }
    },
    "adminUsername": {
      "type": "string",
      "metadata": {
        "description": "Username for the Virtual Machine."
      }
    },
    "adminPassword": {
      "type": "securestring",
      "metadata": {
        "description": "Password for the Virtual Machine."
      }
    },
    "windowsOSVersion": {
      "type": "string",
      "defaultValue": "Windows-Server-Technical-Preview",
      "allowedValues": [
        "2008-R2-SP1",
        "2012-Datacenter",
        "2012-R2-Datacenter",
        "Windows-Server-Technical-Preview"
      ],
      "metadata": {
        "description": "The Windows version for the VM. This will pick a fully patched image of this given Windows version. Allowed values: 2008-R2-SP1, 2012-Datacenter, 2012-R2-Datacenter."
      }
    },
    "vmSize": {
      "type": "string",
      "defaultValue": "Standard_A2",
      "allowedValues": [
        "Standard_A2",
        "Standard_D1",
        "Standard_D1_v2",
        "Standard_DS1"
      ]
    },
    "vmDataDiskSize": {
      "type": "int",
      "defaultValue": "100"
    },
    "rdpFrontend": {
      "type": "int",
      "defaultValue": 50001,
      "metadata": {
        "description": "Public port number for RDP"
      }
    },
    "VNetName": {
      "type": "string",
      "defaultValue": "ArmVNet1"
    },
    "VSubnetName": {
      "type": "string",
      "defaultValue": "Subnet1"
    },
    "storageAccountName": {
      "type": "string",
      "defaultValue": "armstorageact01"
    }
  },
  "variables": {
    "sizeOfDiskInGB": "[parameters('vmDataDiskSize')]",
    "imagePublisher": "MicrosoftWindowsServer",
    "imageOffer": "WindowsServer",
    "vmName": "[parameters('vmName')]",
    "vmSize": "[parameters('vmSize')]",
    "OSDiskName": "[concat(variables('vmName'),'-os')]",
    "dataDisk1VhdName": "[concat(variables('vmName'),'-data1')]",
    "nicName": "[concat(variables('vmName'),'-nic01')]",
    "existingVNetName": "[parameters('VNetName')]",
    "subnetName": "[parameters('VSubnetName')]",
    "publicIPAddressName": "[concat(variables('vmName'),'-PublicIP01')]",
    "publicIPAddressType": "Dynamic",
    "vmStorageAccountContainerName": "[concat(variables('vmName'),'-vhds')]",
    "vnetID": "[resourceId('Microsoft.Network/virtualNetworks',variables('existingVNetName'))]",
    "subnetRef": "[concat(variables('vnetID'),'/subnets/',variables('subnetName'))]",
    "lbName": "[concat(parameters('vmName'),'-lb01')]",
    "lbInboundNatRulesName": "[concat(parameters('vmName'),variables('lbName'),'-InboundNatRules01')]",
    "lbFrontEndName": "[concat(parameters('vmName'),variables('lbName'),'-FrontEnd01')]",
    "lbBackendAddressPoolName": "[concat(parameters('vmName'),variables('lbName'),'-BackendPool01')]",
    "publicIPAddressID": "[resourceId('Microsoft.Network/publicIPAddresses',variables('publicIPAddressName'))]",
    "lbID": "[resourceId('Microsoft.Network/loadBalancers',variables('lbName'))]",
    "lbFrontEndIPConfigID": "[concat(variables('lbID'),'/frontendIPConfigurations/',variables('lbFrontEndName'))]",
    "lbPoolID": "[concat(variables('lbID'),'/backendAddressPools/',variables('lbBackendAddressPoolName'))]",
    "lbNatID": "[concat(variables('lbID'),'/inboundNatRules/',variables('lbInboundNatRulesName'))]",
    "apiVersion": "2015-06-15"
  },
  "resources": [
    {
      "apiVersion": "[variables('apiVersion')]",
      "type": "Microsoft.Network/publicIPAddresses",
      "name": "[variables('publicIPAddressName')]",
      "location": "[resourceGroup().location]",
      "properties": {
        "publicIPAllocationMethod": "[variables('publicIPAddressType')]",
        "dnsSettings": {
          "domainNameLabel": "[parameters('dnsLabelPrefix')]"
        }
      }
    },
    {
      "apiVersion": "[variables('apiVersion')]",
      "name": "[variables('lbName')]",
      "type": "Microsoft.Network/loadBalancers",
      "location": "[resourceGroup().location]",
      "dependsOn": [
        "[concat('Microsoft.Network/publicIPAddresses/', variables('publicIPAddressName'))]"
      ],
      "properties": {
        "frontendIPConfigurations": [
          {
            "name": "[variables('lbFrontEndName')]",
            "properties": {
              "publicIPAddress": {
                "id": "[variables('publicIPAddressID')]"
              }
            }
          }
        ],
        "backendAddressPools": [
          {
            "name": "[variables('lbBackendAddressPoolName')]"
          }
        ],
        "inboundNatRules": [
          {
            "name": "[variables('lbInboundNatRulesName')]",
            "properties": {
              "frontendIPConfiguration": {
                "id": "[variables('lbFrontEndIPConfigID')]"
              },
              "protocol": "tcp",
              "frontendPort": "[parameters('rdpFrontend')]",
              "backendPort": 3389,
              "enableFloatingIP": false
            }
          }
        ]
      }
    },
    {
      "apiVersion": "[variables('apiVersion')]",
      "type": "Microsoft.Network/networkInterfaces",
      "name": "[variables('nicName')]",
      "location": "[resourceGroup().location]",
      "dependsOn": [
        "[concat('Microsoft.Network/loadBalancers/',variables('lbName'))]"
      ],
      "properties": {
        "ipConfigurations": [
          {
            "name": "ipconfig1",
            "properties": {
              "privateIPAllocationMethod": "Dynamic",
              "subnet": {
                "id": "[variables('subnetRef')]"
              },
              "loadBalancerBackendAddressPools": [
                {
                  "id": "[variables('lbPoolID')]"
                }
              ],
              "loadBalancerInboundNatRules": [
                {
                  "id": "[variables('lbNatID')]"
                }
              ]
            }
          }
        ]
      }
    },
    {
      "apiVersion": "[variables('apiVersion')]",
      "type": "Microsoft.Compute/virtualMachines",
      "name": "[variables('vmName')]",
      "location": "[resourceGroup().location]",
      "dependsOn": [
        "[concat('Microsoft.Network/networkInterfaces/', variables('nicName'))]"
      ],
      "properties": {
        "hardwareProfile": {
          "vmSize": "[variables('vmSize')]"
        },
        "osProfile": {
          "computerName": "[variables('vmName')]",
          "adminUsername": "[parameters('adminUsername')]",
          "adminPassword": "[parameters('adminPassword')]"
        },
        "storageProfile": {
          "imageReference": {
            "publisher": "[variables('imagePublisher')]",
            "offer": "[variables('imageOffer')]",
            "sku": "[parameters('windowsOSVersion')]",
            "version": "latest"
          },
          "osDisk": {
            "name": "[variables('OSDiskName')]",
            "vhd": {
              "uri": "[concat('http://',parameters('storageAccountName'),'.blob.core.windows.net/',variables('vmStorageAccountContainerName'),'/',variables('OSDiskName'),'.vhd')]"
            },
            "caching": "ReadWrite",
            "createOption": "FromImage"
          },
          "dataDisks": [
            {
              "name": "[variables('dataDisk1VhdName')]",
              "diskSizeGB": "[variables('sizeOfDiskInGB')]",
              "lun": 0,
              "vhd": {
                "uri": "[concat('http://',parameters('storageAccountName'),'.blob.core.windows.net/',variables('vmStorageAccountContainerName'),'/',variables('dataDisk1VhdName'),'.vhd')]"
              },
              "createOption": "Empty"
            }
          ]
        },
        "networkProfile": {
          "networkInterfaces": [
            {
              "id": "[resourceId('Microsoft.Network/networkInterfaces',variables('nicName'))]"
            }
          ]
        },
        "diagnosticsProfile": {
          "bootDiagnostics": {
            "enabled": "true",
            "storageUri": "[concat('http://',parameters('storageAccountName'),'.blob.core.windows.net')]"
          }
        }
      }
    }
  ]
}

0 件のコメント:

コメントを投稿