曾国藩:一勤天下无难事

发布Azure资源管理(ARM)模板

2019.03.08

以下为代码模板,根据自己情况修改

{
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": { 
    "virtualMachineName": {
      "type": "string",
      "metadata": {
        "description": "虚拟机名称"
      }
    },
    "virtualMachineSize": {
      "type": "string",
      "defaultValue":"Standard_D4s_v3",
      "metadata": {
        "description": "虚拟机大小"
      }
    },
    "adminUsername": {
      "type": "string",
      "metadata": {
        "description": "用户名"
      }
    },
    "virtualNetworkName": {
      "type": "string",
      "metadata": {
        "description": "虚拟网络名"
      }
    },
    "adminPassword": {
      "type": "securestring",
      "metadata": {
        "description": "用户密码"
      }
    },
    "diagnosticsStorageAccountName": {
      "type": "string",
      "metadata": {
        "description": "存储账户名"
      }
    },
    "subnetName": {
      "type": "string",
      "metadata": {
        "description": "子网名称"
      }
    },
    "publicIpAddressName": {
      "type": "string",
      "metadata": {
        "description": "公网地址名称"
      }
    }
  },
  "variables": { 
    "addressPrefix": "10.0.0.0/16",
    "subnetPrefix": "10.0.0.0/24",
    "networkInterfaceName": "alauda-Nic0",
    "networkSecurityGroupName": "alauda-NSG0",
    "diagnosticsStorageAccountType": "Standard_LRS",
    "vnetId": "[resourceId(parameters('virtualMachineName'),'Microsoft.Network/virtualNetworks', parameters('virtualNetworkName'))]",
    "subnetRef": "[resourceId('Microsoft.Network/virtualNetworks/subnets', parameters('virtualNetworkName'), parameters('subnetName'))]"
  },
  "resources": [ 
    {
      "name": "[parameters('virtualMachineName')]",
      "type": "Microsoft.Compute/virtualMachines",
      "apiVersion": "2018-06-01",
      "location": "[resourceGroup().location]",
      "dependsOn": [
        "[concat('Microsoft.Network/networkInterfaces/', variables('networkInterfaceName'))]",
        "[concat('Microsoft.Storage/storageAccounts/', parameters('diagnosticsStorageAccountName'))]"
       ],
      "properties": {
        "osProfile": {
          "computerName": "[parameters('virtualMachineName')]",
          "adminUsername": "[parameters('adminUsername')]",
          "adminPassword": "[parameters('adminPassword')]"
        },
        "hardwareProfile": {
          "vmSize": "[parameters('virtualMachineSize')]"
        },
        "storageProfile": {
          "imageReference": {
            "publisher": "xxxx",   # 上传虚拟机镜像时填写的服务商ID
            "offer": "xxxx-xxxx-xxx",   # 上传虚拟机镜像时填写的服务ID
            "sku": "standard",  # 上传虚拟机镜像时填写的sku
            "version": "latest"
          },
          "osDisk": {
            "caching": "ReadWrite",
            "createOption": "FromImage",
            "managedDisk": {
              "storageAccountType": "Standard_LRS"
            }
          },
          "dataDisks": [   # 这里是给新创建的虚拟机添加一块50G的磁盘
            {
                "name": "alaudadatadisk",
                "lun": 0,
                "caching": "ReadWrite",
                "createOption": "Empty",
                "diskSizeGB": 50,
                "managedDisk": {
                    "storageAccountType": "Premium_LRS"
                }
            }
          ]
        },
        "networkProfile": {
          "networkInterfaces": [
            {
              "id": "[resourceId('Microsoft.Network/networkInterfaces', variables('networkInterfaceName'))]"
            }
          ]
        },
        "diagnosticsProfile": {
          "bootDiagnostics": {
            "enabled": true,
            "storageUri": "[concat('https://', parameters('diagnosticsStorageAccountName'), '.blob.core.chinacloudapi.cn/')]"
          }
        }
      }
    },
    {
        "name": "[parameters('diagnosticsStorageAccountName')]",
        "type": "Microsoft.Storage/storageAccounts",
        "apiVersion": "2017-10-01",
        "location": "[resourceGroup().location]",
        "sku": {
          "name": "[variables('diagnosticsStorageAccountType')]"
        },
        "properties": {}
    },
   {
      "name": "[parameters('virtualNetworkName')]",
      "type": "Microsoft.Network/virtualNetworks",
      "apiVersion": "2018-08-01",
      "location": "[resourceGroup().location]",
      "properties": {
        "addressSpace": {
          "addressPrefixes": [
            "[variables('addressPrefix')]"
          ]
        },
        "subnets": [
          {
            "name": "[parameters('subnetName')]",
            "properties": {
              "addressPrefix": "[variables('subnetPrefix')]",
              "networkSecurityGroup": {
                "id": "[resourceId('Microsoft.Network/networkSecurityGroups', variables('networkSecurityGroupName'))]"
            }
            }
          }
        ]
      },
      "dependsOn": [
        "[resourceId('Microsoft.Network/networkSecurityGroups', variables('networkSecurityGroupName'))]"
      ]
    },
    {
      "name": "[variables('networkInterfaceName')]",
      "type": "Microsoft.Network/networkInterfaces",
      "apiVersion": "2018-08-01",
      "location": "[resourceGroup().location]",
      "dependsOn": [
        "[concat('Microsoft.Network/virtualNetworks/', parameters('virtualNetworkName'))]",
        "[concat('Microsoft.Network/publicIpAddresses/', parameters('publicIpAddressName'))]",
        "[concat('Microsoft.Network/networkSecurityGroups/', variables('networkSecurityGroupName'))]"
      ],
      "properties": {
        "ipConfigurations": [
          {
            "name": "ipconfig1",
            "properties": {
              "subnet": {
                "id": "[variables('subnetRef')]"
              },
              "privateIPAllocationMethod": "Dynamic",
              "publicIpAddress": {
                "id": "[resourceId('Microsoft.Network/publicIpAddresses', parameters('publicIpAddressName'))]"
              }
            }
          }
        ],
        "networkSecurityGroup": {
          "id": "[resourceId('Microsoft.Network/networkSecurityGroups', variables('networkSecurityGroupName'))]"
         }
      }
    },
    {
      "name": "[parameters('publicIpAddressName')]",
      "type": "Microsoft.Network/publicIpAddresses",
      "apiVersion": "2018-02-01",
      "location": "[resourceGroup().location]",
      "properties": {
        "publicIpAllocationMethod": "Static"
      }
    },
    {
      "name": "[variables('networkSecurityGroupName')]",
      "type": "Microsoft.Network/networkSecurityGroups",
      "apiVersion": "2018-02-01",
      "location": "[resourceGroup().location]",
      "properties": {
        "securityRules": [
          {
            "name": "RDP",
            "properties": {
              "priority": 360,
              "protocol": "Tcp",
              "access": "Allow",
              "direction": "Inbound",
              "sourceApplicationSecurityGroups": [],
              "destinationApplicationSecurityGroups": [],
              "sourceAddressPrefix": "*",
              "sourcePortRange": "*",
              "destinationAddressPrefix": "*",
              "destinationPortRange": "22"
            }
          },
          {   # 给新创建的虚拟机开放所需要的端口
            "name": "Tcp-80",
            "properties": {
              "priority": 370,  # 此值为端口的优先级,每个端口的值不一样
              "protocol": "Tcp",
              "access": "Allow",
              "direction": "Inbound",
              "sourceApplicationSecurityGroups": [],
              "destinationApplicationSecurityGroups": [],
              "sourceAddressPrefix": "*",
              "sourcePortRange": "*",
              "destinationAddressPrefix": "*",
              "destinationPortRange": "80"
             }
            },
           {
            "name": "Tcp-443",
            "properties": {
              "priority": 380,
              "protocol": "Tcp",
              "access": "Allow",
              "direction": "Inbound",
              "sourceApplicationSecurityGroups": [],
              "destinationApplicationSecurityGroups": [],
              "sourceAddressPrefix": "*",
              "sourcePortRange": "*",
              "destinationAddressPrefix": "*",
              "destinationPortRange": "443"
            }
          }
        ]
      }
    }
  ],
  "outputs": { 
    "adminUsername": {
      "type": "string",
      "value": "[parameters('adminUsername')]"
    }
  }
}