ARM模板:嵌套模板中的输出问题

huangapple go评论95阅读模式
英文:

ARM template: issue with output in nested templates

问题

以下是您提供的内容的中文翻译:

  1. 我目前面临一个嵌套模板的问题。
  2. 当我应用我的模板(详见下文)时,我从Azure得到了以下答复:
  3. ```plaintext
  4. Azure错误:无效模板
  5. 消息:部署模板验证失败:'sandbox.test.portal'的模板引用无效:无法找到具有此名称的模板资源或资源副本。请参阅https://aka.ms/arm-template-expressions/#reference以获取使用详细信息。
  6. 然而,我不太明白为什么会出现这个问题,因为在嵌套模板内部的内容中,我使用了文档中提供的内容,文档链接在此处:https://github.com/Azure/azure-quickstart-templates/blob/master/101-azure-dns-new-zone/azuredeploy.json
  7. 我的ARM模板:
  8. ```plaintext
  9. {
  10. "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  11. "contentVersion": "1.0.0.0",
  12. "parameters": {
  13. "location": {
  14. "type": "string",
  15. "defaultValue": "[resourceGroup().location]",
  16. "metadata": {
  17. "description": "所有资源的位置。"
  18. }
  19. },
  20. "newZoneName": {
  21. "type": "string",
  22. "defaultValue": "sandbox.test.portal",
  23. "metadata": {
  24. "description": "要创建的DNS区域的名称。必须至少有2个段,例如hostname.org"
  25. }
  26. },
  27. "newRecordName": {
  28. "type": "string",
  29. "defaultValue": "www",
  30. "metadata": {
  31. "description": "要创建的DNS记录的名称。名称是相对于区域而不是FQDN的。"
  32. }
  33. }
  34. },
  35. "variables": {
  36. "publicIPAddressName": "[concat(resourceGroup().name, '-pip')]"
  37. },
  38. "resources": [
  39. {
  40. "apiVersion": "2015-06-15",
  41. "type": "Microsoft.Network/publicIPAddresses",
  42. "name": "[variables('publicIPAddressName')]",
  43. "location": "[parameters('location')]",
  44. "properties": {
  45. "publicIPAllocationMethod": "Dynamic",
  46. "dnsSettings": {
  47. "domainNameLabel": "[parameters('dnsLabelPrefix')]"
  48. }
  49. }
  50. },
  51. {
  52. "apiVersion": "2017-05-10",
  53. "name": "nestedTemplate",
  54. "type": "Microsoft.Resources/deployments",
  55. "resourceGroup": "my-rg",
  56. "subscriptionId": "[subscription().subscriptionId]",
  57. "properties": {
  58. "mode": "Incremental",
  59. "template": {
  60. "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  61. "contentVersion": "1.0.0.0",
  62. "parameters": {},
  63. "variables": {},
  64. "resources": [
  65. {
  66. "type": "Microsoft.Network/dnszones",
  67. "name": "[parameters('newZoneName')]",
  68. "apiVersion": "2016-04-01",
  69. "location": "global",
  70. "properties": {}
  71. },
  72. {
  73. "type": "Microsoft.Network/dnszones/a",
  74. "name": "[concat(parameters('newZoneName'), '/', parameters('newRecordName'))]",
  75. "apiVersion": "2016-04-01",
  76. "location": "global",
  77. "dependsOn": [
  78. "[parameters('newZoneName')]"
  79. ],
  80. "properties": {
  81. "TTL": 3600,
  82. "ARecords": [
  83. {
  84. "ipv4Address": "1.2.3.4"
  85. },
  86. {
  87. "ipv4Address": "1.2.3.5"
  88. }
  89. ]
  90. }
  91. }
  92. ],
  93. "outputs": {
  94. "nameServers": {
  95. "type": "array",
  96. "value": "[reference(parameters('newZoneName')).nameServers]"
  97. }
  98. }
  99. }
  100. }
  101. }
  102. ]
  103. }

希望这可以帮助您更好地理解问题。如果您有任何其他问题,请随时提出。

  1. <details>
  2. <summary>英文:</summary>
  3. I&#39;m currently facing an issue with nested template.
  4. When I&#39;m applying my template (detail below), I get this answer from Azure:

Azure Error: InvalidTemplate
Message: Deployment template validation failed: 'The template reference 'sandbox.test.portal' is not valid: could not find template resource or resource copy with this name. Please see https://aka.ms/arm-template-expressions/#reference for usage details.'.

  1. However, I don&#39;t really understand why I get this issue, because for the content inside the nested template, I used what they provide in the documentation here: https://github.com/Azure/azure-quickstart-templates/blob/master/101-azure-dns-new-zone/azuredeploy.json
  2. My ARM template:

{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"location": {
"type": "string",
"defaultValue": "[resourceGroup().location]",
"metadata": {
"description": "Location for all resources."
}
},
"newZoneName": {
"type": "string",
"defaultValue": "sandbox.test.portal",
"metadata": {
"description": "The name of the DNS zone to be created. Must have at least 2 segements, e.g. hostname.org"
}
},
"newRecordName": {
"type": "string",
"defaultValue": "www",
"metadata": {
"description": "The name of the DNS record to be created. The name is relative to the zone, not the FQDN."
}
}
},
"variables": {
"publicIPAddressName": "[concat(resourceGroup().name, '-pip')]",
},
"resources": [
{
"apiVersion": "2015-06-15",
"type": "Microsoft.Network/publicIPAddresses",
"name": "[variables('publicIPAddressName')]",
"location": "[parameters('location')]",
"properties": {
"publicIPAllocationMethod": "Dynamic",
"dnsSettings": {
"domainNameLabel": "[parameters('dnsLabelPrefix')]"
}
}
},
{
"apiVersion": "2017-05-10",
"name": "nestedTemplate",
"type": "Microsoft.Resources/deployments",
"resourceGroup": "my-rg",
"subscriptionId": "[subscription().subscriptionId]",
"properties": {
"mode": "Incremental",
"template": {
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
},
"variables": {
},
"resources": [
{
"type": "Microsoft.Network/dnszones",
"name": "[parameters('newZoneName')]",
"apiVersion": "2016-04-01",
"location": "global",
"properties": {
}
},
{
"type": "Microsoft.Network/dnszones/a",
"name": "[concat(parameters('newZoneName'), '/', parameters('newRecordName'))]",
"apiVersion": "2016-04-01",
"location": "global",
"dependsOn": [
"[parameters('newZoneName')]"
],
"properties": {
"TTL": 3600,
"ARecords": [
{
"ipv4Address": "1.2.3.4"
},
{
"ipv4Address": "1.2.3.5"
}
]
}
}
],
"outputs": {
"nameServers": {
"type": "array",
"value": "[reference(parameters('newZoneName')).nameServers]"
}
}
}
}
}
]
}

  1. </details>
  2. # 答案1
  3. **得分**: 1
  4. 基本上,你需要从嵌套的内联模板中移除输出部分,因此移除这部分内容:
  5. ```json
  6. "outputs": {
  7. "nameServers": {
  8. "type": "array",
  9. "value": "[reference(parameters('newZoneName')).nameServers]"
  10. }
  11. }

简而言之,嵌套内联部署不推荐使用。

或者,将这些内容移到父模板并进行真正的查找:

  1. reference(resourceId('Microsoft.Network/dnszones', parameters('newZoneName')))
英文:

basically, you need to remove the outputs from the nested inline template, so remove this bit:

  1. &quot;outputs&quot;: {
  2. &quot;nameServers&quot;: {
  3. &quot;type&quot;: &quot;array&quot;,
  4. &quot;value&quot;: &quot;[reference(parameters(&#39;newZoneName&#39;)).nameServers]&quot;
  5. }
  6. }

long story short, nested inline deployments are bad. dont use them.

alternatively move those to the parent template and do a real lookup:

  1. reference(resourceId(&#39;Microsoft.Network/dnszones&#39;, parameters(&#39;newZoneName&#39;)))

答案2

得分: 1

你的模板中有一些小错误:

  1. 在变量“-pip”中有逗号。
  2. 未定义参数“dnsLabelPrefix”。

在嵌套输出方面存在一般性错误。当你在嵌套模板中使用时,Azure 在主模板中找不到它。因此,你必须使用引用函数以及标识符和API:"[reference(resourceId('Microsoft.Network/dnszones', parameters('newZoneName')), '2016-04-01').nameServers]"

我修改了你的模板并在我的订阅中验证了它。祝你有美好的一天!

英文:

You have several minor mistakes in your template:

  1. Comma in variables '-pip')]",
  2. Undefined parameter dnsLabelPrefix

The general mistake in nested outputs. When you use nested templates azure don't find it in your main template. Therefore you must use a reference function with identifier and API: &quot;[reference(resourceId(&#39;Microsoft.Network/dnszones&#39;, parameters(&#39;newZoneName&#39;)), &#39;2016-04-01&#39;).nameServers]&quot;.

I modified your template and validate in my subscription.

  1. {
  2. &quot;$schema&quot;: &quot;https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#&quot;,
  3. &quot;contentVersion&quot;: &quot;1.0.0.0&quot;,
  4. &quot;parameters&quot;: {
  5. &quot;newZoneName&quot;: {
  6. &quot;type&quot;: &quot;string&quot;,
  7. &quot;defaultValue&quot;: &quot;sandbox.test.portal&quot;,
  8. &quot;metadata&quot;: {
  9. &quot;description&quot;: &quot;The name of the DNS zone to be created. Must have at least 2 segements, e.g. hostname.org&quot;
  10. }
  11. },
  12. &quot;newRecordName&quot;: {
  13. &quot;type&quot;: &quot;string&quot;,
  14. &quot;defaultValue&quot;: &quot;www&quot;,
  15. &quot;metadata&quot;: {
  16. &quot;description&quot;: &quot;The name of the DNS record to be created. The name is relative to the zone, not the FQDN.&quot;
  17. }
  18. },
  19. &quot;dnsLabelPrefix&quot;: {
  20. &quot;type&quot;: &quot;string&quot;,
  21. &quot;defaultValue&quot;: &quot;[concat(&#39;dns&#39;,uniqueString(resourceGroup().name))]&quot;
  22. },
  23. &quot;nestedResourceGroup&quot;: {
  24. &quot;type&quot;: &quot;string&quot;,
  25. &quot;defaultValue&quot;: &quot;my-rg&quot;,
  26. &quot;metadata&quot;: {
  27. &quot;description&quot;: &quot;my-rg&quot;
  28. }
  29. }
  30. },
  31. &quot;variables&quot;: {
  32. &quot;publicIPAddressName&quot;: &quot;[concat(resourceGroup().name, &#39;-pip&#39;)]&quot;
  33. },
  34. &quot;resources&quot;: [
  35. {
  36. &quot;apiVersion&quot;: &quot;2015-06-15&quot;,
  37. &quot;type&quot;: &quot;Microsoft.Network/publicIPAddresses&quot;,
  38. &quot;name&quot;: &quot;[variables(&#39;publicIPAddressName&#39;)]&quot;,
  39. &quot;location&quot;: &quot;[resourceGroup().location]&quot;,
  40. &quot;properties&quot;: {
  41. &quot;publicIPAllocationMethod&quot;: &quot;Dynamic&quot;,
  42. &quot;dnsSettings&quot;: {
  43. &quot;domainNameLabel&quot;: &quot;[parameters(&#39;dnsLabelPrefix&#39;)]&quot;
  44. }
  45. }
  46. },
  47. {
  48. &quot;apiVersion&quot;: &quot;2017-05-10&quot;,
  49. &quot;name&quot;: &quot;nestedTemplate&quot;,
  50. &quot;type&quot;: &quot;Microsoft.Resources/deployments&quot;,
  51. &quot;resourceGroup&quot;: &quot;[parameters(&#39;nestedResourceGroup&#39;)]&quot;,
  52. &quot;subscriptionId&quot;: &quot;[subscription().subscriptionId]&quot;,
  53. &quot;properties&quot;: {
  54. &quot;mode&quot;: &quot;Incremental&quot;,
  55. &quot;template&quot;: {
  56. &quot;$schema&quot;: &quot;https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#&quot;,
  57. &quot;contentVersion&quot;: &quot;1.0.0.0&quot;,
  58. &quot;parameters&quot;: {
  59. },
  60. &quot;variables&quot;: {
  61. },
  62. &quot;resources&quot;: [
  63. {
  64. &quot;type&quot;: &quot;Microsoft.Network/dnszones&quot;,
  65. &quot;name&quot;: &quot;[parameters(&#39;newZoneName&#39;)]&quot;,
  66. &quot;apiVersion&quot;: &quot;2016-04-01&quot;,
  67. &quot;location&quot;: &quot;global&quot;,
  68. &quot;properties&quot;: {
  69. }
  70. },
  71. {
  72. &quot;type&quot;: &quot;Microsoft.Network/dnszones/a&quot;,
  73. &quot;name&quot;: &quot;[concat(parameters(&#39;newZoneName&#39;), &#39;/&#39;, parameters(&#39;newRecordName&#39;))]&quot;,
  74. &quot;apiVersion&quot;: &quot;2016-04-01&quot;,
  75. &quot;location&quot;: &quot;global&quot;,
  76. &quot;dependsOn&quot;: [
  77. &quot;[resourceId(&#39;Microsoft.Network/dnszones&#39;, parameters(&#39;newZoneName&#39;))]&quot;
  78. ],
  79. &quot;properties&quot;: {
  80. &quot;TTL&quot;: 3600,
  81. &quot;ARecords&quot;: [
  82. {
  83. &quot;ipv4Address&quot;: &quot;1.2.3.4&quot;
  84. },
  85. {
  86. &quot;ipv4Address&quot;: &quot;1.2.3.5&quot;
  87. }
  88. ]
  89. }
  90. }
  91. ],
  92. &quot;outputs&quot;: {
  93. &quot;nameServers&quot;: {
  94. &quot;type&quot;: &quot;array&quot;,
  95. &quot;value&quot;: &quot;[reference(resourceId(&#39;Microsoft.Network/dnszones&#39;, parameters(&#39;newZoneName&#39;)), &#39;2016-04-01&#39;).nameServers]&quot;
  96. }
  97. }
  98. }
  99. }
  100. }
  101. ]
  102. }

Have a nice day!

huangapple
  • 本文由 发表于 2020年1月3日 23:50:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/59581479.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定