教程:使用 ARM 模板部署虚拟机扩展Tutorial: Deploy virtual machine extensions with ARM templates

了解如何使用 Azure 虚拟机扩展在 Azure VM 上执行部署后配置和自动化任务。Learn how to use Azure virtual machine extensions to perform post-deployment configuration and automation tasks on Azure VMs. 有许多不同的 VM 扩展可与 Azure VM 配合使用。Many different VM extensions are available for use with Azure VMs. 在本教程中,你将从 Azure 资源管理器 (ARM) 模板部署自定义脚本扩展,以便在 Windows VM 上运行 PowerShell 脚本。In this tutorial, you deploy a Custom Script extension from an Azure Resource Manager (ARM) template to run a PowerShell script on a Windows VM. 此脚本在 VM 上安装 Web 服务器。The script installs Web Server on the VM.

本教程涵盖以下任务:This tutorial covers the following tasks:

  • 准备 PowerShell 脚本Prepare a PowerShell script
  • 打开快速入门模板Open a quickstart template
  • 编辑模板Edit the template
  • 部署模板Deploy the template

如果没有 Azure 订阅,请在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.

先决条件Prerequisites

若要完成本文,需要做好以下准备:To complete this article, you need:

准备 PowerShell 脚本Prepare a PowerShell script

可以使用内联 PowerShell 脚本或脚本文件。You can use inline PowerShell script or a script file. 本教程介绍如何使用脚本文件。This tutorial shows how to use a script file. GitHub 共享包含以下内容的 PowerShell 脚本:A PowerShell script with the following content is shared from GitHub:

Install-WindowsFeature -name Web-Server -IncludeManagementTools

如果选择将文件发布到你自己的位置,请在本教程的后面部分更新模板中的 fileUri 元素。If you choose to publish the file to your own location, update the fileUri element in the template later in the tutorial.

打开快速入门模板Open a quickstart template

Azure 快速入门模板是 ARM 模板的存储库。Azure Quickstart Templates is a repository for ARM templates. 无需从头开始创建模板,只需找到一个示例模板并对其自定义即可。Instead of creating a template from scratch, you can find a sample template and customize it. 本教程中使用的模板称为部署简单的 Windows VMThe template used in this tutorial is called Deploy a simple Windows VM.

  1. 在 Visual Studio Code 中,选择“文件” > “打开文件”。 In Visual Studio Code, select File > Open File.

  2. 在“文件名”框中粘贴以下 URL:In the File name box, paste the following URL:

    https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/101-vm-simple-windows/azuredeploy.json
    
  3. 若要打开该文件,请选择“打开”。To open the file, select Open. 该模板定义五个资源:The template defines five resources:

    • Microsoft.Storage/storageAccounts.Microsoft.Storage/storageAccounts.
    • Microsoft.Network/publicIPAddresses.Microsoft.Network/publicIPAddresses.
    • Microsoft.Network/networkSecurityGroups.Microsoft.Network/networkSecurityGroups.
    • Microsoft.Network/virtualNetworks.Microsoft.Network/virtualNetworks.
    • Microsoft.Network/networkInterfaces.Microsoft.Network/networkInterfaces.
    • Microsoft.Compute/virtualMachines.Microsoft.Compute/virtualMachines.

    在自定义模板之前,不妨对其进行一些基本的了解。It's helpful to get some basic understanding of the template before you customize it.

  4. 选择“文件” > “另存为”,使用文件名 azuredeploy.json 将该文件的副本保存到本地计算机。 Save a copy of the file to your local computer with the name azuredeploy.json by selecting File > Save As.

编辑模板Edit the template

将虚拟机扩展资源添加到包含以下内容的现有模板:Add a virtual machine extension resource to the existing template with the following content:

{
  "type": "Microsoft.Compute/virtualMachines/extensions",
  "apiVersion": "2019-12-01",
  "name": "[concat(variables('vmName'),'/', 'InstallWebServer')]",
  "location": "[parameters('location')]",
  "dependsOn": [
      "[concat('Microsoft.Compute/virtualMachines/',variables('vmName'))]"
  ],
  "properties": {
      "publisher": "Microsoft.Compute",
      "type": "CustomScriptExtension",
      "typeHandlerVersion": "1.7",
      "autoUpgradeMinorVersion":true,
      "settings": {
        "fileUris": [
          "https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/tutorial-vm-extension/installWebServer.ps1"
        ],
        "commandToExecute": "powershell.exe -ExecutionPolicy Unrestricted -File installWebServer.ps1"
      }
  }
}

下面是一些重要元素:The following are some important elements:

  • 名称:由于扩展资源是虚拟机对象的子资源,因此其名称必须有虚拟机名称前缀。name: Because the extension resource is a child resource of the virtual machine object, the name must have the virtual machine name prefix. 请参阅设置子资源的名称和类型See Set name and type for child resources.
  • dependsOn:在创建虚拟机以后创建扩展资源。dependsOn: Create the extension resource after you've created the virtual machine.
  • fileUris:存储脚本文件的位置。fileUris: The locations where the script files are stored. 如果不使用提供的位置,则需更新这些值。If you choose not to use the provided location, you need to update the values.
  • commandToExecute:此命令调用脚本。commandToExecute: This command invokes the script.

若要使用内联脚本,请删除“fileUris”并将“commandToExecute”更新为:To use inline script, remove fileUris, and update commandToExecute to:

powershell.exe Install-WindowsFeature -name Web-Server -IncludeManagementTools && powershell.exe remove-item 'C:\\inetpub\\wwwroot\\iisstart.htm' && powershell.exe Add-Content -Path 'C:\\inetpub\\wwwroot\\iisstart.htm' -Value $('Hello World from ' + $env:computername)

此内联脚本还更新 iisstart.html 内容。This inline script also update the iisstart.html content.

还必须打开 HTTP 端口,以便访问 Web 服务器。You must also open the HTTP port so that you would be able to access the web server.

  1. 在模板中找到 securityRulesFind securityRules in the template.

  2. 将以下规则添加到 default-allow-3389 旁边。Add the following rule next to default-allow-3389.

    {
      "name": "AllowHTTPInBound",
      "properties": {
        "priority": 1010,
        "access": "Allow",
        "direction": "Inbound",
        "destinationPortRange": "80",
        "protocol": "Tcp",
        "sourcePortRange": "*",
        "sourceAddressPrefix": "*",
        "destinationAddressPrefix": "*"
      }
    }
    

部署模板Deploy the template

有关部署过程,请参阅“部署模板”部分,文档为教程:创建包含依赖资源的 ARM 模板For the deployment procedure, see the "Deploy the template" section of Tutorial: Create ARM templates with dependent resources. 建议使用为虚拟机管理员帐户生成的密码。We recommended that you use a generated password for the virtual machine administrator account. 请参阅本文的先决条件部分。See this article's Prerequisites section.

在本地 Shell 中,运行以下命令以检索 VM 的公共 IP 地址:From the local Shell, run the following command to retrieve the public IP address of the VM:

(Get-AzPublicIpAddress -ResourceGroupName $resourceGroupName).IpAddress

将该 IP 地址粘贴到 Web 浏览器中。Paste the IP address into a Web browser. 此时会打开默认的 Internet Information Services (IIS) 欢迎页:The default Internet Information Services (IIS) welcome page opens:

Internet Information Services 欢迎页

清理资源Clean up resources

不再需要部署的 Azure 资源时,请通过删除资源组将其清除。When you no longer need the Azure resources you deployed, clean them up by deleting the resource group.

  1. 在 Azure 门户的左窗格中选择“资源组”。In the Azure portal, in the left pane, select Resource group.
  2. 在“按名称筛选”框中输入资源组名称。In the Filter by name box, enter the resource group name.
  3. 选择资源组名称。Select the resource group name. 将显示资源组中的六个资源。Six resources are displayed in the resource group.
  4. 在顶部菜单中选择“删除资源组”。In the top menu, select Delete resource group.

后续步骤Next steps

在本教程中,你已部署虚拟机和虚拟机扩展。In this tutorial, you deployed a virtual machine and a virtual machine extension. 此扩展在虚拟机上安装了 IIS Web 服务器。The extension installed the IIS web server on the virtual machine. 若要了解如何使用 Azure SQL 数据库扩展导入 BACPAC 文件,请参阅:To learn how to use the Azure SQL Database extension to import a BACPAC file, see: