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

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. 

主站蜘蛛池模板: 类乌齐县| 晴隆县| 荥阳市| 清水河县| 大城县| 儋州市| 海安县| 辽中县| 松桃| 中江县| 息烽县| 泽库县| 湄潭县| 句容市| 阿拉尔市| 新宾| 和平县| 图片| 岳西县| 苏尼特右旗| 广西| 天等县| 小金县| 新巴尔虎左旗| 罗城| 安丘市| 张家口市| 宜章县| 潜江市| 深圳市| 凌源市| 宁国市| 司法| 丰县| 永泰县| 巴塘县| 溧水县| 南通市| 大埔区| 麟游县| 怀集县|