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

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

ARM template: issue with output in nested templates

问题

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

我目前面临一个嵌套模板的问题。
当我应用我的模板(详见下文)时,我从Azure得到了以下答复:

```plaintext
Azure错误:无效模板
消息:部署模板验证失败:'sandbox.test.portal'的模板引用无效:无法找到具有此名称的模板资源或资源副本。请参阅https://aka.ms/arm-template-expressions/#reference以获取使用详细信息。

然而,我不太明白为什么会出现这个问题,因为在嵌套模板内部的内容中,我使用了文档中提供的内容,文档链接在此处:https://github.com/Azure/azure-quickstart-templates/blob/master/101-azure-dns-new-zone/azuredeploy.json


我的ARM模板:

```plaintext
{
    "$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": "所有资源的位置。"
            }
        },
        "newZoneName": {
            "type": "string",
            "defaultValue": "sandbox.test.portal",
            "metadata": {
                "description": "要创建的DNS区域的名称。必须至少有2个段,例如hostname.org"
            }
        },
        "newRecordName": {
            "type": "string",
            "defaultValue": "www",
            "metadata": {
                "description": "要创建的DNS记录的名称。名称是相对于区域而不是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]"
                        }
                    }
                }
            }
        }
    ]
}

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


<details>
<summary>英文:</summary>
I&#39;m currently facing an issue with nested template.
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.'.


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
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]"
}
}
}
}
}
]
}


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

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

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

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

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

&quot;outputs&quot;: {
&quot;nameServers&quot;: {
&quot;type&quot;: &quot;array&quot;,
&quot;value&quot;: &quot;[reference(parameters(&#39;newZoneName&#39;)).nameServers]&quot;
}
}

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

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

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.

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

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:

确定