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

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. 

主站蜘蛛池模板: 嘉荫县| 山丹县| 南通市| 温州市| 阳东县| 西和县| 西华县| 黄石市| 海林市| 安化县| 云梦县| 万荣县| 利川市| 嘉荫县| 南岸区| 卫辉市| 大洼县| 安吉县| 岳普湖县| 咸丰县| 柯坪县| 顺昌县| 民县| 宁陵县| 正宁县| 临城县| 昌平区| 达尔| 宁陕县| 尼木县| 安远县| 大田县| 松溪县| 外汇| 巴林左旗| 喀什市| 闽清县| 承德县| 进贤县| 洛阳市| 湄潭县|