ARM テンプレートの比較関数

Resource Manager には、Azure Resource Manager テンプレート (ARM テンプレート) で比較を行うための関数がいくつか用意されています。

ヒント

ARM テンプレートと同じ機能を備え、構文も使いやすいため、Bicep をお勧めします。 詳細については、合体論理演算子と比較演算子に関するページを参照してください。

coalesce

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

パラメーターから最初の null 以外の値を返します。 空の文字列、空の配列、空のオブジェクトは null ではありません。

Bicep では、?? 演算子を使用してください。 「Coalesce ?? 」を参照してください。

パラメーター

パラメーター 必須 Type 説明
arg1 はい 整数、文字列、配列、オブジェクト null かどうかがテストされる最初の値。
その他の引数 いいえ 整数、文字列、配列、オブジェクト null かどうかを確かめるその他の値。

戻り値

最初の null 以外のパラメーターの値。文字列、整数、配列、またはオブジェクトが返されます。 すべてのパラメーターが null の場合は null になります。

次のテンプレート例では、coalesce をさまざまな方法で使用したときの出力を示します。

{
  "$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))]"
    }
  }
}

既定値を使用した場合の前の例の出力は次のようになります。

名前 Type
stringOutput String default
intOutput int 1
objectOutput Object {"first": "default"}
arrayOutput Array [1]
emptyOutput Bool True

equals

equals(arg1, arg2)

2 つの値が互いに等しいかどうかを確認します。

Bicep では、代わりに == 演算子を使用してください。 「Equals == 」を参照してください。

パラメーター

パラメーター 必須 Type 説明
arg1 はい 整数、文字列、配列、オブジェクト 等しいかどうかを確認する 1 番目の値。
arg2 はい 整数、文字列、配列、オブジェクト 等しいかどうかを確認する 2 番目の値。

戻り値

値が等しい場合は 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'))]"
    }
  }
}

既定値を使用した場合の前の例の出力は次のようになります。

名前 Type
checkInts Bool True
checkStrings Bool True
checkArrays Bool True
checkObjects Bool True

次のテンプレート例では、notequals を使用します。

{
  "$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))]"
    }
  }
}

前の例からの出力は次のようになります。

名前 Type [値]
checkNotEquals Bool True

greater

greater(arg1, arg2)

1 番目の値が 2 番目の値より大きいかどうかを確認します。

Bicep では、代わりに > 演算子を使用してください。 「次の値より大きい >」を参照してください。

パラメーター

パラメーター 必須 Type 説明
arg1 はい 整数または文字列 大きいかどうかを比較する 1 番目の値。
arg2 はい 整数または文字列 大きいかどうかを比較する 2 番目の値。

戻り値

1 番目の値が 2 番目の値よりも大きい場合は 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'))]"
    }
  }
}

既定値を使用した場合の前の例の出力は次のようになります。

名前 Type
checkInts Bool False
checkStrings Bool True

greaterOrEquals

greaterOrEquals(arg1, arg2)

1 番目の値が 2 番目の値以上かどうかを確認します。

Bicep では、代わりに >= 演算子を使用してください。 「次の値以上 >=」を参照してください。

パラメーター

パラメーター 必須 Type 説明
arg1 はい 整数または文字列 以上かどうかを比較する 1 番目の値。
arg2 はい 整数または文字列 以上かどうかを比較する 2 番目の値。

戻り値

1 番目の値が 2 番目の値以上の場合は 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'))]"
    }
  }
}

既定値を使用した場合の前の例の出力は次のようになります。

名前 Type
checkInts Bool False
checkStrings Bool True

less

less(arg1, arg2)

1 番目の値が 2 番目の値より小さいかどうかを確認します。

Bicep では、代わりに < 演算子を使用してください。 「次の値より小さい <」を参照してください。

パラメーター

パラメーター 必須 Type 説明
arg1 はい 整数または文字列 小さいかどうかを比較する 1 番目の値。
arg2 はい 整数または文字列 小さいかどうかを比較する 2 番目の値。

戻り値

1 番目の値が 2 番目の値よりも小さい場合は 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'))]"
    }
  }
}

既定値を使用した場合の前の例の出力は次のようになります。

名前 Type
checkInts Bool True
checkStrings Bool False

lessOrEquals

lessOrEquals(arg1, arg2)

1 番目の値が 2 番目の値以下かどうかを確認します。

Bicep では、代わりに <= 演算子を使用してください。 「次の値以下 <=」を参照してください。

パラメーター

パラメーター 必須 Type 説明
arg1 はい 整数または文字列 以下かどうかを比較する 1 番目の値。
arg2 はい 整数または文字列 以下かどうかを比較する 2 番目の値。

戻り値

1 番目の値が 2 番目の値以下の場合は 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'))]"
    }
  }
}

既定値を使用した場合の前の例の出力は次のようになります。

名前 Type
checkInts Bool True
checkStrings Bool False

次のステップ