练习 - 使用 Azure 资源管理器标记和参数文件
在本练习中,你将添加标记来帮助组织和跟踪Microsoft Azure 资源。 还可以使用 Azure 资源管理器 (ARM) 模板参数文件来允许每个部署的不同参数配置。
本练习使用 适用于 Visual Studio Code 的 Azure 资源管理器工具。 请务必在 Visual Studio Code 中安装此扩展。
注释
本练习为选做练习。 若要完成本练习,则需要在开始之前创建 Azure 订阅。 如果没有 Azure 帐户或不想暂时创建一个帐户,可以通读说明,以便了解所显示的信息。
注释
在本单元中,你将使用 Azure Cloud Shell 作为终端。 可以通过 Azure 门户 或 Cloud Shell 登录访问 Cloud Shell。 无需在电脑或笔记本电脑上安装任何内容即可使用它。
注释
在本练习中,请将示例中的 myResourceGroupName 替换为现有资源组的名称或为此练习创建的资源组的名称。
创建标记以跟踪资源部署环境和项目
首先,创建一个参数以用作模板中的资源标记。
- 在 Visual Studio Code 的 azuredeploy.json 文件中,将光标放在参数的右大括号 - storageSKU后面。 添加逗号并按 Enter。
- 类型 par。 你将看到相关代码片段的列表。 
- 选择 arm-param。 请记住,此作会将泛型参数添加到模板。 如下代码所示: - "parameter1": { "type": "string", "metadata": { "description": "description" }
- 更改为 - parameter1resourceTag, 并将值- "type":更改为 对象。 请记住,参数可以具有字符串、secureString、int、bool、object、secureObject 和数组数据类型。 这些参数类型的示例语法链接在此模块的摘要中。
- 添加名为 defaultValue 的属性: 并将该值设置为 {“Environment”: “Dev”, “Project”: “Tutorial”}。 - 参数块应类似于以下代码: - "parameters": { "storagePrefix": { "type": "string", "minLength": 3, "maxLength": 11 }, "storageSKU": { "type": "string", "defaultValue": "Standard_LRS", "allowedValues": [ "Standard_LRS", "Standard_GRS", "Standard_RAGRS", "Standard_ZRS", "Premium_LRS", "Premium_ZRS", "Standard_GZRS", "Standard_RAGZRS" ] }, "resourceTags": { "type": "object", "defaultValue": { "Environment": "Dev", "Project": "Tutorial" } } },
- 使用此参数标记存储帐户资源。 - tags:更改资源定义中的属性:- "tags": "[parameters('resourceTags')]",
- 文件应如下所示: - { "$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", "Standard_ZRS", "Premium_LRS", "Premium_ZRS", "Standard_GZRS", "Standard_RAGZRS" ] }, "resourceTags": { "type": "object", "defaultValue": { "Environment": "Dev", "Project": "Tutorial" } } }, "functions": [], "variables": { "uniqueStorageName": "[toLower(concat(parameters('storagePrefix'),uniqueString(resourceGroup().id)))]" }, "resources": [{ "name": "[variables('uniqueStorageName')]", "type": "Microsoft.Storage/storageAccounts", "apiVersion": "2019-06-01", "tags": "[parameters('resourceTags')]", "location": "[resourceGroup().location]", "kind": "StorageV2", "sku": { "name": "[parameters('storageSKU')]" } }], "outputs": {} }
- 保存文件。 
使用更新的标记部署 ARM 模板
- 将更新的 ARM 模板部署到 Azure。 请务必使用之前使用的相同 - storagePrefix。- templateFile="azuredeploy.json" today=$(date +"%d-%b-%Y") DeploymentName="updateTags-"$today az deployment group create \ --name $DeploymentName \ --template-file $templateFile \ --parameters storagePrefix={your-Prefix} storageSKU=Standard_LRS
- 将更新的 ARM 模板部署到 Azure。 请务必使用之前使用的相同 - storagePrefix。- $templateFile = "azuredeploy.json" $today=Get-Date -Format "MM-dd-yyyy" $deploymentName="updateTags-"+"$today" New-AzResourceGroupDeployment ` -Name $deploymentName ` -TemplateFile $templateFile ` -storagePrefix {your storagePrefix} ` -storageSKU Standard_LRS
验证新标记是否在部署中
- 在 Azure 中,选择 myResourceGroupName 资源组,然后选择已部署的存储帐户。 
- 请注意 环境:开发人员 和 项目:教程 标记:   
使用参数文件
每次部署此模板时,当前都有三个要填写的参数。 模板的每个用户都可以创建一个文件来保存其参数值。 在这里,你将创建一个要用于模板的参数文件。
- 在 Visual Studio Code 中,创建另一个文件。 azuredeploy.parameters.dev.json调用它。 
- 在此文件中,将添加要输入到开发环境的模板中的模板参数的值。 更改标记值以查看部署是否进行更改。 例如,可以更改为 - projectNameLearn:- { "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#", "contentVersion": "1.0.0.0", "parameters": { "storagePrefix": { "value": "{unique-prefix}" }, "storageSKU": { "value": "Standard_LRS" }, "resourceTags": { "value": { "Environment": "Dev", "Project": "Learn" } } } }
- 请务必替换为 - {unique-prefix}唯一前缀。
- 保存文件。 
使用参数文件部署模板
在本部分中,你将部署 ARM 模板,并指定要使用的参数文件。
- 在 Visual Studio Code 终端中,运行以下 Azure CLI 命令: - templateFile="azuredeploy.json" devParameterFile="azuredeploy.parameters.dev.json" today=$(date +"%d-%b-%Y") DeploymentName="addParameterFile-"$today az deployment group create \ --name $DeploymentName \ --template-file $templateFile \ --parameters $devParameterFile
- 检查 Azure 以确保部署成功,并且标记值已更改:   
- 作为挑战,请为生产环境创建参数文件。 运行命令以部署到生产环境时更改参数文件路径。 
- 在 Visual Studio Code 终端中,运行以下 Azure PowerShell 命令: - $templateFile = "azuredeploy.json" $parameterFile="azuredeploy.parameters.dev.json" $today=Get-Date -Format "MM-dd-yyyy" $deploymentName="addParameterFile-"+"$today" New-AzResourceGroupDeployment ` -Name $deploymentName ` -TemplateFile $templateFile ` -TemplateParameterFile $parameterFile
- 检查 Azure 以确保部署成功,并且标记值已更改:   
- 作为挑战,请为生产环境创建参数文件。 运行命令以部署到生产环境时更改参数文件路径。