首页 > 解决方案 > ARM 模板:嵌套模板中的输出问题

问题描述

我目前面临嵌套模板的问题。当我应用我的模板时(详情如下),我从 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.'.

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

我的 ARM 模板:

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

标签: azureazure-devopsarm-template

解决方案


基本上,您需要从嵌套的内联模板中删除输出,因此请删除此位:

"outputs": {
    "nameServers": {
        "type": "array",
        "value": "[reference(parameters('newZoneName')).nameServers]"
    }
}

长话短说,嵌套的内联部署很糟糕。不要使用它们。

或者将它们移动到父模板并进行真正的查找:

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

推荐阅读