繁体   English   中英

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

[英]ARM template: issue with output in nested templates

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

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

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

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

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

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

您的模板中有几个小错误:

  1. 变量中的逗号 '-pip')]" ,
  2. 未定义的参数dnsLabelPrefix

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

我修改了您的模板并在我的订阅中进行了验证。

    {
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "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."
            }
        },
        "dnsLabelPrefix": {
            "type": "string",
            "defaultValue": "[concat('dns',uniqueString(resourceGroup().name))]"
        },
        "nestedResourceGroup": {
            "type": "string",
            "defaultValue": "my-rg",
            "metadata": {
                "description": "my-rg"
            }
        }
    },
    "variables": {
        "publicIPAddressName": "[concat(resourceGroup().name, '-pip')]"
    },
    "resources": [
        {
            "apiVersion": "2015-06-15",
            "type": "Microsoft.Network/publicIPAddresses",
            "name": "[variables('publicIPAddressName')]",
            "location": "[resourceGroup().location]",
            "properties": {
                "publicIPAllocationMethod": "Dynamic",
                "dnsSettings": {
                    "domainNameLabel": "[parameters('dnsLabelPrefix')]"
                }
            }
        },
        {
            "apiVersion": "2017-05-10",
            "name": "nestedTemplate",
            "type": "Microsoft.Resources/deployments",
            "resourceGroup": "[parameters('nestedResourceGroup')]",
            "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": [
                                "[resourceId('Microsoft.Network/dnszones', parameters('newZoneName'))]"
                            ],
                            "properties": {
                                "TTL": 3600,
                                "ARecords": [
                                    {
                                        "ipv4Address": "1.2.3.4"
                                    },
                                    {
                                        "ipv4Address": "1.2.3.5"
                                    }
                                ]
                            }
                        }
                    ],
                    "outputs": {
                        "nameServers": {
                            "type": "array",
                            "value": "[reference(resourceId('Microsoft.Network/dnszones', parameters('newZoneName')), '2016-04-01').nameServers]"
                        }
                    }
                }
            }
        }
    ]
}

祝你今天过得愉快!

暂无
暂无

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

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