Funzioni logiche per i modelli di ARM

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 da usare. Per ulteriori informazioni, vedere la funzione logica bool e gli operatori logici.

e

and(arg1, arg2, ...)

Verifica se tutti i valori dei parametri sono true.

La funzione and non è supportata in Bicep. Usare invece l'operatore &&.

Parametri

Parametro Richiesto Type Descrizione
arg1 boolean Primo valore da controllare per verificare se è true.
arg2 boolean Secondo valore da controllare per verificare se è true.
altri argomenti No boolean Argomenti aggiuntivi da controllare per verificare se sono true.

Valore restituito

Restituisce True se tutti i valori sono true. In caso contrario, restituisce False.

Esempi

L'esempio seguente mostra come usare le funzioni logiche.

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "resources": [],
  "outputs": {
    "andExampleOutput": {
      "type": "bool",
      "value": "[and(bool('true'), bool('false'))]"
    },
    "orExampleOutput": {
      "type": "bool",
      "value": "[or(bool('true'), bool('false'))]"
    },
    "notExampleOutput": {
      "type": "bool",
      "value": "[not(bool('true'))]"
    }
  }
}

L'output dell'esempio precedente è:

Nome Type Valore
andExampleOutput Bool Falso
orExampleOutput Bool Vero
notExampleOutput Bool Falso

bool

bool(arg1)

Converte il parametro in un valore booleano.

In Bicep usare la funzione logica bool.

Parametri

Parametro Richiesto Type Descrizione
arg1 stringa o numero intero Valore da convertire in un valore booleano.

Valore restituito

Valore booleano del valore convertito.

Osservazioni:

È anche possibile usare true() e false() per ottenere valori booleani.

Esempi

L'esempio seguente illustra come usare il parametro bool con un numero intero o una stringa.

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "resources": [],
  "outputs": {
    "trueString": {
      "type": "bool",
      "value": "[bool('true')]"
    },
    "falseString": {
      "type": "bool",
      "value": "[bool('false')]"
    },
    "trueInt": {
      "type": "bool",
      "value": "[bool(1)]"
    },
    "falseInt": {
      "type": "bool",
      "value": "[bool(0)]"
    }
  }
}

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

Nome Type Valore
trueString Bool Vero
falseString Bool Falso
trueInt Bool Vero
falseInt Bool Falso

false

false()

Restituisce false.

La funzione false non è disponibile in Bicep. Usare invece la parola chiave false.

Parametri

La funzione false non accetta alcun parametro.

Valore restituito

Un booleano che è sempre false.

Esempio

Nell'esempio seguente viene restituito un valore in uscita false.

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

L'output dell'esempio precedente è:

Nome Type Valore
falseOutput Bool Falso

if

if(condition, trueValue, falseValue)

Restituisce un valore in base a un condizione true o false.

La funzione if non è supportata in Bicep. Usare invece ?: operator.

Parametri

Parametro Richiesto Type Descrizione
condizione boolean Valore da controllare per verificare se è true o false.
trueValue string, int, object o array Valore da restituire quando la condizione è true.
falseValue string, int, object o array Valore da restituire quando la condizione è false.

Valore restituito

Restituisce il secondo parametro, quando il primo parametro è True. In caso contrario, restituisce il terzo parametro.

Osservazioni:

Quando la condizione è True, viene valutato solo il valore true. Quando la condizione è False, viene valutato solo il valore false. Con la funzione if è possibile includere espressioni che sono valide solo in modo condizionale. Ad esempio, è possibile fare riferimento a una risorsa esistente in una condizione, ma non nell'altra condizione. Un esempio di valutazione condizionale delle espressioni è illustrato nella sezione seguente.

Esempi

L'esempio seguente illustra come usare la funzione if.

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "resources": [
  ],
  "outputs": {
    "yesOutput": {
      "type": "string",
      "value": "[if(equals('a', 'a'), 'yes', 'no')]"
    },
    "noOutput": {
      "type": "string",
      "value": "[if(equals('a', 'b'), 'yes', 'no')]"
    },
    "objectOutput": {
      "type": "object",
      "value": "[if(equals('a', 'a'), json('{\"test\": \"value1\"}'), json('null'))]"
    }
  }
}

L'output dell'esempio precedente è:

Nome Type Valore
yesOutput Stringa yes
noOutput Stringa no
objectOutput Oggetto { "test": "value1" }

Il modello di esempio seguente illustra come usare questa funzione con espressioni che sono valide solo in modo condizionale.

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "vmName": {
      "type": "string"
    },
    "location": {
      "type": "string"
    },
    "logAnalytics": {
      "type": "string",
      "defaultValue": ""
    }
  },
  "resources": [
   {
      "condition": "[not(empty(parameters('logAnalytics')))]",
      "type": "Microsoft.Compute/virtualMachines/extensions",
      "apiVersion": "2022-11-01",
      "name": "[format('{0}/omsOnboarding', parameters('vmName'))]",
      "location": "[parameters('location')]",
      "properties": {
        "publisher": "Microsoft.EnterpriseCloud.Monitoring",
        "type": "MicrosoftMonitoringAgent",
        "typeHandlerVersion": "1.0",
        "autoUpgradeMinorVersion": true,
        "settings": {
          "workspaceId": "[if(not(empty(parameters('logAnalytics'))), reference(parameters('logAnalytics'), '2015-11-01-preview').customerId, null())]"
        },
        "protectedSettings": {
          "workspaceKey": "[if(not(empty(parameters('logAnalytics'))), listKeys(parameters('logAnalytics'), '2015-11-01-preview').primarySharedKey, null())]"
        }
      }
    }
  ],
  "outputs": {
    "mgmtStatus": {
      "type": "string",
      "value": "[if(not(empty(parameters('logAnalytics'))), 'Enabled monitoring for VM!', 'Nothing to enable')]"
    }
  }
}

not

not(arg1)

Converte il valore booleano nel valore opposto.

La funzione not non è supportata in Bicep. Usare invece !: operator.

Parametri

Parametro Richiesto Type Descrizione
arg1 boolean Valore da convertire.

Valore restituito

Restituisce True quando il parametro è False. Restituisce False quando il parametro è True.

Esempi

L'esempio seguente mostra come usare le funzioni logiche.

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "resources": [],
  "outputs": {
    "andExampleOutput": {
      "type": "bool",
      "value": "[and(bool('true'), bool('false'))]"
    },
    "orExampleOutput": {
      "type": "bool",
      "value": "[or(bool('true'), bool('false'))]"
    },
    "notExampleOutput": {
      "type": "bool",
      "value": "[not(bool('true'))]"
    }
  }
}

L'output dell'esempio precedente è:

Nome Type Valore
andExampleOutput Bool Falso
orExampleOutput Bool Vero
notExampleOutput Bool Falso

L’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 Vero

or

or(arg1, arg2, ...)

Verifica se uno qualsiasi dei valori dei parametri è true.

La funzione or non è supportata in Bicep. Utilizzare invece || operator.

Parametri

Parametro Richiesto Type Descrizione
arg1 boolean Primo valore da controllare per verificare se è true.
arg2 boolean Secondo valore da controllare per verificare se è true.
altri argomenti No boolean Argomenti aggiuntivi da controllare per verificare se sono true.

Valore restituito

Restituisce True se uno qualsiasi dei valori è true. In caso contrario, restituisce False.

Esempi

L'esempio seguente mostra come usare le funzioni logiche.

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "resources": [],
  "outputs": {
    "andExampleOutput": {
      "type": "bool",
      "value": "[and(bool('true'), bool('false'))]"
    },
    "orExampleOutput": {
      "type": "bool",
      "value": "[or(bool('true'), bool('false'))]"
    },
    "notExampleOutput": {
      "type": "bool",
      "value": "[not(bool('true'))]"
    }
  }
}

L'output dell'esempio precedente è:

Nome Type Valore
andExampleOutput Bool Falso
orExampleOutput Bool Vero
notExampleOutput Bool Falso

true

true()

Restituisce true.

La funzione true non è disponibile in Bicep. Usare invece la parola chiave true.

Parametri

La funzione true non accetta alcun parametro.

Valore restituito

Un booleano che è sempre true.

Esempio

Nell'esempio seguente viene restituito un valore in uscita true.

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

L'output dell'esempio precedente è:

Nome Type Valore
trueOutput Bool Vero

Passaggi successivi