ARM 範本的比較函式

Resource Manager 提供數個函式,可讓您在 Azure Resource Manager 範本 (ARM 範本) 中進行比較:

提示

我們建議使用 Bicep,因為其提供的功能與 ARM 範本相同,而且語法更易於使用。 若要深入了解,請參閱聯合邏輯運算子和比較運算子。

coalesce

coalesce(arg1, arg2, arg3, ...)

從參數中傳回第一個非 null 值。 空白字串、空白陣列和空白物件不是 null。

在 Bicep 中,請改用 ?? 運算子。 請參閱聯合??

參數

參數 必要 類型​ 描述
arg1 Yes 整數、字串、陣列或物件 要測試是否為 null 的第一個值。
更多參數 No 整數、字串、陣列或物件 要測試是否為 Null 的其他值。

傳回值

第一個非 null 參數的值,此值可為字串、整數、陣列或物件。 如果所有參數都是 null,則傳回 null。

範例

下列範例範本顯示不同的聯合用法所得到的輸出。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "objectToTest": {
      "type": "object",
      "defaultValue": {
        "null1": null,
        "null2": null,
        "string": "default",
        "int": 1,
        "object": { "first": "default" },
        "array": [ 1 ]
      }
    }
  },
  "resources": [
  ],
  "outputs": {
    "stringOutput": {
      "type": "string",
      "value": "[coalesce(parameters('objectToTest').null1, parameters('objectToTest').null2, parameters('objectToTest').string)]"
    },
    "intOutput": {
      "type": "int",
      "value": "[coalesce(parameters('objectToTest').null1, parameters('objectToTest').null2, parameters('objectToTest').int)]"
    },
    "objectOutput": {
      "type": "object",
      "value": "[coalesce(parameters('objectToTest').null1, parameters('objectToTest').null2, parameters('objectToTest').object)]"
    },
    "arrayOutput": {
      "type": "array",
      "value": "[coalesce(parameters('objectToTest').null1, parameters('objectToTest').null2, parameters('objectToTest').array)]"
    },
    "emptyOutput": {
      "type": "bool",
      "value": "[empty(coalesce(parameters('objectToTest').null1, parameters('objectToTest').null2))]"
    }
  }
}

上述範例中具有預設值的輸出如下:

名稱 類型
stringOutput String 預設值
intOutput int 1
objectOutput Object {"first": "default"}
arrayOutput 陣列 [1]
emptyOutput Bool True

等於

equals(arg1, arg2)

檢查兩個值是否彼此相等。

在 Bicep 中,請改用 == 運算子。 請參閱 Equals ==

參數

參數 必要 類型​ 描述
arg1 Yes 整數、字串、陣列或物件 要檢查是否相等的第一個值。
arg2 Yes 整數、字串、陣列或物件 要檢查是否相等的第二個值。

傳回值

如果值相等則傳回 True,否則會傳回 False

備註

equals 函式通常會搭配 condition 元素,用來測試是否已部署資源。

{
  "condition": "[equals(parameters('newOrExisting'),'new')]",
  "type": "Microsoft.Storage/storageAccounts",
  "name": "[variables('storageAccountName')]",
  "apiVersion": "2022-09-01",
  "location": "[resourceGroup().location]",
  "sku": {
    "name": "[variables('storageAccountType')]"
  },
  "kind": "Storage",
  "properties": {}
}

範例

下列範例會檢查不同類型的值是否相等。 所有預設值都會傳回 True。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "firstInt": {
      "type": "int",
      "defaultValue": 1
    },
    "secondInt": {
      "type": "int",
      "defaultValue": 1
    },
    "firstString": {
      "type": "string",
      "defaultValue": "a"
    },
    "secondString": {
      "type": "string",
      "defaultValue": "a"
    },
    "firstArray": {
      "type": "array",
      "defaultValue": [ "a", "b" ]
    },
    "secondArray": {
      "type": "array",
      "defaultValue": [ "a", "b" ]
    },
    "firstObject": {
      "type": "object",
      "defaultValue": { "a": "b" }
    },
    "secondObject": {
      "type": "object",
      "defaultValue": { "a": "b" }
    }
  },
  "resources": [
  ],
  "outputs": {
    "checkInts": {
      "type": "bool",
      "value": "[equals(parameters('firstInt'), parameters('secondInt') )]"
    },
    "checkStrings": {
      "type": "bool",
      "value": "[equals(parameters('firstString'), parameters('secondString'))]"
    },
    "checkArrays": {
      "type": "bool",
      "value": "[equals(parameters('firstArray'), parameters('secondArray'))]"
    },
    "checkObjects": {
      "type": "bool",
      "value": "[equals(parameters('firstObject'), parameters('secondObject'))]"
    }
  }
}

上述範例中具有預設值的輸出如下:

名稱 類型
checkInts Bool True
checkStrings Bool True
checkArrays Bool True
checkObjects Bool True

下列範例範本使用 not 搭配 equals

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "resources": [
  ],
  "outputs": {
    "checkNotEquals": {
      "type": "bool",
      "value": "[not(equals(1, 2))]"
    }
  }
}

前述範例的輸出為:

名稱 類型
checkNotEquals Bool True

greater

greater(arg1, arg2)

檢查第一個值是否大於第二個值。

在 Bicep 中,請改用 > 運算子。 請參閱大於 >

參數

參數 必要 類型​ 描述
arg1 Yes 整數或字串 用於大於比較的第一個值。
arg2 Yes 整數或字串 用於大於比較的第二個值。

傳回值

如果第一個值大於第二個值則傳回 True,否則傳回 False

範例

下列範例會檢查某個值是否大於另一個值。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "firstInt": {
      "type": "int",
      "defaultValue": 1
    },
    "secondInt": {
      "type": "int",
      "defaultValue": 2
    },
    "firstString": {
      "type": "string",
      "defaultValue": "A"
    },
    "secondString": {
      "type": "string",
      "defaultValue": "a"
    }
  },
  "resources": [
  ],
  "outputs": {
    "checkInts": {
      "type": "bool",
      "value": "[greater(parameters('firstInt'), parameters('secondInt') )]"
    },
    "checkStrings": {
      "type": "bool",
      "value": "[greater(parameters('firstString'), parameters('secondString'))]"
    }
  }
}

上述範例中具有預設值的輸出如下:

名稱 類型
checkInts Bool False
checkStrings Bool True

greaterOrEquals

greaterOrEquals(arg1, arg2)

檢查第一個值是否大於或等於第二個值。

在 Bicep 中,請改用 >= 運算子。 請參閱大於或等於 >=

參數

參數 必要 類型​ 描述
arg1 Yes 整數或字串 用於大於或等於比較的第一個值。
arg2 Yes 整數或字串 用於大於或等於比較的第二個值。

傳回值

如果第一個值大於或等於第二個值則傳回 True,否則傳回 False

範例

下列範例會檢查某個值是否大於或等於另一個值。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "firstInt": {
      "type": "int",
      "defaultValue": 1
    },
    "secondInt": {
      "type": "int",
      "defaultValue": 2
    },
    "firstString": {
      "type": "string",
      "defaultValue": "A"
    },
    "secondString": {
      "type": "string",
      "defaultValue": "a"
    }
  },
  "resources": [
  ],
  "outputs": {
    "checkInts": {
      "type": "bool",
      "value": "[greaterOrEquals(parameters('firstInt'), parameters('secondInt') )]"
    },
    "checkStrings": {
      "type": "bool",
      "value": "[greaterOrEquals(parameters('firstString'), parameters('secondString'))]"
    }
  }
}

上述範例中具有預設值的輸出如下:

名稱 類型
checkInts Bool False
checkStrings Bool True

less

less(arg1, arg2)

檢查第一個值是否小於第二個值。

在 Bicep 中,請改用 < 運算子。 請參閱小於 <

參數

參數 必要 類型​ 描述
arg1 Yes 整數或字串 用於小於比較的第一個值。
arg2 Yes 整數或字串 用於小於比較的第二個值。

傳回值

如果第一個值小於第二個值則傳回 True,否則傳回 False

範例

下列範例會檢查某個值是否小於另一個值。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "firstInt": {
      "type": "int",
      "defaultValue": 1
    },
    "secondInt": {
      "type": "int",
      "defaultValue": 2
    },
    "firstString": {
      "type": "string",
      "defaultValue": "A"
    },
    "secondString": {
      "type": "string",
      "defaultValue": "a"
    }
  },
  "resources": [
  ],
  "outputs": {
    "checkInts": {
      "type": "bool",
      "value": "[less(parameters('firstInt'), parameters('secondInt') )]"
    },
    "checkStrings": {
      "type": "bool",
      "value": "[less(parameters('firstString'), parameters('secondString'))]"
    }
  }
}

上述範例中具有預設值的輸出如下:

名稱 類型
checkInts Bool True
checkStrings Bool False

lessOrEquals

lessOrEquals(arg1, arg2)

檢查第一個值是否小於或等於第二個值。

在 Bicep 中,請改用 <= 運算子。 請參閱小於或等於 <=

參數

參數 必要 類型​ 描述
arg1 Yes 整數或字串 用於小於或等於比較的第一個值。
arg2 Yes 整數或字串 用於小於或等於比較的第二個值。

傳回值

如果第一個值小於或等於第二個值則傳回 True,否則傳回 False

範例

下列範例會檢查某個值是否小於或等於另一個值。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "firstInt": {
      "type": "int",
      "defaultValue": 1
    },
    "secondInt": {
      "type": "int",
      "defaultValue": 2
    },
    "firstString": {
      "type": "string",
      "defaultValue": "A"
    },
    "secondString": {
      "type": "string",
      "defaultValue": "a"
    }
  },
  "resources": [
  ],
  "outputs": {
    "checkInts": {
      "type": "bool",
      "value": "[lessOrEquals(parameters('firstInt'), parameters('secondInt') )]"
    },
    "checkStrings": {
      "type": "bool",
      "value": "[lessOrEquals(parameters('firstString'), parameters('secondString'))]"
    }
  }
}

上述範例中具有預設值的輸出如下:

名稱 類型
checkInts Bool True
checkStrings Bool False

下一步