ARM模板可选参数

ARM template optional parameters

我有一个非常简单的 ARM 模板 json 文件,其参数为:

"StorageName": {
  "type": "string",
  "defaultValue": ""
},

和资源:

{
      "name": "[parameters('StorageName')]",
      "type": "Microsoft.Storage/storageAccounts",
      "location": "[resourceGroup().location]",
      "apiVersion": "2018-02-01",
      "condition": "[greater(length(parameters('StorageName')), 0)]",
      "sku": {
        "name": "[parameters('StorageType')]"
      },
      "dependsOn": [],
      "tags": {
        "displayName": "..storage"
      },
      "properties": {
        "accountType": "[parameters('StorageType')]",
        "supportsHttpsTrafficOnly": true,
        "encryption": {
          "services": {
            "blob": {
              "enabled": true
            },
            "file": {
              "enabled": true
            }
          },
          "keySource": "Microsoft.Storage"
        }
      },
      "kind": "[parameters('StorageKind')]"
    }

StorageName 具有默认的空字符串值,这是基于 Microsoft 文档的有效值 - The default value can be an empty string.

我使用 condition 功能创建存储,如果只提供名称

"condition": "[greater(length(parameters('StorageName')), 0)]",

但是当我 运行 这个 ARM 模板时,我在 vso 控制台中仍然收到错误:

2018-08-13T08:46:56.1816398Z The detected encoding for file 'D:\a\r1\a\...\drop\azuredeploy.json' is 'utf-8'
2018-08-13T08:46:56.1949411Z The detected encoding for file 'D:\a\r1\a\...\drop\azuredeploy.parameters.staging.json' is 'utf-8'
2018-08-13T08:46:56.1950518Z Starting Deployment.
2018-08-13T08:46:57.1434733Z There were errors in your deployment. Error code: InvalidTemplate.
2018-08-13T08:46:57.1435535Z ##[error]Deployment template validation failed: 'The template resource '' at line '1' and column '1358' is not valid. The name property cannot be null or empty. Please see https://aka.ms/arm-template/#resources for usage details.'.
2018-08-13T08:46:57.1436356Z ##[error]Task failed while creating or updating the template deployment

关于如何使参数可选的任何建议?

您不能使用空字符串作为资源名称。如此合乎逻辑的解决方案,将其从空字符串更改为 'false' (或任何其他值)并执行以下操作:

"[not(equals(parameters('name'), 'false'))] # << use the same value here

或者你可以这样做:

"[not(empty(parameters('name')))]"

并使用此条件来部署或不部署嵌套部署,即部署存储帐户。这将起作用,因为您可以为嵌套部署使用另一个名称,但它不会启动,因为条件评估为 false。

这是做某事的一般方法if\when parameter\object不为空