Funzioni di confronto per i modelli di Azure Resource Manager

Resource Manager offre diverse funzioni per eseguire confronti nel modello di Azure Resource Manager (modello arm):

Suggerimento

È consigliabile usare Bicep perché offre le stesse funzionalità dei modelli arm e la sintassi è più semplice da usare. Per altre informazioni, vedere l'operatore logico coalesce 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 Obbligatoria Tipo Descrizione
arg1 int, stringa, matrice o oggetto Il primo valore da controllare per verificare se è null.
più argomenti No int, stringa, matrice o oggetto Altri valori da testare per 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 default
intOutput Int 1
objectOutput Oggetto {"first": "default"}
arrayOutput Array  [1]
emptyOutput Bool True

equals

equals(arg1, arg2)

Controlla se due valori sono uguali tra loro.

In Bicep usare invece l'operatore == . Vedere Equals ==.

Parametri

Parametro Obbligatoria Tipo Descrizione
arg1 int, stringa, matrice o oggetto Il primo valore per verificare l'uguaglianza.
arg2 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.

Commenti

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 verificarne 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": "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'))]"
    }
  }
}

L'output dell'esempio precedente con i valori predefiniti è il seguente:

Nome Type valore
checkInts Bool True
checkStrings Bool True
checkArrays Bool True
checkObjects Bool True

Il modello di esempio seguente usa not con 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))]"
    }
  }
}

L'output dell'esempio precedente è:

Nome Type valore
checkNotEquals Bool True

greater

greater(arg1, arg2)

Controlla se il primo valore è maggiore del secondo.

In Bicep usare invece l'operatore > . Vedere Maggiore di >.

Parametri

Parametro Obbligatoria Tipo Descrizione
arg1 int o stringa Il primo valore per il confronto del maggiore.
arg2 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 True

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 Obbligatoria Tipo Descrizione
arg1 int o stringa Il primo valore per il confronto del maggiore e dell'uguaglianza.
arg2 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 True

less

less(arg1, arg2)

Controlla se il primo valore è minore del secondo.

In Bicep usare invece l'operatore < . Vedere Meno di <.

Parametri

Parametro Obbligatoria Tipo Descrizione
arg1 int o stringa Il primo valore per il confronto del minore.
arg2 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 uno è 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 True
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 <=.

Parametri

Parametro Obbligatoria Tipo Descrizione
arg1 int o stringa Il primo valore per il confronto del minore o dell'uguaglianza.
arg2 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 True
checkStrings Bool Falso

Passaggi successivi