练习 - 向 Azure 资源管理器模板添加参数和输出

已完成

在本练习中,你将添加一个参数,在部署期间定义 Azure 存储帐户名称。 然后,添加一个参数来定义所允许的存储帐户 SKU,并定义用于此部署的存储帐户 SKU。 还可添加一个稍后可在部署过程中使用的输出,使 Azure 资源管理器模板(ARM 模板)有其他用途。

为 ARM 模板创建参数

在这里,你将添加可在运行时设置的参数,使 ARM 模板更加灵活。 为 storageName 值创建参数。

  1. 在 Visual Studio Code 中,将光标放在 azuredeploy.json 文件中 parameters 属性的大括号内。 如下所示:"parameters":{},

  2. 选择 Enter,然后输入“par”。 你将看到相关代码片段的列表。 选择 new-parameter,将泛型参数添加到模板。 它的外观如下所示:

     "parameters": {
        "parameter1": {
        "type": "string",
        "metadata": {
            "description": "description"
        }
      }
    },
    
  3. 将参数从 parameter1 更改为 storageName,并将类型保留为字符串。 添加“3”的“minLength”值和“24”的“maxLength”值。 添加“The name of the Azure storage resource”的说明值。

  4. 参数块应如下所示:

    "parameters": {
      "storageName": {
        "type": "string",
        "minLength": 3,
        "maxLength": 24,
        "metadata": {
          "description": "The name of the Azure storage resource"
        }
      }
    },
    
  5. namedisplayName 值中使用 resources 块中的新参数。 整个文件如下所示:

    {
      "$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": [
        {
          "name": "[parameters('storageName')]",
          "type": "Microsoft.Storage/storageAccounts",
          "apiVersion": "2019-06-01",
          "tags": {
            "displayName": "[parameters('storageName')]"
          },
          "location": "[resourceGroup().location]",
          "kind": "StorageV2",
          "sku": {
            "name": "Standard_LRS",
            "tier": "Standard"
          }
        }
      ],
      "outputs": {}
    }
    
  6. 保存文件。

部署参数化 ARM 模板

在这里,你将更改部署的名称以更好地反映此部署的作用,并填充新参数的值。

在终端中运行以下 Azure CLI 命令。 此代码片段与之前使用的代码相同,但部署的名称已更改。 填写 storageName 参数的唯一名称。 记住,该名称在所有 Azure 中必须是唯一的。 可使用在上一单元中创建的唯一名称。 在这种情况下,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 中必须是唯一的。 可使用在上一单元中创建的唯一名称。 在这种情况下,Azure 将更新资源,而不是创建新资源。

$templateFile="azuredeploy.json"
$today=Get-Date -Format "MM-dd-yyyy"
$deploymentName="addnameparameter-"+"$today"
New-AzResourceGroupDeployment `
  -Name $deploymentName `
  -TemplateFile $templateFile `
  -storageName {your-unique-name}

检查你的部署

  1. 部署完成后,返回到浏览器中的 Azure 门户。 转到资源组,可以看到现在有“3 个已成功”的部署。 选择此链接。

    请注意,列表中包含了所有三个部署。

  2. 如之前一样浏览 addnameparameter 部署。

添加另一个参数来限制允许的值

在此处,使用参数来限制参数允许的值。

  1. 将光标置于 storageName 参数的右大括号后面。 添加一个逗号,并选择 Enter

  2. 再次输入“par”,然后选择“new-parameter”。

  3. 将新泛型参数更改为以下内容:

    "storageSKU": {
       "type": "string",
       "defaultValue": "Standard_LRS",
       "allowedValues": [
         "Standard_LRS",
         "Standard_GRS",
         "Standard_RAGRS",
         "Standard_ZRS",
         "Premium_LRS",
         "Premium_ZRS",
         "Standard_GZRS",
         "Standard_RAGZRS"
       ]
     }
    

    此处,你正在列出此参数将允许的值。 如果使用不允许的值运行模板,部署则将失败。

  4. 向此参数添加一个注释。

    Screenshot of the azuredeploy.json file showing the comment This is the allowed values for an Azure storage account in the line preceding the storageSKU parameter.

    ARM 模板支持 ///* */ 注释。

  5. 更新 resources 以使用 storageSKU 参数。 利用 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": [
        {
          "name": "[parameters('storageName')]",
          "type": "Microsoft.Storage/storageAccounts",
          "apiVersion": "2019-06-01",
          "tags": {
            "displayName": "[parameters('storageName')]"
          },
          "location": "[resourceGroup().location]",
          "kind": "StorageV2",
          "sku": {
            "name": "[parameters('storageSKU')]",
            "tier": "Standard"
          }
        }
      ],
      "outputs": {}
    }
    
  6. 保存文件。

部署 ARM 模板

在此,你将使用允许列表中的 storageSKU 参数成功部署。 然后,你将尝试使用不在允许列表中的 storageSKU 参数来部署模板。 第二个部署将如预期那样失败。

  1. 运行以下命令来部署模板。 填写 storageName 参数的唯一名称。 记住,该名称在所有 Azure 中必须是唯一的。 你可以使用在上一部分中创建的唯一名称。 在这种情况下,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 后会发生什么情况。

  2. 运行以下命令以使用不允许的参数来部署模板。 此处,你还将 storageSKU 参数更改为 Basic。 填写 storageName 参数的唯一名称。 记住,该名称在所有 Azure 中必须是唯一的。 你可以使用在上一部分中创建的唯一名称。 在这种情况下,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}
    

    此部署失败。 请注意错误。

    Screenshot of the Terminal window showing the deployment validation error.

  1. 运行以下命令来部署模板。 填写 storageName 参数的唯一名称。 记住,该名称在所有 Azure 中必须是唯一的。 你可以使用在上一部分中创建的唯一名称。 在这种情况下,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 后会发生什么情况。

  2. 运行以下命令以使用不允许的参数来部署模板。 此处,你还将 storageSKU 参数更改为 Basic。 填写 storageName 参数的唯一名称。 记住,该名称在所有 Azure 中必须是唯一的。 你可以使用在上一部分中创建的唯一名称。 在这种情况下,Azure 将更新资源,而不是创建新资源。

    $today=Get-Date -Format "MM-dd-yyyy"
    $deploymentName="addSkuParameter-"+"$today"
    New-AzResourceGroupDeployment `
      -Name $deploymentName `
      -TemplateFile $templateFile `
      -storageName {your-unique-name} `
      -storageSKU Basic
    

    此部署失败。 请注意错误。

    Screenshot of the Terminal window showing the deployment validation error.

将输出添加到 ARM 模板

此处,你需要添加到 ARM 模板的 outputs 部分以输出存储帐户资源的终结点。

  1. 在 Visual Studio Code 中的 azuredeploy.json 文件中,将光标放在输出属性 "outputs":{}, 中的大括号内。

  2. 按 Enter,然后输入“out”。你将获得相关代码片段的列表。 选择“new-output”。 这会将泛型输出添加到模板。 它将如下所示:

    "outputs": {
      "output1": {
        "type": "string",
        "value": "value"
      }
    
  3. 将“output1”更改为“storageEndpoint”,然后将 type 的值更改为“object”。 将 value 的值更改为“[reference(parameters('storageName')).primaryEndpoints]”。 这是我们在上一单元中介绍的用于获取终结点数据的表达式。 我们将类型指定为了“object”,因此它将返回 JSON 格式的对象。

    "outputs": {
      "storageEndpoint": {
        "type": "object",
        "value": "[reference(parameters('storageName')).primaryEndpoints]"
      }
    
  4. 保存文件。

使用输出部署 ARM 模板

此处,你将部署模板,并看到采用 JSON 格式的终结点输出。 需要为 storageName 参数填写一个唯一的名称。 记住,该名称在所有 Azure 中必须是唯一的。 你可以使用在上一部分中创建的唯一名称。 在这种情况下,Azure 将更新资源,而不是创建新资源。

  1. 运行以下命令来部署模板。 请确保将 {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}
    

    请注意输出。

    Screenshot of the Terminal window showing the primary endpoints output as JSON.

  1. 运行以下命令来部署模板。 请确保将 {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
    

    请注意输出。

    Screenshot of the Terminal window showing the primary endpoints output as JSON.

检查你的输出部署

在 Azure 门户中,转到 addOutputs 部署。 还可以在此处找到你的输出。

Screenshot of the Azure portal showing the output selection in the left menu.