You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/azure-resource-manager/templates/linked-templates.md
+80-56Lines changed: 80 additions & 56 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ ms.date: 12/11/2019
6
6
---
7
7
# Using linked and nested templates when deploying Azure resources
8
8
9
-
To deploy complex solutions, you can break your template into many related templates, and then deploy them together through a main template. The related templates can be separate files or template syntax that is embedded within the main template. This article uses the term **linked template** to refer to a separate template file that is linked to from the main template. It uses the term **nested template** to refer to embedded template syntax within the main template.
9
+
To deploy complex solutions, you can break your template into many related templates, and then deploy them together through a main template. The related templates can be separate files or template syntax that is embedded within the main template. This article uses the term **linked template** to refer to a separate template file that is referenced via a link from the main template. It uses the term **nested template** to refer to embedded template syntax within the main template.
10
10
11
11
For small to medium solutions, a single template is easier to understand and maintain. You can see all the resources and values in a single file. For advanced scenarios, linked templates enable you to break down the solution into targeted components. You can easily reuse these templates for other scenarios.
12
12
@@ -86,11 +86,11 @@ The following example deploys a storage account through a nested template.
86
86
}
87
87
```
88
88
89
-
### Scope for expressions in nested templates
89
+
### Expression evaluation scope in nested templates
90
90
91
91
When using a nested template, you can specify whether template expressions are evaluated within the scope of the parent template or the nested template. The scope determines how parameters, variables, and functions like [resourceGroup](template-functions-resource.md#resourcegroup) and [subscription](template-functions-resource.md#subscription) are resolved.
92
92
93
-
You set the scope through the `expressionEvaluationOptions` property. By default, the `expressionEvaluationOptions` property is set to `outer`, which means it uses the parent template scope. Set the value to `inner` to scope expressions to the nested template.
93
+
You set the scope through the `expressionEvaluationOptions` property. By default, the `expressionEvaluationOptions` property is set to `outer`, which means it uses the parent template scope. Set the value to `inner` to cause expressions to be evaluated within the scope of the nested template.
94
94
95
95
```json
96
96
{
@@ -152,14 +152,14 @@ The following template demonstrates how template expressions are resolved accord
152
152
}
153
153
```
154
154
155
-
The value of the variable changes based on the scope. The following table shows the results for both scopes.
155
+
The value of `exampleVar` changes depending on the value of the `scope` property in `expressionEvaluationOptions`. The following table shows the results for both scopes.
The following example deploys a SQL server and retrieves a key vault secret to use for the password. The scope is set to `inner` because it dynamically creates the key vault ID and passes it as a parameter to the nested template.
162
+
The following example deploys a SQL server and retrieves a key vault secret to use for the password. The scope is set to `inner` because it dynamically creates the key vault ID (see `adminPassword.reference.keyVault` in the outer templates `parameters`) and passes it as a parameter to the nested template.
163
163
164
164
```json
165
165
{
@@ -209,6 +209,22 @@ The following example deploys a SQL server and retrieves a key vault secret to u
@@ -271,7 +271,7 @@ The following example deploys a SQL server and retrieves a key vault secret to u
271
271
272
272
> [!NOTE]
273
273
>
274
-
> When scope is set to `outer`, you can't use the `reference` function in the outputs section of a nested template for a resource you have deployed in the nested template. To return the values for a deployed resource in a nested template, either use inner scope or convert your nested template to a linked template.
274
+
> When scope is set to `outer`, you can't use the `reference` function in the outputs section of a nested template for a resource you have deployed in the nested template. To return the values for a deployed resource in a nested template, either use `inner` scope or convert your nested template to a linked template.
275
275
276
276
## Linked template
277
277
@@ -302,9 +302,18 @@ To link a template, add a [deployments resource](/azure/templates/microsoft.reso
302
302
}
303
303
```
304
304
305
-
You can't specify a local file or a file that is only available on your local network. You can only provide a URI value that includes either **http** or **https**. Resource Manager must be able to access the template. One option is to place your linked template in a storage account, and use the URI for that item.
305
+
When referencing a linked template, the value of `uri` must not be a local file or a file that is only available on your local network. You must provide a URI value that downloadable as **http** or **https**.
306
+
307
+
> [!NOTE]
308
+
>
309
+
> You may reference templates using parameters that ultimately resolve
310
+
> to something that uses **http** or **https**, for example, using the
You don't have to provide the `contentVersion` property for the template or parameters. If you don't provide a content version value, the current version of the template is deployed. If you provide a value for content version, it must match the version in the linked template; otherwise, the deployment fails with an error.
315
+
316
+
Resource Manager must be able to access the template. One option is to place your linked template in a storage account, and use the URI for that item.
308
317
309
318
### Parameters for linked template
310
319
@@ -319,12 +328,12 @@ You can provide the parameters for your linked template either in an external fi
@@ -355,6 +364,41 @@ To pass parameter values inline, use the **parameters** property.
355
364
356
365
You can't use both inline parameters and a link to a parameter file. The deployment fails with an error when both `parametersLink` and `parameters` are specified.
357
366
367
+
## `contentVersion`
368
+
369
+
You don't have to provide the `contentVersion` property for the `templateLink` or `parametersLink` property. If you don't provide a `contentVersion`, the current version of the template is deployed. If you provide a value for content version, it must match the version in the linked template; otherwise, the deployment fails with an error.
370
+
371
+
## Using variables to link templates
372
+
373
+
The previous examples showed hard-coded URL values for the template links. This approach might work for a simple template, but it doesn't work well for a large set of modular templates. Instead, you can create a static variable that stores a base URL for the main template and then dynamically create URLs for the linked templates from that base URL. The benefit of this approach is that you can easily move or fork the template because you need to change only the static variable in the main template. The main template passes the correct URIs throughout the decomposed template.
374
+
375
+
The following example shows how to use a base URL to create two URLs for linked templates (**sharedTemplateUrl** and **vmTemplate**).
You can also use [deployment()](template-functions-deployment.md#deployment) to get the base URL for the current template, and use that to get the URL for other templates in the same location. This approach is useful if your template location changes or you want to avoid hard coding URLs in the template file. The templateLink property is only returned when linking to a remote template with a URL. If you're using a local template, that property isn't available.
Ultimately, you would use the variable in the `uri` property of a `templateLink` property.
394
+
395
+
```json
396
+
"templateLink": {
397
+
"uri": "[variables('sharedTemplateUrl')]",
398
+
"contentVersion":"1.0.0.0"
399
+
}
400
+
```
401
+
358
402
## Using copy
359
403
360
404
To create multiple instances of a resource with a nested template, add the copy element at the level of the **Microsoft.Resources/deployments** resource. Or, if the scope is inner, you can add the copy within the nested template.
@@ -404,35 +448,13 @@ The following example template shows how to use copy with a nested template.
404
448
]
405
449
```
406
450
407
-
## Using variables to link templates
408
-
409
-
The previous examples showed hard-coded URL values for the template links. This approach might work for a simple template but it doesn't work well when working with a large set of modular templates. Instead, you can create a static variable that stores a base URL for the main template and then dynamically create URLs for the linked templates from that base URL. The benefit of this approach is you can easily move or fork the template because you only need to change the static variable in the main template. The main template passes the correct URIs throughout the decomposed template.
410
-
411
-
The following example shows how to use a base URL to create two URLs for linked templates (**sharedTemplateUrl** and **vmTemplate**).
You can also use [deployment()](template-functions-deployment.md#deployment) to get the base URL for the current template, and use that to get the URL for other templates in the same location. This approach is useful if your template location changes or you want to avoid hard coding URLs in the template file. The templateLink property is only returned when linking to a remote template with a URL. If you're using a local template, that property isn't available.
To get an output value from a linked template, retrieve the property value with syntax like: `"[reference('deploymentName').outputs.propertyName.value]"`.
432
454
433
-
When getting an output property from a linked template, the property name can't include a dash.
455
+
When getting an output property from a linked template, the property name must not include a dash.
434
456
435
-
The following examples demonstrate how to reference a linked template and retrieve an output value. The linked template returns a simple message.
457
+
The following examples demonstrate how to reference a linked template and retrieve an output value. The linked template returns a simple message. First, the linked template:
436
458
437
459
```json
438
460
{
@@ -481,9 +503,9 @@ The main template deploys the linked template and gets the returned value. Notic
481
503
}
482
504
```
483
505
484
-
Like other resource types, you can set dependencies between the linked template and other resources. When other resources require an output value from the linked template, make sure the linked template is deployed before them. Or, when the linked template relies on other resources, make sure other resources are deployed before the linked template.
506
+
As with other resource types, you can set dependencies between the linked template and other resources. When other resources require an output value from the linked template, make sure the linked template is deployed before them. Or, when the linked template relies on other resources, make sure other resources are deployed before the linked template.
485
507
486
-
The following example shows a template that deploys a public IP address and returns the resource ID:
508
+
The following example shows a template that deploys a public IP address and returns the resource ID of the Azure resource for that public IP:
487
509
488
510
```json
489
511
{
@@ -518,7 +540,7 @@ The following example shows a template that deploys a public IP address and retu
518
540
}
519
541
```
520
542
521
-
To use the public IP address from the preceding template when deploying a load balancer, link to the template and add a dependency on the deployment resource. The public IP address on the load balancer is set to the output value from the linked template.
543
+
To use the public IP address from the preceding template when deploying a load balancer, link to the template and declare a dependency on the `Microsoft.Resources/deployments` resource. The public IP address on the load balancer is set to the output value from the linked template.
522
544
523
545
```json
524
546
{
@@ -548,6 +570,7 @@ To use the public IP address from the preceding template when deploying a load b
548
570
"properties": {
549
571
"privateIPAllocationMethod": "Dynamic",
550
572
"publicIPAddress": {
573
+
// this is where the output value from linkedTemplate is used
0 commit comments