部署链接和嵌套的 ARM 模板
随着部署的复杂性的增长,你可能希望使用 Azure 资源管理器(ARM) 链接或嵌套模板来部署资源的模块化方法。 链接模板和嵌套模板是将部署分解成许多相关模板的方法,然后通过主模板将它们一起部署。
链接的模板
链接模板 是指连接单独的模板文件(由主模板中的链接引用)的行为。 通过链接模板可以创建由多个单独 ARM 模板组成的可重用、可组合和模块化的部署。
引用链接模板时,必须提供可通过 HTTP 或 HTTPS 访问的 URI 值。 与上一个单元不同,我们可以将本地文件用作模板。
若要使用链接模板,必须先在可公开访问的终结点(如 GitHub 或 Azure Blob 存储)上暂存模板。 使用由共享访问签名 (SAS) 令牌保护的 Azure 存储帐户,使模板免受公共访问的安全。
若要将链接模板添加到 ARM 模板,请添加 Microsoft.Resources/deployments
资源和配置了模板位置的 templateLink
属性。
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"name": {
"type": "string",
"defaultValue": "linkeddemo001"
}
},
"variables": {
"linked-template": "https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/mslearn-arm-Module-sample/storage.json",
"linked-template-two": "https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/mslearn-arm-Module-sample/identity.json"
},
"resources": [
{
"name": "storage",
"type": "Microsoft.Resources/deployments",
"apiVersion": "2019-10-01",
"properties": {
"mode": "Incremental",
"templateLink": {
"uri": "[variables('linked-template')]",
"contentVersion": "1.0.0.0"
},
"parameters": {
"name": { "value": "[parameters('name')]" },
"___location": { "value": "[resourceGroup().___location]" }
}
}
},
{
"name": "identity",
"type": "Microsoft.Resources/deployments",
"apiVersion": "2019-10-01",
"dependsOn": [
"[resourceId('Microsoft.Resources/deployments','storage')]"
],
"properties": {
"mode": "Incremental",
"templateLink": {
"uri": "[variables('linked-template-two')]",
"contentVersion": "1.0.0.0"
},
"parameters": {
"name": { "value": "[parameters('name')]" },
"___location": { "value": "[resourceGroup().___location]" }
}
}
}
],
"outputs": {
"storageURI": {
"type": "string",
"value": "[reference('storage').outputs.storageEndpoint.value]"
}
}
}
如果需要,还可以将参数值传递到链接模板,并在部署时从链接模板获取输出。 参数可以通过参数文件或内联参数传递。
{
"name": "storage",
"type": "Microsoft.Resources/deployments",
"apiVersion": "2019-10-01",
"properties": {
"mode": "Incremental",
"templateLink": {
"uri": "[variables('linked-template')]",
"contentVersion": "1.0.0.0"
},
"parameters": {
"name": { "value": "[parameters('name')]" },
"___location": { "value": "[resourceGroup().___location]" }
}
}
}
对于中小型解决方案,单个模板更易于理解和维护。 可以查看单个文件中的所有资源和值。 对于高级方案,使用链接模板可将解决方案分解为目标组件。 可以轻松地将这些模板重复用于其他方案。
嵌套模板
嵌套模板 是指在主模板中嵌入模板语法的行为。 嵌套模板允许高级部署方案,例如从单个模板文件部署到多个 Azure 资源管理器范围或多个资源组。 与链接模板不同,其中每个模板都存储在其自己的模板文件中,嵌套模板允许将许多单个模板存储在一个文件中。 出于多种原因,你可能想要使用此模板结构,例如将资源部署到多个资源组或部署范围时。
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"storageAccountName": {
"type": "string"
}
},
"resources": [
{
"type": "Microsoft.Resources/deployments",
"apiVersion": "2019-10-01",
"name": "nestedTemplate1",
"properties": {
"mode": "Incremental",
"template": {
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2019-04-01",
"name": "[parameters('storageAccountName')]",
"___location": "West US",
"sku": {
"name": "Standard_LRS"
},
"kind": "StorageV2"
}
]
}
}
}
],
"outputs": {
}
}
使用嵌套模板时,可以指定在父模板或嵌套模板的范围内计算模板表达式。 范围决定了如何解析参数、变量和函数,如resourceGroup
和subscription
。