ARM模板可选参数

Jev*_*sov 2 azure azure-rm-template azure-pipelines

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

"StorageName": {
  "type": "string",
  "defaultValue": ""
},
Run Code Online (Sandbox Code Playgroud)

和资源:

{
      "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')]"
    }
Run Code Online (Sandbox Code Playgroud)

StorageName具有默认为空字符串值,这是基于微软的文档有效值-默认值可以是一个空字符串。

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

"condition": "[greater(length(parameters('StorageName')), 0)]",
Run Code Online (Sandbox Code Playgroud)

但是当我运行这个 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
Run Code Online (Sandbox Code Playgroud)

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

4c7*_*b41 6

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

"[not(equals(parameters('name'), 'false'))] # << use the same value here
Run Code Online (Sandbox Code Playgroud)

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

"[not(empty(parameters('name')))]"
Run Code Online (Sandbox Code Playgroud)

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

如果\when parameter\object 不为空,这是执行某些操作的一般方法。