ARM嵌套模板:使用嵌套模板部署模板时,未正确填充_artifactLocation参数

问题描述 投票:0回答:1

我试图弄清楚嵌套模板是如何工作的,我有以下模板。我正在尝试使用VS部署机制从VS部署:

  1. 右键单击项目> Deploy> New
  2. “工件存储帐户”字段预先填充“自动创建存储帐户”,我就这样离开了
  3. 单击Deploy按钮

如果你看一下变量中的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”

  • 创建存储帐户,上载模板,参数和PS1脚本
  • 未在资源组/部署中创建新部署

如果我使用资源> 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"
    }
  }
}
azure azure-resource-manager
1个回答
1
投票

不幸的是,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'))]"

要么应该工作。

© www.soinside.com 2019 - 2024. All rights reserved.