我试图弄清楚嵌套模板是如何工作的,我有以下模板。我正在尝试使用 VS 部署机制从 VS 进行部署:
- 右键单击项目 > 部署 > 新建
- “工件存储帐户”字段已预先填充“自动创建存储帐户”,我就这样保留
- 点击“部署”按钮
如果您查看 HelloWorldParent.json 模板中的变量,您将看到两个变量“nestedTemplateUri”和“nestedTemplateUriWithBlobContainerName”。
据我了解,“nestedTemplateUri”应包含“blob 容器名称”,但情况似乎并非如此。
如果我使用资源 > 属性 > templateLink > "uri": "[variables('nestedTemplateUri')]"进行部署
- 部署失败并显示:
Error: Code=InvalidContentLink; Message=Unable to download deployment content from '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'
- 存储帐户已创建,模板、参数和 PS1 脚本已上传
- 未在资源组/部署中创建新部署
如果我使用资源 > 属性 > 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": {
}
}
NestedTemplates/HelloWorld.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'))]"
两者都应该有效。
关于azure - ARM嵌套模板: _artifactLocation parameter is not populated correctly when deploying template with nested templates,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55112342/