ARM 템플릿의 변수

이 문서에서는 ARM 템플릿(Azure Resource Manager 템플릿)에서 변수를 정의하고 사용하는 방법을 설명합니다. 변수를 사용하여 템플릿을 간소화합니다. 템플릿 전체에서 복잡한 식을 반복하는 대신 복잡한 식을 포함하는 변수를 정의합니다. 그런 다음 템플릿 전체에서 필요에 따라 해당 변수를 사용합니다.

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

구성 변수

환경 구성을 위해 관련 값을 포함하는 변수를 정의할 수 있습니다. 변수를 값이 있는 개체로 정의합니다. 다음 예제에서는 두 환경(test 및 prod)에 대한 값을 보유하는 개체를 보여 줍니다. 배포하는 동안 이러한 값 중 하나를 전달합니다.

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

다음 단계

  • 변수에 사용할 수 있는 속성에 대해 알아보려면 ARM 템플릿의 구조 및 구문 이해를 참조 하세요.
  • 변수를 만드는 방법에 대한 권장 사항은 모범 사례 - 변수를 참조 하세요.
  • 네트워크 보안 그룹에 보안 규칙을 할당하는 예제 템플릿은 네트워크 보안 규칙매개 변수 파일을 참조하세요.