如何使用 ARM 模板创建 Azure 虚拟机





5.00/5 (1投票)
详细说明如何使用 PowerShell 中的 ARM 模板创建 Azure 虚拟机,重点介绍从设置 ARM 模板到部署它的步骤
今天让我们使用 ARM 创建相同的虚拟机。
首先,启动 PowerShell 控制台并登录到您的订阅
Login-AzureRmAccount
1. 创建您的 ARM 模板文件
启动您最喜欢的编辑器,Visual Studio Code 或 Visual Studio 本身是我的两个选择。
创建一个新的 JSON 文件并将其命名为 new-vm.json
将以下结构复制并粘贴到新文件中。这是我们 ARM 模板的开始。
{
"$schema" "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion" "1.0.0.0",
"parameters" {
},
"variables" {
},
"resources" [
],
"outputs" {
}
}
2. 创建 PowerShell 脚本,该脚本将运行我们的 ARM 模板
当然,这可以在 PowerShell 命令行中完成,但创建脚本文件并能够多次运行它要快得多,也更可重复,而无需手动输入这些内容。
创建一个名为 deploy-new-vm.ps1 的新文件
3. 创建资源组
就像上次一样,我们需要创建一个资源组,这是对我们要创建的所有元素进行分组的逻辑位置。这需要在 PowerShell 中完成,所以让我们将以下内容添加到我们的新的 deploy-new-vm.ps1 文件中。
我们还需要选择要将此新资源组放入的订阅,您可以使用 Get-AzureRmSubscription
命令获取所有订阅的列表,选择您想要的并复制 Id 并将其输入到下面的 $subscriptionId
中
$subscriptionId <span class="o">= "<YOUR_SUBSCRIPTION_ID>"
Select-AzureRmSubscription -SubscriptionId $subscriptionId
$resourceGroup <span class="o">= "test-infra"
$location <span class="o">= "North Europe"
New-AzureRmResourceGroup -Name $resourceGroup -Location $location
4. 创建存储帐户
最后,我们可以添加到我们新的 ARM 模板中的内容。
首先,将以下参数添加到模板的“parameters
”部分
"parameters" {
"storageName" {
"type" "string",
"defaultValue" "teststorage",
"metadata" {
"description" "Name for our storage account"
}
}
}
现在添加一个新资源,即我们的存储帐户。
"resources" [
{
"name" "[concat(parameters('storageName'), uniqueString(resourceGroup().id))]",
"type" "Microsoft.Storage/storageAccounts",
"location" "[resourceGroup().location]",
"apiVersion" "2015-06-15",
"properties" {
"accountType" "Standard_LRS"
}
}
]
这与我们在上一篇文章中创建的存储相同。
我们使用 storageName
这是一个参数,我们添加一个唯一的字符串,该字符串由 uniqueString(resourceGroup().id)
生成,这意味着我们可以多次运行它,并且我们不会与 Azure 云中其他人的存储帐户冲突。
位置由 resourcegroup
所在的位置定义,使用 [resourceGroup().location]
。
存储帐户类型仍然是标准本地冗余存储。
完整的文件现在将如下所示
{
"$schema" "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion" "1.0.0.0",
"parameters" {
"storageName" {
"type" "string",
"defaultValue" "teststorage",
"metadata" {
"description" "Name for our storage account"
}
}
},
"variables" {
},
"resources" [
{
"name" "[concat(parameters('storageName'), uniqueString(resourceGroup().id))]",
"type" "Microsoft.Storage/storageAccounts",
"location" "[resourceGroup().location]",
"apiVersion" "2015-06-15",
"properties" {
"accountType" "Standard_LRS"
}
}
],
"outputs" {
}
}
5. 创建虚拟网络
虚拟机需要存在于虚拟网络中,因此我们在 ARM 模板中创建一个新资源。
{
"name" "test-net",
"type" "Microsoft.Network/virtualNetworks",
"location" "[resourceGroup().location]",
"apiVersion" "2015-06-15",
"properties" {
"addressSpace" {
"addressPrefixes" [
"10.0.0.0/16"
]
},
"subnets" [
{
"name" "frontendSubnet",
"properties" {
"addressPrefix" "10.0.1.0/24"
}
}
]
}
}
6. 设置 IP 地址和网络接口
我们需要为我们的新虚拟机提供网络接口和 IP 地址。这可以是静态的,但我们将其保留为动态的,就像上一篇文章一样。
为我们的公共 IP 地址创建一个新资源,我们将使用它来 RDP 进入 VM。
{
"apiVersion" "2016-03-30",
"type" "Microsoft.Network/publicIPAddresses",
"name" "test-publicip",
"location" "[resourceGroup().location]",
"properties" {
"publicIPAllocationMethod" "Dynamic"
}
}
让我们为我们的 VM 名称创建一个参数。
"vmName" {
"type" "string",
"defaultValue" "testvm1",
"metadata" {
"description" "Name for our Virtual Machine"
}
}
现在让我们为我们的新网络接口创建一个新资源。
{
"name" "[concat(parameters('vmName'),'-nic0')]",
"type" "Microsoft.Network/networkInterfaces",
"location" "[resourceGroup().location]",
"apiVersion" "2017-06-01",
"properties" {
"ipConfigurations" [
{
"name" "ipconfig",
"properties" {
"privateIPAllocationMethod" "Dynamic",
"publicIPAddress" {
"id" "[resourceId('Microsoft.Network/publicIPAddresses',
'test-publicip')]"
},
"subnet" {
"id" "[concat(resourceId('Microsoft.Network/virtualNetworks',
'test-net'),'/subnets/','frontendSubnet')]"
}
}
}
]
}
}
您可以在此处看到,我们正在使用 concat
关键字为我们的网络接口创建一个名称,该名称是 VM 名称的前缀,然后添加 -nic0
。
7. 创建我们的虚拟机
{
"name" "[concat(parameters('vmName'))]",
"type" "Microsoft.Compute/virtualMachines",
"location" "[resourceGroup().location]",
"apiVersion" "2017-03-30",
"dependsOn" [
"[concat('Microsoft.Network/networkInterfaces/',parameters('vmName'),'-nic0')]"
],
"properties" {
"hardwareProfile" {
"vmSize" "Basic_A1"
},
"osProfile" {
"computerName" "[parameters('vmName')]",
"adminUsername" "YOURUSERNAME",
"adminPassword" "YOUR_PASSWORD12345678"
},
"storageProfile" {
"imageReference" {
"publisher" "MicrosoftWindowsServer",
"offer" "WindowsServer",
"sku" "2012-R2-Datacenter",
"version" "latest"
},
"osDisk" {
"osType" "Windows",
"name" "[concat(parameters('vmName'),'-','osdisk')]",
"createOption" "FromImage",
"caching" "ReadWrite"
}
},
"networkProfile" {
"networkInterfaces" [
{
"id" "[concat(resourceId('Microsoft.Network/networkInterfaces',
concat(parameters('vmName'))),'-nic0')]"
}
]
}
}
}
此资源中的 dependsOn
值意味着它将等待直到指定资源的配置完成,然后才开始创建此当前资源。您可以依赖多个资源。在这种情况下,我们依赖于网络接口的设置,然后再继续创建虚拟机。
8. 部署 ARM 模板
让我们将 ARM 模板的部署添加到我们的 deploy-new-vm.ps1
脚本中。
New-AzureRmResourceGroupDeployment `
-Name test-infra-deployment `
-ResourceGroupName $resourceGroup `
-TemplateFile new-vm.json `
-Verbose -Force
完整的解决方案
我们的最终 ARM 模板 json 文件 (new-vm.json) 看起来像这样
{
"$schema" "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion" "1.0.0.0",
"parameters" {
"storageName" {
"type" "string",
"defaultValue" "teststorage",
"metadata" {
"description" "Name for our storage account"
}
},
"vmName" {
"type" "string",
"defaultValue" "testvm1",
"metadata" {
"description" "Name for our Virtual Machine"
}
}
},
"variables" {
},
"resources" [
{
"name" "[concat(parameters('storageName'), uniqueString(resourceGroup().id))]",
"type" "Microsoft.Storage/storageAccounts",
"location" "[resourceGroup().location]",
"apiVersion" "2015-06-15",
"properties" {
"accountType" "Standard_LRS"
}
},
{
"name" "test-net",
"type" "Microsoft.Network/virtualNetworks",
"location" "[resourceGroup().location]",
"apiVersion" "2015-06-15",
"properties" {
"addressSpace" {
"addressPrefixes" [
"10.0.0.0/16"
]
},
"subnets" [
{
"name" "frontendSubnet",
"properties" {
"addressPrefix" "10.0.1.0/24"
}
}
]
}
},
{
"apiVersion" "2016-03-30",
"type" "Microsoft.Network/publicIPAddresses",
"name" "test-publicip",
"location" "[resourceGroup().location]",
"properties" {
"publicIPAllocationMethod" "Dynamic"
}
},
{
"name" "[concat(parameters('vmName'),'-nic0')]",
"type" "Microsoft.Network/networkInterfaces",
"location" "[resourceGroup().location]",
"apiVersion" "2017-06-01",
"properties" {
"ipConfigurations" [
{
"name" "ipconfig",
"properties" {
"privateIPAllocationMethod" "Dynamic",
"publicIPAddress" {
"id" "[resourceId('Microsoft.Network/publicIPAddresses','test-publicip')]"
},
"subnet" {
"id" "[concat(resourceId('Microsoft.Network/virtualNetworks', 'test-net'),'/subnets/','frontendSubnet')]"
}
}
}
]
}
},
{
"name" "[concat(parameters('vmName'))]",
"type" "Microsoft.Compute/virtualMachines",
"location" "[resourceGroup().location]",
"apiVersion" "2017-03-30",
"dependsOn" [
"[concat('Microsoft.Network/networkInterfaces/',parameters('vmName'),'-nic0')]"
],
"properties" {
"hardwareProfile" {
"vmSize" "Basic_A1"
},
"osProfile" {
"computerName" "[parameters('vmName')]",
"adminUsername" "YOURUSERNAME",
"adminPassword" "Your_PASSWORD12345678"
},
"storageProfile" {
"imageReference" {
"publisher" "MicrosoftWindowsServer",
"offer" "WindowsServer",
"sku" "2012-R2-Datacenter",
"version" "latest"
},
"osDisk" {
"osType" "Windows",
"name" "[concat(parameters('vmName'),'-','osdisk')]",
"createOption" "FromImage",
"caching" "ReadWrite"
}
},
"networkProfile" {
"networkInterfaces" [
{
"id" "[concat(resourceId('Microsoft.Network/networkInterfaces',
concat(parameters('vmName'))),'-nic0')]"
}
]
}
}
}
],
"outputs" {
}
}
以及我们的 Powershell 文件 (deploy-new-vm.ps1)
$subscriptionId = "<YOUR_SUBSCRIPTION_ID>"
Select-AzureRmSubscription -SubscriptionId $subscriptionId
$resourceGroup = "test-infra"
$location = "North Europe"
New-AzureRmResourceGroup -Name $resourceGroup -Location $location
New-AzureRmResourceGroupDeployment `
-Name test-infra-deployment `
-ResourceGroupName $resourceGroup `
-TemplateFile new-vm.json `
-Verbose -Force
部署它!
启动 PowerShell 提示符并确保您已经运行了 Login-AzureRmAccount
,浏览到我们的两个新文件所在的文件夹并运行它们。
cd c:\dev\Create-VM-with-ARM\
.\deploy-new-vm.ps1
Github
源文件可在 Github 上获取。