Funzioni di confronto per i modelli di Azure Resource Manager
Resource Manager offre diverse funzioni per fare dei confronti nel modello di Azure Resource Manager (modello di ARM):
Suggerimento
È consigliabile Bicep perché offre le stesse funzionalità dei modelli di ARM e la sintassi è più semplice. Per altre informazioni, vedere l'operatore logico di unione e gli operatori di confronto .
coalesce
coalesce(arg1, arg2, arg3, ...)
Restituisce il primo valore non null dai parametri. Stringhe vuote, matrici vuote e oggetti vuoti sono non null.
In Bicep usare invece l'operatore ??
. Vedi Coalesce ??.
Parametri
Parametro | Richiesto | Type | Descrizione |
---|---|---|---|
arg1 | Sì | int, stringa, matrice o oggetto | Il primo valore da controllare per verificare se è null. |
altri argomenti | No | int, stringa, matrice o oggetto | Altri valori da testare per i valori Null. |
Valore restituito
Valore dei primi parametri non null, che può essere una stringa, un numero intero, una matrice o un oggetto. Null se tutti i parametri sono null.
Esempio
Il modello di esempio seguente illustra l'output per diversi usi di 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))]"
}
}
}
L'output dell'esempio precedente con i valori predefiniti è il seguente:
Nome | Type | Valore |
---|---|---|
stringOutput | String | impostazione predefinita |
intOutput | Int | 1 |
objectOutput | Object | {"first": "default"} |
arrayOutput | Matrice | [1] |
emptyOutput | Bool | Vero |
equals
equals(arg1, arg2)
Controlla se due valori sono identici. Il confronto fa distinzione tra maiuscole e minuscole.
In Bicep usare invece l'operatore ==
. Vedere Equals ==.
Parametri
Parametro | Richiesto | Type | Descrizione |
---|---|---|---|
arg1 | Sì | int, stringa, matrice o oggetto | Il primo valore per verificare l'uguaglianza. |
arg2 | Sì | int, stringa, matrice o oggetto | Il secondo valore per verificare l'uguaglianza. |
Valore restituito
Restituisce True se i valori sono uguali; in caso contrario, restituisce False.
Osservazioni:
La funzione uguale a viene spesso usata con l'elemento condition
per verificare se la risorsa viene distribuita.
{
"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": {}
}
Esempio
Nell'esempio seguente vengono controllati diversi tipi di valori per verificare l'uguaglianza. Tutti i valori predefiniti restituiscono 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": "demo"
},
"secondString": {
"type": "string",
"defaultValue": "Demo"
},
"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'))]"
}
}
}
L'output dell'esempio precedente con i valori predefiniti è il seguente:
Nome | Type | Valore | Nota |
---|---|---|---|
checkInts | Bool | Vero | |
checkStrings | Bool | Falso | Il risultato è false dovuto al fatto che il confronto fa distinzione tra maiuscole e minuscole. |
checkArrays | Bool | Vero | |
checkObjects | Bool | Vero |
Il modello di esempio seguente usa non con uguale a .
{
"$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))]"
}
}
}
L'output dell'esempio precedente è:
Nome | Type | Valore |
---|---|---|
checkNotEquals | Bool | Vero |
greater
greater(arg1, arg2)
Controlla se il primo valore è maggiore del secondo.
In Bicep usare invece l'operatore >
. Vedere Maggiore di >.
Parametri
Parametro | Richiesto | Type | Descrizione |
---|---|---|---|
arg1 | Sì | int o stringa | Il primo valore per il confronto del maggiore. |
arg2 | Sì | int o stringa | Il secondo valore per il confronto del maggiore. |
Valore restituito
Restituisce True se il primo valore è maggiore del secondo; in caso contrario, restituisce False.
Esempio
Nell'esempio seguente viene verificato se il valore è maggiore dell'altro.
{
"$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'))]"
}
}
}
L'output dell'esempio precedente con i valori predefiniti è il seguente:
Nome | Type | Valore |
---|---|---|
checkInts | Bool | Falso |
checkStrings | Bool | Vero |
greaterOrEquals
greaterOrEquals(arg1, arg2)
Controlla se il primo valore è maggiore o uguale al secondo valore.
In Bicep usare invece l'operatore >=
. Vedere Maggiore o uguale >a =.
Parametri
Parametro | Richiesto | Type | Descrizione |
---|---|---|---|
arg1 | Sì | int o stringa | Il primo valore per il confronto del maggiore e dell'uguaglianza. |
arg2 | Sì | int o stringa | Il secondo valore per il confronto del maggiore e dell'uguaglianza. |
Valore restituito
Restituisce True se il primo valore è maggiore o uguale al secondo; in caso contrario, restituisce False.
Esempio
Nell'esempio seguente viene verificato se il valore è maggiore o uguale all'altro.
{
"$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'))]"
}
}
}
L'output dell'esempio precedente con i valori predefiniti è il seguente:
Nome | Type | Valore |
---|---|---|
checkInts | Bool | Falso |
checkStrings | Bool | Vero |
less
less(arg1, arg2)
Controlla se il primo valore è minore del secondo.
In Bicep usare invece l'operatore <
. Vedere Minore di <.
Parametri
Parametro | Richiesto | Type | Descrizione |
---|---|---|---|
arg1 | Sì | int o stringa | Il primo valore per il confronto del minore. |
arg2 | Sì | int o stringa | Il secondo valore per il confronto del minore. |
Valore restituito
Restituisce True se il primo valore è inferiore al secondo; in caso contrario, restituisce False.
Esempio
Nell'esempio seguente viene verificato se il valore di un valore è minore dell'altro.
{
"$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'))]"
}
}
}
L'output dell'esempio precedente con i valori predefiniti è il seguente:
Nome | Type | Valore |
---|---|---|
checkInts | Bool | Vero |
checkStrings | Bool | Falso |
lessOrEquals
lessOrEquals(arg1, arg2)
Controlla se il primo valore è minore o uguale al secondo valore.
In Bicep usare invece l'operatore <=
. Vedere Minore o uguale <a =.
Parametri
Parametro | Richiesto | Type | Descrizione |
---|---|---|---|
arg1 | Sì | int o stringa | Il primo valore per il confronto del minore o dell'uguaglianza. |
arg2 | Sì | int o stringa | Il secondo valore per il confronto del minore o dell'uguaglianza. |
Valore restituito
Restituisce True se il primo valore è minore o uguale al secondo; in caso contrario, restituisce False.
Esempio
Nell'esempio seguente viene verificato se il valore è minore o uguale all'altro.
{
"$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'))]"
}
}
}
L'output dell'esempio precedente con i valori predefiniti è il seguente:
Nome | Type | Valore |
---|---|---|
checkInts | Bool | Vero |
checkStrings | Bool | Falso |
Passaggi successivi
- Per una descrizione delle sezioni in un modello di ARM, vedere Comprendere la struttura e la sintassi dei modelli di ARM.