ARM 範本中的變數

此文章描述如何在 Azure Resource Manager 範本 (ARM 範本) 中定義和使用變數。 您可以使用變數來簡化範本。 您可定義包含複雜運算式的變數,而不需在整個範本中重複執行複雜運算式。 如此一來,您就能在整個範本中視需要使用該變數。

Resource Manager 會在開始部署作業之前解析變數。 只要在範本中使用變數,Resource Manager 就會以已解析的值來加以取代。

提示

我們建議使用 Bicep,因為其提供的功能與 ARM 範本相同,而且語法更易於使用。 若要深入了解,請參閱變數

範本中限制使用 256 個變數。 如需詳細資訊,請參閱範本限制

定義變數

定義變數時,您不會指定變數的資料類型。 而應改為提供值或範本運算式。 變數類型是從已解析的值推斷而來。 下列範例會將變數設定為字串。

"variables": {
  "stringVar": "example value"
},

若要建構變數,您可以使用參數或其他變數中的值。

"parameters": {
  "inputValue": {
    "defaultValue": "deployment parameter",
    "type": "string"
  }
},
"variables": {
  "stringVar": "myVariable",
  "concatToVar": "[concat(variables('stringVar'), '-addtovar') ]",
  "concatToParam": "[concat(parameters('inputValue'), '-addtoparam')]"
}

您可以使用範本函式來建構變數值。

下列範例會建立儲存體帳戶名稱的字串值。 而且使用數個範本函式來取得一個參數值,並將該值串連至唯一字串。

"variables": {
  "storageName": "[concat(toLower(parameters('storageNamePrefix')), uniqueString(resourceGroup().id))]"
},

您無法在變數宣告中使用 reference 函式或任何 list 函式。 這些函式會取得資源的執行階段狀態,而且在解析變數時無法在部署之前執行。

使用變數

下列範例顯示如何將變數用於資源屬性。

若要參考變數的值,請使用 variables 函式。

"variables": {
  "storageName": "[concat(toLower(parameters('storageNamePrefix')), uniqueString(resourceGroup().id))]"
},
"resources": [
  {
    "type": "Microsoft.Storage/storageAccounts",
    "name": "[variables('storageName')]",
    ...
  }
]

範本範例

下列範本不會部署任何資源。 而是會顯示一些宣告不同類型變數的方式。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "inputValue": {
      "defaultValue": "deployment parameter",
      "type": "string"
    }
  },
  "variables": {
    "stringVar": "myVariable",
    "concatToVar": "[concat(variables('stringVar'), '-addtovar') ]",
    "concatToParam": "[concat(parameters('inputValue'), '-addtoparam')]",
    "arrayVar": [
      1,
      2,
      3,
      4
    ],
    "objectVar": {
      "property1": "value1",
      "property2": "value2"
    },
    "copyWithinVar": {
      "copy": [
        {
          "name": "disks",
          "count": 5,
          "input": {
            "name": "[concat('myDataDisk', copyIndex('disks', 1))]",
            "diskSizeGB": "1",
            "diskIndex": "[copyIndex('disks')]"
          }
        },
        {
          "name": "diskNames",
          "count": 5,
          "input": "[concat('myDataDisk', copyIndex('diskNames', 1))]"
        }
      ]
    },
    "copy": [
      {
        "name": "topLevelCopy1",
        "count": 5,
        "input": {
          "name": "[concat('oneDataDisk', copyIndex('topLevelCopy1', 1))]",
          "diskSizeGB": "1",
          "diskIndex": "[copyIndex('topLevelCopy1')]"
        }
      },
      {
        "name": "topLevelCopy2",
        "count": 3,
        "input": {
          "name": "[concat('twoDataDisk', copyIndex('topLevelCopy2', 1))]",
          "diskSizeGB": "1",
          "diskIndex": "[copyIndex('topLevelCopy2')]"
        }
      },
      {
        "name": "topLevelCopy3",
        "count": 4,
        "input": "[concat('stringValue', copyIndex('topLevelCopy3'))]"
      },
      {
        "name": "topLevelCopy4",
        "count": 4,
        "input": "[copyIndex('topLevelCopy4')]"
      }
    ]
  },
  "resources": [],
  "outputs": {
    "stringOutput": {
      "type": "string",
      "value": "[variables('stringVar')]"
    },
    "concatToVariableOutput": {
      "type": "string",
      "value": "[variables('concatToVar')]"
    },
    "concatToParameterOutput": {
      "type": "string",
      "value": "[variables('concatToParam')]"
    },
    "arrayOutput": {
      "type": "array",
      "value": "[variables('arrayVar')]"
    },
    "arrayElementOutput": {
      "type": "int",
      "value": "[variables('arrayVar')[0]]"
    },
    "objectOutput": {
      "type": "object",
      "value": "[variables('objectVar')]"
    },
    "copyWithinVariableOutput": {
      "type": "object",
      "value": "[variables('copyWithinVar')]"
    },
    "topLevelCopyOutput1": {
      "type": "array",
      "value": "[variables('topLevelCopy1')]"
    },
    "topLevelCopyOutput2": {
      "type": "array",
      "value": "[variables('topLevelCopy2')]"
    },
    "topLevelCopyOutput3": {
      "type": "array",
      "value": "[variables('topLevelCopy3')]"
    },
    "topLevelCopyOutput4": {
      "type": "array",
      "value": "[variables('topLevelCopy4')]"
    }
  }
}

設定變數

您可定義包含設定環境相關值的變數。 您可將變數定義為具有值的物件。 下列範例顯示的物件包含兩個環境的值:測試生產環境。請在部署期間傳入上述其中一個值。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "environmentName": {
      "type": "string",
      "allowedValues": [
        "test",
        "prod"
      ],
      "metadata": {
        "description": "Specify either test or prod for configuration values."
      }
    }
  },
  "variables": {
    "environmentSettings": {
      "test": {
        "instanceSize": "Small",
        "instanceCount": 1
      },
      "prod": {
        "instanceSize": "Large",
        "instanceCount": 4
      }
    }
  },
  "resources": [],
  "outputs": {
    "instanceSize": {
      "value": "[variables('environmentSettings')[parameters('environmentName')].instanceSize]",
      "type": "string"
    },
    "instanceCount": {
      "value": "[variables('environmentSettings')[parameters('environmentName')].instanceCount]",
      "type": "int"
    }
  }
}

下一步