教程:使用参数文件部 ARM 模板Tutorial: Use parameter files to deploy your ARM template

本教程介绍如何使用参数文件存储在部署过程中传递的值。In this tutorial, you learn how to use parameter files to store the values you pass in during deployment. 在以前的教程中,我们通过部署命令使用了内联参数。In the previous tutorials, you used inline parameters with your deployment command. 此方法适用于测试 Azure 资源管理器模板(ARM 模板),但是当自动执行部署时,可以更容易地为环境传递一组值。This approach worked for testing your Azure Resource Manager template (ARM template), but when automating deployments it can be easier to pass a set of values for your environment. 参数文件可以方便你针对特定的环境将参数值打包。Parameter files make it easier to package parameter values for a specific environment. 在本教程中,我们将针对开发和生产环境创建参数文件。In this tutorial, you'll create parameter files for development and production environments. 完成该过程需要大约 12 分钟It takes about 12 minutes to complete.

先决条件Prerequisites

建议完成有关标记的教程,但这不是必需的。We recommend that you complete the tutorial about tags, but it's not required.

必须已安装带有资源管理器工具扩展的 Visual Studio Code,以及 Azure PowerShell 或 Azure CLI。You must have Visual Studio Code with the Resource Manager Tools extension, and either Azure PowerShell or Azure CLI. 有关详细信息,请参阅模板工具For more information, see template tools.

审阅模板Review template

模板有许多可以在部署过程中提供的参数。Your template has many parameters you can provide during deployment. 在上一教程的末尾,模板如下所示:At the end of the previous tutorial, your template looked like:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storagePrefix": {
      "type": "string",
      "minLength": 3,
      "maxLength": 11
    },
    "storageSKU": {
      "type": "string",
      "defaultValue": "Standard_LRS",
      "allowedValues": [
        "Standard_LRS",
        "Standard_GRS",
        "Standard_RAGRS",
        "Premium_LRS"
      ]
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]"
    },
    "appServicePlanName": {
      "type": "string",
      "defaultValue": "exampleplan"
    },
    "webAppName": {
      "type": "string",
      "metadata": {
        "description": "Base name of the resource such as web app name and app service plan "
      },
      "minLength": 2
    },
    "linuxFxVersion": {
      "type": "string",
      "defaultValue": "php|7.0",
      "metadata": {
        "description": "The Runtime stack of current web app"
      }
    },
    "resourceTags": {
        "type": "object",
        "defaultValue": {
            "Environment": "Dev",
            "Project": "Tutorial"
        }
    }
  },
  "variables": {
    "uniqueStorageName": "[concat(parameters('storagePrefix'), uniqueString(resourceGroup().id))]",
    "webAppPortalName": "[concat(parameters('webAppName'), uniqueString(resourceGroup().id))]"
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2019-04-01",
      "name": "[variables('uniqueStorageName')]",
      "location": "[parameters('location')]",
      "tags": "[parameters('resourceTags')]",
      "sku": {
        "name": "[parameters('storageSKU')]"
      },
      "kind": "StorageV2",
      "properties": {
        "supportsHttpsTrafficOnly": true
      }
    },
    {
      "type": "Microsoft.Web/serverfarms",
      "apiVersion": "2016-09-01",
      "name": "[parameters('appServicePlanName')]",
      "location": "[parameters('location')]",
      "tags": "[parameters('resourceTags')]",
      "sku": {
        "name": "B1",
        "tier": "Basic",
        "size": "B1",
        "family": "B",
        "capacity": 1
      },
      "kind": "linux",
      "properties": {
        "perSiteScaling": false,
        "reserved": true,
        "targetWorkerCount": 0,
        "targetWorkerSizeId": 0
      }
    },
    {
      "type": "Microsoft.Web/sites",
      "apiVersion": "2016-08-01",
      "name": "[variables('webAppPortalName')]",
      "location": "[parameters('location')]",
      "dependsOn": [
        "[parameters('appServicePlanName')]"
      ],
      "tags": "[parameters('resourceTags')]",
      "kind": "app",
      "properties": {
        "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', parameters('appServicePlanName'))]",
        "siteConfig": {
          "linuxFxVersion": "[parameters('linuxFxVersion')]"
        }
      }
    }
  ],
  "outputs": {
    "storageEndpoint": {
      "type": "object",
      "value": "[reference(variables('uniqueStorageName')).primaryEndpoints]"
    }
  }
}

该模板可以正常工作,但现在,你希望能够轻松地管理为模板传入的参数。This template works well, but now you want to easily manage the parameters that you pass in for the template.

添加参数文件Add parameter files

参数文件是 JSON 文件,其结构类似于模板。Parameter files are JSON files with a structure that is similar to your template. 在文件中,我们提供需要在部署过程中传入的参数值。In the file, you provide the parameter values you want to pass in during deployment.

在参数文件中,为模板中的参数提供值。Within the parameter file, you provide values for the parameters in your template. 参数文件中每个参数的名称必须与模板中的参数名称相匹配。The name of each parameter in your parameter file must match the name of a parameter in your template. 该名称不区分大小写,但为了方便查看匹配的值,我们建议你匹配模板中的大小写。The name is case-insensitive but to easily see the matching values we recommend that you match the casing from the template.

无需为每个参数提供值。You don't have to provide a value for every parameter. 如果未指定的参数具有默认值,则在部署过程中将使用该值。If an unspecified parameter has a default value, that value is used during deployment. 如果参数没有默认值,并且未在参数文件中指定值,系统会在部署过程中提示你提供值。If a parameter doesn't have a default value and isn't specified in the parameter file, you're prompted to provide a value during deployment.

不能在参数文件中指定与模板中的参数名称不匹配的参数名称。You can't specify a parameter name in your parameter file that doesn't match a parameter name in the template. 如果提供了未知参数,会收到错误。You get an error when unknown parameters are provided.

在 Visual Studio Code 中,创建包含以下内容的新文件。In Visual Studio Code, create a new file with following content. 使用名称 azuredeploy.parameters.dev.json 保存文件。Save the file with the name azuredeploy.parameters.dev.json.

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storagePrefix": {
      "value": "devstore"
    },
    "storageSKU": {
      "value": "Standard_LRS"
    },
    "appServicePlanName": {
      "value": "devplan"
    },
    "webAppName": {
      "value": "devapp"
    },
    "resourceTags": {
      "value": {
        "Environment": "Dev",
        "Project": "Tutorial"
      }
    }
  }
}

此文件是用于开发环境的参数文件。This file is your parameter file for the development environment. 请注意,它将“Standard_LRS”用于存储帐户,使用“dev”前缀为资源命名,并将 Environment 标记设置为“Dev” 。Notice that it uses Standard_LRS for the storage account, names resources with a dev prefix, and sets the Environment tag to Dev.

同样,创建包含以下内容的新文件。Again, create a new file with the following content. 使用名称 azuredeploy.parameters.prod.json 保存文件。Save the file with the name azuredeploy.parameters.prod.json.

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storagePrefix": {
      "value": "contosodata"
    },
    "storageSKU": {
      "value": "Standard_GRS"
    },
    "appServicePlanName": {
      "value": "contosoplan"
    },
    "webAppName": {
      "value": "contosowebapp"
    },
    "resourceTags": {
      "value": {
        "Environment": "Production",
        "Project": "Tutorial"
      }
    }
  }
}

此文件是用于生产环境的参数文件。This file is your parameter file for the production environment. 请注意,它将“Standard_GRS”用于存储帐户,使用“contoso”前缀为资源命名,并将 Environment 标记设置为“Production” 。Notice that it uses Standard_GRS for the storage account, names resources with a contoso prefix, and sets the Environment tag to Production. 在实际生产环境中,还需要将应用服务与非免费版 SKU 配合使用,但在本教程中,我们将继续使用该免费版 SKU。In a real production environment, you would also want to use an app service with a SKU other than free, but we'll continue to use that SKU for this tutorial.

部署模板Deploy template

使用 Azure CLI 或 Azure PowerShell 来部署模板。Use either Azure CLI or Azure PowerShell to deploy the template.

让我们创建两个新的资源组,对模板进行最终测试。As a final test of your template, let's create two new resource groups. 一个用于开发环境,一个用于生产环境。One for the dev environment and one for the production environment.

对于模板和参数变量,请将 {path-to-the-template-file}{path-to-azuredeploy.parameters.dev.json}{path-to-azuredeploy.parameters.prod.json} 和大括号 {} 替换为你的模板和参数文件路径。For the template and parameter variables, replace {path-to-the-template-file}, {path-to-azuredeploy.parameters.dev.json}, {path-to-azuredeploy.parameters.prod.json}, and the curly braces {} with your template and parameter file paths.

首先,我们部署到开发环境。First, we'll deploy to the dev environment.

$templateFile = "{path-to-the-template-file}"
$parameterFile="{path-to-azuredeploy.parameters.dev.json}"
New-AzResourceGroup `
  -Name myResourceGroupDev `
  -Location "China East"
New-AzResourceGroupDeployment `
  -Name devenvironment `
  -ResourceGroupName myResourceGroupDev `
  -TemplateFile $templateFile `
  -TemplateParameterFile $parameterFile

现在,我们部署到生产环境。Now, we'll deploy to the production environment.

$parameterFile="{path-to-azuredeploy.parameters.prod.json}"
New-AzResourceGroup `
  -Name myResourceGroupProd `
  -Location "China North"
New-AzResourceGroupDeployment `
  -Name prodenvironment `
  -ResourceGroupName myResourceGroupProd `
  -TemplateFile $templateFile `
  -TemplateParameterFile $parameterFile

备注

如果部署失败,请使用 verbose 开关获取有关正在创建的资源的信息。If the deployment failed, use the verbose switch to get information about the resources being created. 使用 debug 开关获取调试的详细信息。Use the debug switch to get more information for debugging.

验证部署Verify deployment

可以通过在 Azure 门户中浏览资源组来验证部署。You can verify the deployment by exploring the resource groups from the Azure portal.

  1. 登录 Azure 门户Sign in to the Azure portal.
  2. 在左侧菜单中选择“资源组”。 From the left menu, select Resource groups.
  3. 可以看到在本教程中部署的两个新资源组。You see the two new resource groups you deployed in this tutorial.
  4. 选择任一资源组,查看部署的资源。Select either resource group and view the deployed resources. 请注意,这些资源与我们在参数文件中为该环境指定的值匹配。Notice that they match the values you specified in your parameter file for that environment.

清理资源Clean up resources

  1. 在 Azure 门户上的左侧菜单中选择“资源组” 。From the Azure portal, select Resource group from the left menu.
  2. 在“按名称筛选”字段中输入资源组名称。Enter the resource group name in the Filter by name field. 如果已完成此系列,则需删除三个资源组:myResourceGroup、myResourceGroupDev 和 myResourceGroupProd 。If you've completed this series, you have three resource groups to delete - myResourceGroup, myResourceGroupDev, and myResourceGroupProd.
  3. 选择资源组名称。Select the resource group name.
  4. 在顶部菜单中选择“删除资源组”。Select Delete resource group from the top menu.

后续步骤Next steps

恭喜!你已完成本简介,知道如何将模板部署到 Azure 了。Congratulations, you've finished this introduction to deploying templates to Azure. 如果你有任何意见和建议,请在反馈部分告知我们。Let us know if you have any comments and suggestions in the feedback section. 谢谢!Thanks!

下一个教程系列将详细介绍如何部署模板。The next tutorial series goes into more detail about deploying templates.