次の方法で共有


ARM テンプレートの変数

この記事では、Azure Resource Manager テンプレート (ARM テンプレート) で変数を定義して使用する方法について説明します。 テンプレートを簡略化するには、変数を使用します。 テンプレート全体で複雑な式を繰り返すのではなく、複雑な式を含む変数を定義します。 次に、テンプレート全体で必要に応じてその変数を使用します。

Resource Manager は、デプロイ操作を開始する前に変数を解決します。 テンプレートで変数が使用されている場合は、Resource Manager によって解決された値に置き換えられます。

ヒント

ARM テンプレートと同じ機能を備え、構文も使いやすいため、Bicep をお勧めします。 詳細については、 変数を参照してください。

テンプレート内の変数は 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))]"
},

変数宣言で 参照 関数または リスト 関数を使用することはできません。 これらの関数はリソースのランタイム状態を取得し、変数が解決されたときにデプロイ前に実行することはできません。

変数を使用する

次の例は、リソース プロパティに変数を使用する方法を示しています。

変数の値を参照するには、 変数 関数を使用します。

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

構成変数

環境を構成するための関連する値を保持する変数を定義できます。 変数は、値を持つオブジェクトとして定義します。 次の例は、testprod という 2 つの環境の値を保持するオブジェクトを示しています。デプロイ中に、これらの値のいずれかを渡します。

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

次のステップ