Skip to content

Commit f37455e

Browse files
Merge pull request #146728 from tfitzmac/0211vars
add bicep variables
2 parents 6514435 + 54e6515 commit f37455e

File tree

1 file changed

+105
-10
lines changed

1 file changed

+105
-10
lines changed

articles/azure-resource-manager/templates/template-variables.md

Lines changed: 105 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,99 @@
11
---
22
title: Variables in templates
3-
description: Describes how to define variables in an Azure Resource Manager template (ARM template).
3+
description: Describes how to define variables in an Azure Resource Manager template (ARM template) and Bicep file.
44
ms.topic: conceptual
5-
ms.date: 01/26/2021
5+
ms.date: 02/12/2021
66
---
77

8-
# Variables in ARM template
8+
# Variables in ARM templates
99

10-
This article describes how to define and use variables in your Azure Resource Manager template (ARM template). You use variables to simplify your template. Rather than repeating complicated expressions throughout your template, you define a variable that contains the complicated expression. Then, you reference that variable as needed throughout your template.
10+
This article describes how to define and use variables in your Azure Resource Manager template (ARM template) or Bicep file. You use variables to simplify your template. Rather than repeating complicated expressions throughout your template, you define a variable that contains the complicated expression. Then, you use that variable as needed throughout your template.
1111

1212
Resource Manager resolves variables before starting the deployment operations. Wherever the variable is used in the template, Resource Manager replaces it with the resolved value.
1313

14+
[!INCLUDE [Bicep preview](../../../includes/resource-manager-bicep-preview.md)]
15+
1416
## Define variable
1517

16-
When defining a variable, provide a value or template expression that resolves to a [data type](template-syntax.md#data-types). You can use the value from a parameter or another variable when constructing the variable.
18+
When defining a variable, you don't specify a [data type](template-syntax.md#data-types) for the variable. Instead provide a value or template expression. The variable type is inferred from the resolved value. The following example sets a variable to a string.
19+
20+
# [JSON](#tab/json)
21+
22+
```json
23+
"variables": {
24+
"stringVar": "example value"
25+
},
26+
```
27+
28+
# [Bicep](#tab/bicep)
29+
30+
```bicep
31+
var stringVar = 'example value'
32+
```
1733

18-
You can use [template functions](template-functions.md) in the variable declaration, but you can't use the [reference](template-functions-resource.md#reference) function or any of the [list](template-functions-resource.md#list) functions. These functions get the runtime state of a resource, and can't be executed before deployment when variables are resolved.
34+
---
1935

20-
The following example shows a variable definition. It creates a string value for a storage account name. It uses several template functions to get a parameter value, and concatenates it to a unique string.
36+
You can use the value from a parameter or another variable when constructing the variable.
37+
38+
# [JSON](#tab/json)
39+
40+
```json
41+
"parameters": {
42+
"inputValue": {
43+
"defaultValue": "deployment parameter",
44+
"type": "string"
45+
}
46+
},
47+
"variables": {
48+
"stringVar": "myVariable",
49+
"concatToVar": "[concat(variables('stringVar'), '-addtovar') ]",
50+
"concatToParam": "[concat(parameters('inputValue'), '-addtoparam')]"
51+
}
52+
```
53+
54+
# [Bicep](#tab/bicep)
55+
56+
```bicep
57+
param inputValue string = 'deployment parameter'
58+
59+
var stringVar = 'myVariable'
60+
var concatToVar = '${stringVar}-addtovar'
61+
var concatToParam = '${inputValue}-addtoparam'
62+
```
63+
64+
---
65+
66+
You can use [template functions](template-functions.md) to construct the variable value.
67+
68+
In JSON templates, you can't use the [reference](template-functions-resource.md#reference) function or any of the [list](template-functions-resource.md#list) functions in the variable declaration. These functions get the runtime state of a resource, and can't be executed before deployment when variables are resolved.
69+
70+
The reference and list functions are valid when declaring a variable in a Bicep file.
71+
72+
The following example creates a string value for a storage account name. It uses several template functions to get a parameter value, and concatenates it to a unique string.
73+
74+
# [JSON](#tab/json)
2175

2276
```json
2377
"variables": {
2478
"storageName": "[concat(toLower(parameters('storageNamePrefix')), uniqueString(resourceGroup().id))]"
2579
},
2680
```
2781

82+
# [Bicep](#tab/bicep)
83+
84+
```bicep
85+
var storageName = '${toLower(storageNamePrefix)}${uniqueString(resourceGroup().id)}'
86+
```
87+
88+
---
89+
2890
## Use variable
2991

30-
In the template, you reference the value for the parameter by using the [variables](template-functions-deployment.md#variables) function. The following example shows how to use the variable for a resource property.
92+
The following example shows how to use the variable for a resource property.
93+
94+
# [JSON](#tab/json)
95+
96+
In a JSON template, you reference the value for the variable by using the [variables](template-functions-deployment.md#variables) function.
3197

3298
```json
3399
"resources": [
@@ -39,18 +105,47 @@ In the template, you reference the value for the parameter by using the [variabl
39105
]
40106
```
41107

108+
# [Bicep](#tab/bicep)
109+
110+
In a Bicep file, you reference the value for the variable by providing the variable name.
111+
112+
```bicep
113+
resource demoAccount 'Microsoft.Storage/storageAccounts@2019-06-01' = {
114+
name: storageName
115+
```
116+
117+
---
118+
42119
## Example template
43120

44-
The following template doesn't deploy any resources. It just shows some ways of declaring variables.
121+
The following template doesn't deploy any resources. It shows some ways of declaring variables of different types.
122+
123+
# [JSON](#tab/json)
45124

46125
:::code language="json" source="~/resourcemanager-templates/azure-resource-manager/variables.json":::
47126

127+
# [Bicep](#tab/bicep)
128+
129+
Bicep doesn't currently support loops.
130+
131+
:::code language="bicep" source="~/resourcemanager-templates/azure-resource-manager/variables.bicep":::
132+
133+
---
134+
48135
## Configuration variables
49136

50-
You can define variables that hold related values for configuring an environment. You define the variable as an object with the values. The following example shows an object that holds values for two environments - **test** and **prod**. You pass in one of these values during deployment.
137+
You can define variables that hold related values for configuring an environment. You define the variable as an object with the values. The following example shows an object that holds values for two environments - **test** and **prod**. Pass in one of these values during deployment.
138+
139+
# [JSON](#tab/json)
51140

52141
:::code language="json" source="~/resourcemanager-templates/azure-resource-manager/variablesconfigurations.json":::
53142

143+
# [Bicep](#tab/bicep)
144+
145+
:::code language="json" source="~/resourcemanager-templates/azure-resource-manager/variablesconfigurations.bicep":::
146+
147+
---
148+
54149
## Next steps
55150

56151
* To learn about the available properties for variables, see [Understand the structure and syntax of ARM templates](template-syntax.md).

0 commit comments

Comments
 (0)