如何在嵌套模板资源中使用copyIndex?

问题描述 投票:0回答:1

大图:我想使用ARM模板在服务总线上创建多个主题。

已知的事实:部署模板的应用程序服务与服务总线位于不同的资源组中。

痛点:我使用的是嵌套模板,因为我正在尝试创建目标资源组external的资源(主题)。在此嵌套模板中,我不确定如何使副本正常工作。

this MS doc开始,我相信我的语法是正确的。

这是我的参数的列出方式:

        "sharedResourcesResourceGroupName": {
            "type": "string",
            "defaultValue": "sharedResourceGroupName",
            "metadata": {
                "description": "Resource Group in which platform shared resources live"
            }
        },
        "serviceBusNamespaceName": {
            "type": "string",
            "defaultValue": "serviceBusName",
            "metadata": {
                "description": "Name of the Service Bus namespace"
            }
        },
        "topics": {
            "type": "array",
            "metadata": {
                "description": "List of topics"
            },
            "defaultValue": [
                "topic1",
                "topic2"
            ]
        }

这是我的用于使用copyIndex()方法创建主题的资源对象:

 {
            "apiVersion": "2018-05-01",
            "type": "Microsoft.Resources/deployments",
            "name": "[concat(parameters('serviceBusNamespaceName'))]",
            "resourceGroup": "[parameters('sharedResourcesResourceGroupName')]",
            "properties": {
                "mode": "Incremental",
                "template":{
                    "$schema": "2018-05-01",
                    "contentVersion": "1.0.0.0",
                    "parameters": {},
                    "variables": {},
                    "resources": [
                        {
                            "type": "Microsoft.ServiceBus/namespaces/topics",
                            "name": "[concat(parameters('serviceBusNamespaceName'), '/', parameters('topics')[copyIndex()])]",
                            "apiVersion": "2017-04-01",
                            "location": "[resourceGroup().location]",
                            "properties": {},
                            "copy": {
                                "name": "topics",
                                "count": "[length(parameters('topics'))]"
                            },
                            "dependsOn": [
                                "[parameters('serviceBusNamespaceName')]"
                            ]
                        }
                    ]
                }     
            }
        }

我正在通过以下命令使用Azure Powershell测试手臂模板部署:

Connect-AzAccount
Set-AZContext -SubscriptionName subscriptionWhereTheAppServiceLives
New-AzResourceGroupDeployment -ResourceGroupName resourceGroupWhereAppServiceLives -TemplateFile <path to template file>\azuredeploy.json -TemplateParameterFile <path to parameters file>\azuredeploy.parameters.json

我从Azure Powershell控制台收到的错误是:'在此位置不应期望模板函数'copyIndex'。该功能只能在指定了副本的资源中使用。

如果删除“复制”对象并将“名称”替换为"[concat(parameters('serviceBusNamespaceName'), '/topicName')]",则模板可以在正确的服务总线中创建一个主题。但我希望创建多个主题。

任何见识将不胜感激!

azure azure-resource-manager arm-template azure-servicebus-topics
1个回答
0
投票

我认为您可以这样做:

{
    "apiVersion": "2018-05-01",
    "type": "Microsoft.Resources/deployments",
    "name": "[concat(parameters('serviceBusNamespaceName'), copyIndex())]",
    "resourceGroup": "[parameters('sharedResourcesResourceGroupName')]",
    "copy": {
        "name": "topics",
        "count": "[length(parameters('topics'))]"
    },
    "dependsOn": [
        "[parameters('serviceBusNamespaceName')]"
    ],
    "properties": {
        "mode": "Incremental",
        "template": {
            "$schema": "2018-05-01",
            "contentVersion": "1.0.0.0",
            "resources": [
                {
                    "type": "Microsoft.ServiceBus/namespaces/topics",
                    "name": "[concat(parameters('serviceBusNamespaceName'), '/', parameters('topics')[copyIndex()])]",
                    "apiVersion": "2017-04-01",
                    "location": "[resourceGroup().location]",
                    "properties": {}
                }
            ]
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.