官术网_书友最值得收藏!

azuredeploy.json

This is the file that contains the code version of the VM being deployed.  It is made up of parameters, which are provided or overwritten by the preceding parameters file.  The JSON defines three resources: a VNET, a VM, and a NIC resource.  Let's look at how the resources in this file are added and how the sections are broken down. To add resources, you just need to right-click on the resource element in Visual Studio, as you can see in the following screenshot:

Adding a resource to an ARM Template in Visual Studio

The parameters are the input to the template and are the values that are provided by the parameters file:

It is a good practice to provide a meaningful description. Default values can also be provided if they are optional values.

Let's take a look at the following code:

{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"adminUsername": {
"type": "string",
"metadata": {
"description": "The name of the administrator account of the new VM and domain"
}
},
"adminPassword": {
"type": "securestring",
"metadata": {
"description": "The password for the administrator account of the new VM and domain"
}
},
"PrivateSubnet": {
"type": "string",
"defaultValue": "10.0.1.0/24",
"metadata": { "description": "Private VNet IP Subnet" }
}
},

The variables section is for variables within the template that can leverage functions. As you can see, in the following code, we are using the split function to break apart the PrivateSubnet to help create unique names for our VMs:

"variables": {
"GroupStart": "[split(parameters('PrivateSubnet'), '.')[0]]",
"GroupMain": "[split(parameters('PrivateSubnet'), '.')[1]]",
"GroupNumber": "[split(parameters('PrivateSubnet'), '.')[2]]",
"instWin1": "[concat(variables('GroupMain'),'-',variables('GroupNumber'),'-Win1')]",
"instWin1NIC": "[concat(variables('GroupMain'),'-',variables('GroupNumber'),'-Win1NIC')]",
"NetworkVNetID": "[resourceId('Microsoft.Network/virtualNetworks', 'NetworkVNet')]",
"PrivateSubnetName": "PrivateSubnet",
"PrivateSubnetRef": "[concat(variables('NetworkVNetID'), '/subnets/', variables('PrivateSubnetName'))]"
},

In the resources section, we define the resource the template will deploy. In this scenario, we are deploying VNet, NIC, and VM resources. The VM will use the NIC card to connect to the VNet. The VNet is the virtual network that the VM will use to communicate to other resources deployed on the VNet. The following code explains how the NIC card is built into the ARM template:

  "resources": [
{
"apiVersion": "2016-03-30",
"type": "Microsoft.Network/networkInterfaces",
"name": "[variables('instWin1NIC')]",
"location": "[resourceGroup().location]",
"dependsOn": [
"[resourceId('Microsoft.Network/virtualNetworks', 'NetworkVNet')]"
],
"properties": {
"ipConfigurations": [
{
"name": "ipconfig1",
"properties": {
"privateIPAllocationMethod": "Static",
"privateIPAddress": "[concat(variables('GroupStart'),'.',variables('GroupMain'),'.',variables('GroupNumber'),'.31')]",
"subnet": {
"id": "[variables('PrivateSubnetRef')]"
}
}
}
]
}
}

Now, let's look at VM resource creation and how we add the NIC resource we created previously to the VM.  As you can see, in the following code, the NIC is applied as the network interface resource for the VM:

{
"apiVersion": "2016-04-30-preview",
"type": "Microsoft.Compute/virtualMachines",
"name": "[variables('instWin1')]",
"location": "[resourceGroup().location]",
"dependsOn": [
"[resourceId('Microsoft.Network/networkInterfaces/', variables('instWin1NIC'))]"
],
"properties": {
"hardwareProfile": { "vmSize": "Standard_DS1" },
"osProfile": {
"computerName": "[variables('instWin1')]",
"adminUsername": "[parameters('adminUsername')]",
"adminPassword": "[parameters('adminPassword')]"
},
"storageProfile": {
"imageReference": {
"publisher": "MicrosoftWindowsServer",
"offer": "WindowsServer",
"sku": "2012-R2-Datacenter",
"version": "latest"
},
"osDisk": {
"createOption": "FromImage",
"caching": "ReadWrite"
}
},
"networkProfile": {
"networkInterfaces": [
{
"id": "[resourceId('Microsoft.Network/networkInterfaces',variables('instWin1NIC'))]"
}
]
}
}
}
]
}

As you can see, the ARM template helps us create a repeatable and configurable process. However, let's look at taking things a bit further. One of the biggest things folks realize is that there are other resources like firewalls, routers, and load balancers that are required when you choose to manage your applications in virtual machines.  While this may be a choice that you may not be able to avoid, it also requires you to update the OS and Service Packs. 

主站蜘蛛池模板: 巢湖市| 隆林| 法库县| 巢湖市| 通海县| 长治县| 高淳县| 吉木萨尔县| 宜兰市| 肥西县| 江陵县| 独山县| 哈密市| 桃园县| 都兰县| 卢氏县| 旺苍县| 噶尔县| 昔阳县| 怀柔区| 南安市| 镇江市| 饶阳县| 迁安市| 高清| 阿拉善右旗| 衢州市| 诏安县| 赣州市| 东光县| 常宁市| 建阳市| 呼图壁县| 镇巴县| 永顺县| 门源| 高要市| 漳州市| 武山县| 吉木乃县| 竹溪县|