Observação
O acesso a essa página exige autorização. Você pode tentar entrar ou alterar diretórios.
O acesso a essa página exige autorização. Você pode tentar alterar os diretórios.
本教程介绍如何使用 参数文件 来存储部署期间传入的值。 在前面的教程中,你对部署命令使用了内联参数。 此方法适用于测试 Azure 资源管理器模板(ARM 模板),但在自动部署时,可以更轻松地为环境传递一组值。 通过参数文件,可以更轻松地打包特定环境的参数值。 在本教程中,你将为开发和生产环境创建参数文件。 此说明需要 12 分钟 才能完成。
先决条件
建议完成 有关标记的教程,但这不是必需的。
需要具有 Visual Studio Code 和 Azure PowerShell 或 Azure CLI。 有关详细信息,请参阅 模板工具。
查看模板
模板有许多可以在部署期间提供的参数。 在上一教程结束时,模板具有以下 JSON 文件:
{
"$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": "2025-06-01",
"name": "[variables('uniqueStorageName')]",
"location": "[parameters('location')]",
"tags": "[parameters('resourceTags')]",
"sku": {
"name": "[parameters('storageSKU')]"
},
"kind": "StorageV2",
"properties": {
"supportsHttpsTrafficOnly": true
}
},
{
"type": "Microsoft.Web/serverfarms",
"apiVersion": "2025-03-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": "2025-03-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]"
}
}
}
此模板效果良好,但现在想要轻松管理为模板传入的参数。
添加参数文件
参数文件是 JSON 文件,其结构类似于模板。 在文件中,提供要在部署期间传入的参数值。
在参数文件中,为模板中的参数提供值。 参数文件中每个参数的名称需要与模板中的参数的名称匹配。 名称不区分大小写,但为了更容易查看匹配的值,我们建议您遵循模板中的大小写格式。
无需为每个参数提供值。 如果未指定参数具有默认值,则该值在部署期间使用。 如果参数没有默认值,并且未在参数文件中指定,系统会提示你在部署期间提供值。
不能在参数文件中指定与模板中的参数名称不匹配的参数名称。 提供未知参数时会收到错误。
在 Visual Studio Code 中,创建包含以下内容的新文件。 使用名称 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"
}
}
}
}
此文件是开发环境的参数文件。 请注意,它对存储帐户使用 Standard_LRS,将具有 dev 前缀的资源命名,并将 Environment 标记设置为 Dev。
同样,创建包含以下内容的新文件。 使用名称 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"
}
}
}
}
此文件是生产环境的参数文件。 请注意,它对存储帐户使用Standard_GRS,将具有 contoso 前缀的资源命名,并将标记设置为Environment“生产”。 在实际生产环境中,你可能还希望使用具有非免费 SKU 的应用服务,但我们在本教程中使用该 SKU。
部署模板
使用 Azure CLI 或 Azure PowerShell 部署模板。
作为模板的最终测试,让我们为开发环境创建两个新的资源组,一个用于生产环境。
对于模板和参数变量,请将{path-to-the-template-file}{path-to-azuredeploy.parameters.dev.json}{path-to-azuredeploy.parameters.prod.json}大括号{}替换为模板和参数文件路径。
首先,让我们部署到开发环境。
$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
现在,我们将部署到生产环境。
$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 开关获取有关要创建的资源的信息。 使用 debug 开关获取调试的详细信息。
验证部署
可以通过从 Azure 门户浏览资源组来验证部署。
- 登录到 Azure 门户。
- 在左侧菜单中,选择 “资源组”。
- 你将看到本教程中部署的两个新资源组。
- 选择任一资源组并查看已部署的资源。 请注意,它们与在该环境的参数文件中指定的值匹配。
清理资源
在 Azure 门户中,从左侧菜单中选择 资源组 。
选中复选框旁边的超链接资源组名称。 如果完成此系列,则有三个要删除的资源组 - myResourceGroup、 myResourceGroupDev 和 myResourceGroupProd。
从顶部菜单中选择 “删除资源组 ”图标。
注意
删除资源组的操作不可逆。
在显示并选择“ 删除”的弹出窗口中键入资源组名称。
后续步骤
祝贺。 你已完成 Azure 模板部署介绍的学习。 如果你在反馈部分中有任何评论和建议,请告知我们。
下一个教程系列更详细地介绍了如何部署模板。