创建资源管理器参数文件Create Resource Manager parameter file

你可能会发现,与在脚本中以内联值的形式传递参数相比,使用包含参数值的 JSON 文件更为容易。Rather than passing parameters as inline values in your script, you may find it easier to use a JSON file that contains the parameter values. 本文介绍如何创建参数文件。This article shows how to create the parameter file.

参数文件Parameter file

参数文件采用以下格式:The parameter file has the following format:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "<first-parameter-name>": {
      "value": "<first-value>"
    },
    "<second-parameter-name>": {
      "value": "<second-value>"
    }
  }
}

请注意,参数值以纯文本形式存储在参数文件中。Notice that the parameter values are stored as plain text in the parameter file. 此方法适用于非敏感值,例如,为资源指定 SKU。This approach works for values that aren't sensitive, such as specifying the SKU for a resource. 它不适用于敏感值(例如密码)。It doesn't work for sensitive values, such as passwords. 如果需要将敏感值作为参数传递,请将该值存储在 Key Vault 中,并在参数文件中引用该 Key Vault。If you need to pass a sensitive value as a parameter, store the value in a key vault, and reference the key vault in your parameter file. 在部署过程中会安全地检索敏感值。The sensitive value is securely retrieved during deployment.

以下参数文件包含一个纯文本值和一个存储在 Key Vault 中的值。The following parameter file includes a plain text value and a value that is stored in a key vault.

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "<first-parameter-name>": {
      "value": "<first-value>"
    },
    "<second-parameter-name>": {
      "reference": {
        "keyVault": {
          "id": "<resource-id-key-vault>"
        },
        "secretName": "<secret-name>"
      }
    }
  }
}

有关使用 Key Vault 中的值的详细信息,请参阅在部署过程中使用 Azure Key Vault 传递安全参数值For more information about using values from a key vault, see Use Azure Key Vault to pass secure parameter value during deployment.

定义参数值Define parameter values

若要确定如何定义参数值,请打开要部署的模板。To figure out how to define the parameter values, open the template you're deploying. 查看该模板的参数节。Look at the parameters section of the template. 以下示例显示了模板中的参数。The following example shows the parameters from a template.

"parameters": {
  "storagePrefix": {
    "type": "string",
    "maxLength": 11
  },
  "storageAccountType": {
    "type": "string",
    "defaultValue": "Standard_LRS",
    "allowedValues": [
    "Standard_LRS",
    "Standard_GRS",
    "Premium_LRS"
    ]
  }
}

要注意的第一个详细信息是每个参数的名称。The first detail to notice is the name of each parameter. 参数文件中的值必须与名称匹配。The values in your parameter file must match the names.

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storagePrefix": {
    },
    "storageAccountType": {
    }
  }
}

注意参数的类型。Notice the type of the parameter. 参数文件中的值的类型必须相同。The values in your parameter file must have the same types. 对于此模板,可将两个参数都作为字符串提供。For this template, you can provide both parameters as strings.

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storagePrefix": {
      "value": ""
    },
    "storageAccountType": {
      "value": ""
    }
  }
}

接下来查看默认值。Next, look for a default value. 如果参数具有默认值,则你可以提供一个值,但不一定要这样做。If a parameter has a default value, you can provide a value but you don't have to.

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storagePrefix": {
      "value": "" // This value must be provided.
    },
    "storageAccountType": {
      "value": "" // This value is optional. Template will use default value if not provided.
    }
  }
}

最后,查看允许的值和任何限制(例如最大长度)。Finally, look at the allowed values and any restrictions like max length. 这些限制告知可为参数提供的值的范围。They tell you the range of values you can provide for the parameter.

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storagePrefix": {
      "value": "storage"
    },
    "storageAccountType": {
      "value": "Standard_LRS"
    }
  }
}

参数类型格式Parameter type formats

以下示例演示不同参数类型的格式。The following example shows the formats of different parameter types.

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "exampleString": {
      "value": "test string"
    },
    "exampleInt": {
      "value": 4
    },
    "exampleBool": {
      "value": true
    },
    "exampleArray": {
      "value": [
        "value 1",
        "value 2"
      ]
    },
    "exampleObject": {
      "value": {
        "property1": "value1",
        "property2": "value2"
      }
    }
   }
}

使用参数文件部署模板Deploy template with parameter file

请参阅:See:

文件名File name

参数文件的一般命名约定是将 .parameters 添加到模板名称。The general convention for naming the parameter file is to add .parameters to the template name. 例如,如果模板名为 azuredeploy.json,则参数文件名为 azuredeploy.parameters.jsonFor example, if your template is named azuredeploy.json, your parameter file is named azuredeploy.parameters.json. 此命名约定可帮助你查看模板与参数之间的连接。This naming convention helps you see the connection between the template and the parameters.

若要部署到不同的环境,请创建多个参数文件。To deploy to different environments, create more than one parameter file. 为参数文件命名时,请添加一种识别其用途的方式。When naming the parameter file, add a way to identify its use. 例如,使用 azuredeploy.parameters-dev.jsonazuredeploy.parameters-prod.jsonFor example, use azuredeploy.parameters-dev.json and azuredeploy.parameters-prod.json

参数优先级Parameter precedence

可以在同一部署操作中使用内联参数和本地参数文件。You can use inline parameters and a local parameter file in the same deployment operation. 例如,可以在本地参数文件中指定某些值,并在部署期间添加其他内联值。For example, you can specify some values in the local parameter file and add other values inline during deployment. 如果同时为本地参数文件中的参数和内联参数提供值,则内联值优先。If you provide values for a parameter in both the local parameter file and inline, the inline value takes precedence.

可以通过提供文件的 URI 来使用外部参数文件。It's possible to use an external parameter file, by providing the URI to the file. 执行此操作时,不能传递是内联值或来自本地文件的其他值。When you do this, you can't pass other values either inline or from a local file. 会忽略所有内联参数。All inline parameters are ignored. 提供外部文件中的所有参数值。Provide all parameter values in the external file.

参数名冲突Parameter name conflicts

如果模板包括的一个参数与 PowerShell 命令中的某个参数同名,PowerShell 使用后缀 FromTemplate 显示模板的参数。If your template includes a parameter with the same name as one of the parameters in the PowerShell command, PowerShell presents the parameter from your template with the postfix FromTemplate. 例如,模板中名为 ResourceGroupName 的参数与 New-AzResourceGroupDeployment cmdlet 中的 ResourceGroupName 参数冲突。For example, a parameter named ResourceGroupName in your template conflicts with the ResourceGroupName parameter in the New-AzResourceGroupDeployment cmdlet. 系统会提示你提供 ResourceGroupNameFromTemplate 的值。You're prompted to provide a value for ResourceGroupNameFromTemplate. 可以使用未用于部署命令的参数名称来避免这种混淆。You can avoid this confusion by using parameter names that aren't used for deployment commands.

后续步骤Next steps