练习 - 向 Azure 资源管理器模板添加参数和输出
在本练习中,你将添加一个参数以在部署期间定义 Azure 存储帐户名称。 然后,添加一个参数来定义所允许的存储帐户 SKU,并定义用于此部署的存储帐户 SKU。 还可添加一个稍后可在部署过程中使用的输出,使 Azure 资源管理器模板(ARM 模板)有其他用途。
为 ARM 模板创建参数
此处,通过添加可以在运行时设置的参数,使 ARM 模板更加灵活。 为 storageName 值创建参数。
- 在 Visual Studio Code 中的 azuredeploy.json 文件中,更新 - "parameters":{},,如下所示:- "parameters": { "storageName": { "type": "string", "minLength": 3, "maxLength": 24, "metadata": { "description": "The name of the Azure storage resource" } } },- 若要正确设置 JSON 文件的格式,请按 Alt+Shift+F。 
- 在 - resources和- name值中使用- displayName块中的新参数。 整个文件如以下代码示例所示:- { "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "parameters": { "storageName": { "type": "string", "minLength": 3, "maxLength": 24, "metadata": { "description": "The name of the Azure storage resource" } } }, "functions": [], "variables": {}, "resources": [ { "type": "Microsoft.Storage/storageAccounts", "apiVersion": "2025-01-01", "name": "[parameters('storageName')]", "tags": { "displayName": "[parameters('storageName')]" }, "location": "[resourceGroup().location]", "kind": "StorageV2", "sku": { "name": "Standard_LRS" } } ], "outputs": {} }
- 保存文件。 
部署参数化 ARM 模板
此处,你将更改部署的名称以更好地反映此部署的作用,并填充新参数的值。
在终端中运行以下 Azure CLI 命令。 此脚本与之前使用的脚本相同,但部署名称已更改。 输入参数的唯一值 storageName 。 它必须在 Azure 中全局唯一,包含 3 到 24 个字符,并且仅包含小写字母、数字和连字符。 可以重复使用在上一单元中创建的唯一名称;如果这样做,Azure 将更新现有资源,而不是创建新的资源。
templateFile="azuredeploy.json"
today=$(date +"%d-%b-%Y")
DeploymentName="addnameparameter-"$today
az deployment group create \
  --name $DeploymentName \
  --template-file $templateFile \
  --parameters storageName={your-unique-name}
在终端中运行以下 Azure PowerShell 命令。 此脚本与之前使用的脚本相同,但部署名称已更改。 输入参数的唯一值 storageName 。 它必须在 Azure 中全局唯一,包含 3 到 24 个字符,并且仅包含小写字母、数字和连字符。 可以重复使用在上一单元中创建的唯一名称;如果这样做,Azure 将更新现有资源,而不是创建新的资源。
$templateFile="azuredeploy.json"
$today=Get-Date -Format "MM-dd-yyyy"
$deploymentName="addnameparameter-"+"$today"
New-AzResourceGroupDeployment `
  -Name $deploymentName `
  -TemplateFile $templateFile `
  -storageName {your-unique-name}
检查你的部署
- 部署完成后,返回到浏览器中的 Azure 门户。 转到资源组,可以看到现在有“3 个已成功”的部署。 选择此链接。 - 请注意,列表中包含了所有三个部署。 
- 如之前一样浏览 addnameparameter 部署。 
添加另一个限制允许的值的参数
此处使用参数来限制参数允许的值。
- 添加一个名为 - storageSKU的新参数到- parameters部分的azuredeploy.json文件。- // This is the allowed values for an Azure storage account "storageSKU": { "type": "string", "defaultValue": "Standard_LRS", "allowedValues": [ "Standard_LRS", "Standard_GRS", "Standard_RAGRS", "Standard_ZRS", "Premium_LRS", "Premium_ZRS", "Standard_GZRS", "Standard_RAGZRS" ] }- 第一行是注释。 ARM 模板支持 - //和- /* */注释。
- 更新 resources 以使用 参数。 如果利用 Visual Studio Code 中的 IntelliSense,会使此步骤更简单。 - "sku": { "name": "[parameters('storageSKU')]" }- 整个文件如以下代码示例所示: - { "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "parameters": { "storageName": { "type": "string", "minLength": 3, "maxLength": 24, "metadata": { "description": "The name of the Azure storage resource" } }, "storageSKU": { "type": "string", "defaultValue": "Standard_LRS", "allowedValues": [ "Standard_LRS", "Standard_GRS", "Standard_RAGRS", "Standard_ZRS", "Premium_LRS", "Premium_ZRS", "Standard_GZRS", "Standard_RAGZRS" ] } }, "functions": [], "variables": {}, "resources": [ { "type": "Microsoft.Storage/storageAccounts", "apiVersion": "2025-01-01", "name": "[parameters('storageName')]", "tags": { "displayName": "[parameters('storageName')]" }, "location": "[resourceGroup().location]", "kind": "StorageV2", "sku": { "name": "[parameters('storageSKU')]" } } ], "outputs": {} }
- 保存文件。 
部署 ARM 模板
在此,可使用允许列表中的 storageSKU 参数成功部署。 然后尝试使用不在允许列表中的 storageSKU 参数来部署模板。 第二个部署如预期那样失败。
- 通过运行以下命令部署模板。 填写 - storageName参数的唯一名称。 它必须在 Azure 中全局唯一,包含 3 到 24 个字符,并且仅包含小写字母、数字和连字符。 可以重复使用在上一单元中创建的唯一名称;如果这样做,Azure 将更新现有资源,而不是创建新的资源。- templateFile="azuredeploy.json" today=$(date +"%d-%b-%Y") DeploymentName="addSkuParameter-"$today az deployment group create \ --name $DeploymentName \ --template-file $templateFile \ --parameters storageSKU=Standard_GRS storageName={your-unique-name}- 允许此部署完成。 此部署会如预期那样成功。 允许的值的列表会阻止模板的用户传入对资源无效的参数值。 让我们看看提供无效的 SKU 后会发生什么情况。 
- 运行以下命令以使用不允许的参数来部署模板。 此处,你还将 - storageSKU参数更改为 Basic。 填写- storageName参数的唯一名称。 它必须在 Azure 中全局唯一,包含 3 到 24 个字符,并且仅包含小写字母、数字和连字符。 可以重复使用在上一单元中创建的唯一名称;如果这样做,Azure 将更新现有资源,而不是创建新的资源。- templateFile="azuredeploy.json" today=$(date +"%d-%b-%Y") DeploymentName="addSkuParameter-"$today az deployment group create \ --name $DeploymentName \ --template-file $templateFile \ --parameters storageSKU=Basic storageName={your-unique-name}- 此部署失败。 请注意错误。   
- 通过运行以下命令部署模板。 填写 - storageName参数的唯一名称。 它必须在 Azure 中全局唯一,包含 3 到 24 个字符,并且仅包含小写字母、数字和连字符。 可以重复使用在上一单元中创建的唯一名称;如果这样做,Azure 将更新现有资源,而不是创建新的资源。- $today=Get-Date -Format "MM-dd-yyyy" $deploymentName="addSkuParameter-"+"$today" New-AzResourceGroupDeployment ` -Name $deploymentName ` -TemplateFile $templateFile ` -storageName {your-unique-name} ` -storageSKU Standard_GRS- 允许此部署完成。 此部署会如预期那样成功。 允许的值的列表会阻止模板的用户传入对资源无效的参数值。 让我们看看提供无效的 SKU 后会发生什么情况。 
- 运行以下命令以使用不允许的参数来部署模板。 此处,你还将 - storageSKU参数更改为 Basic。 填写- storageName参数的唯一名称。 它必须在 Azure 中全局唯一,包含 3 到 24 个字符,并且仅包含小写字母、数字和连字符。 可以重复使用在上一单元中创建的唯一名称;如果这样做,Azure 将更新现有资源,而不是创建新的资源。- $today=Get-Date -Format "MM-dd-yyyy" $deploymentName="addSkuParameter-"+"$today" New-AzResourceGroupDeployment ` -Name $deploymentName ` -TemplateFile $templateFile ` -storageName {your-unique-name} ` -storageSKU Basic- 此部署失败。 请注意错误。   
将输出添加到 ARM 模板
此处,你需要添加到 ARM 模板的 outputs 部分以输出存储帐户资源的终结点。
- 在 Visual Studio Code 中的 azuredeploy.json 文件中,更新 - "outputs":{},如下所示:- "outputs": { "storageEndpoint": { "type": "object", "value": "[reference(parameters('storageName')).primaryEndpoints]" } }
- 保存文件。 
使用输出部署 ARM 模板
此处,你将部署模板,并看到采用 JSON 格式的终结点输出。 需要为 storageName 参数填写一个唯一的名称。 它必须在 Azure 中全局唯一,包含 3 到 24 个字符,并且仅包含小写字母、数字和连字符。 可以重复使用在上一单元中创建的唯一名称;如果这样做,Azure 将更新现有资源,而不是创建新的资源。
- 通过运行以下命令部署模板。 请确保将 {your-unique-name} 替换为对你而言唯一的字符串。 - templateFile="azuredeploy.json" today=$(date +"%d-%b-%Y") DeploymentName="addoutputs-"$today az deployment group create \ --name $DeploymentName \ --template-file $templateFile \ --parameters storageSKU=Standard_LRS storageName={your-unique-name}- 请注意输出。   
- 通过运行以下命令部署模板。 请确保将 {your-unique-name} 替换为对你而言唯一的字符串。 - $today=Get-Date -Format "MM-dd-yyyy" $deploymentName="addOutputs-"+"$today" New-AzResourceGroupDeployment ` -Name $deploymentName ` -TemplateFile $templateFile ` -storageName {your-unique-name} ` -storageSKU Standard_LRS- 请注意输出。   
检查你的输出部署
在 Azure 门户中,转到 addOutputs 部署。 还可以在此处找到你的输出。
              
              