ARM模板可选参数

时间:2018-08-13 09:14:39

标签: azure azure-pipelines arm-template

我有一个非常简单的带有参数的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

任何建议如何使参数可选?

1 个答案:

答案 0 :(得分:3)

您不能使用空字符串作为资源名称。因此,将其从空字符串更改为“ false”(或其他任何值),然后执行以下操作:

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

或者,您可以执行以下操作:

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

并使用此条件来部署或不部署嵌套部署,这将部署存储帐户。之所以可行,是因为您可以为嵌套部署使用另一个名称,但是由于条件的计算结果为false,因此它不会启动。

这是\在参数\对象不为空时执行操作的一般方法。