我试图弄清楚嵌套模板是如何工作的,我有以下模板。我正在尝试使用VS部署机制从VS部署:
如果你看一下变量中的HelloWorldParent.json模板,你会看到两个变量“nestedTemplateUri”和“nestedTemplateUriWithBlobContainerName”。
我的理解是“nestedTemplateUri”应该包含“blob容器名称”,但似乎并非如此。
如果我使用资源> properties> templateLink>“uri”进行部署:“[variables('nestedTemplateUri')]”
错误:Code = InvalidContentLink;消息=无法从'https://********.blob.core.windows.net/NestedTemplates/HelloWorld.json下载部署内容?sv = 2017-07-29&sr = c&sig = ZCJAoOdp08qDWxbzKbXSZzX1VBCf7%2FNSt4aIznFCTPQ% 3D&SE = 2019-03-12T03:39:09Z& SP = R”
如果我使用资源> properties> templateLink>“uri”进行部署:“[variables('nestedTemplateUriWithBlobContainerName')]”
任何的想法?任何帮助都非常感谢!
HelloWorldParent.json
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"_artifactsLocation": {
"type": "string",
"metadata": {
"description": "The base URI where artifacts required by this template are located including a trailing '/'"
}
},
"_artifactsLocationSasToken": {
"type": "securestring",
"metadata": {
"description": "The sasToken required to access _artifactsLocation. When the template is deployed using the accompanying scripts, a sasToken will be automatically generated. Use the defaultValue if the staging location is not secured."
},
"defaultValue": ""
}
},
"variables": {
"blobContainerName": "[concat(resourceGroup().name, '-stageartifacts/')]",
"nestedTemplateUriWithBlobContainerName": "[uri(parameters('_artifactsLocation'), concat(variables('blobContainerName'), 'NestedTemplates/HelloWorld.json', parameters('_artifactsLocationSasToken')))]",
"nestedTemplateUri": "[uri(parameters('_artifactsLocation'), concat('NestedTemplates/HelloWorld.json', parameters('_artifactsLocationSasToken')))]"
},
"resources": [
{
"apiVersion": "2017-05-10",
"name": "linkedTemplate",
"type": "Microsoft.Resources/deployments",
"properties": {
"mode": "incremental",
"templateLink": {
"uri": "[variables('nestedTemplateUri')]",
"contentVersion": "1.0.0.0"
}
}
}
],
"outputs": {
"messageFromLinkedTemplate": {
"type": "string",
"value": "[reference('linkedTemplate').outputs.greetingMessage.value]"
},
"_artifactsLocation": {
"type": "string",
"value": "[parameters('_artifactsLocation')]"
}
}
}
HelloWorldParent.parameters.json
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
}
}
嵌套模板/ Hello World.json
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {},
"variables": {},
"resources": [],
"outputs": {
"greetingMessage": {
"value": "Hello World (1)",
"type": "string"
}
}
}
不幸的是,VS有点“过时”了它对你的场景的支持......问题是你正在使用URI函数而_artifactsLocation没有尾部斜杠。所以你有几个选择来修复:
1)在VS的PS1文件中有一行如下所示:
$OptionalParameters[$ArtifactsLocationName] = $StorageAccount.Context.BlobEndPoint + $StorageContainerName
如果将其更改为此(添加尾随/):
$OptionalParameters[$ArtifactsLocationName] = $StorageAccount.Context.BlobEndPoint + $StorageContainerName + "/"
它应该工作 - 或者你可以用这个替换整个脚本:https://github.com/Azure/azure-quickstart-templates/blob/master/Deploy-AzureResourceGroup.ps1
请注意,如果您有其他模板在没有尾部斜杠的情况下工作,那么这将是一个重大变化。
2)使用concat()创建uri而不是uri()函数。您仍然需要知道是否存在尾部斜杠,但此更改可以在模板中完成,而不是PS1文件。
"nestedTemplateUri": "[concat(parameters('_artifactsLocation'), '/NestedTemplates/HelloWorld.json', parameters('_artifactsLocationSasToken'))]"
要么应该工作。