繁体   English   中英

ARM嵌套模板:部署带有嵌套模板的模板时,_artifactLocation参数未正确填充

[英]ARM nested templates: _artifactLocation parameter is not populated correctly when deploying template with nested templates

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

  1. 右键单击项目>部署>新建
  2. “工件存储帐户”字段中预填充了“自动创建存储帐户”,我将其保留为
  3. 单击部署按钮

如果您在变量的HelloWorldParent.json模板中查看,您将看到两个变量“ nestedTemplateUri”和“ nestedTemplateUriWithBlobContainerName”。

据我了解,“ nestedTemplateUri”应包含“ blob容器名称”,但事实并非如此。

如果我使用资源>属性> templateLink>“ uri”进行部署:“ [variables('nestedTemplateUri')]”

  • 部署失败,并显示以下信息:

错误:代码= 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脚本
  • 没有在资源组/部署中创建新的部署

如果我使用资源>属性> 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'))]"

两者都应该起作用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM