使用 copyIndex 时如何获取 ResourceId 作为 VM 的输出 [英] How to get ResourceId as output for VM when using copyIndex

查看:15
本文介绍了使用 copyIndex 时如何获取 ResourceId 作为 VM 的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 copyIndex(0) 创建多个虚拟机资源(以及公共 IP 地址、网卡...)

I use copyIndex(0) to create several virtualMachine resources (along with publicIP addresses, nic...)

我需要资源 ID 作为部署的输出以进行进一步处理.通常我使用 resourceId() 函数执行此操作,但由于名称是动态的并且 copyIndex 在输出部分中无效,因此我无法找出正确的语法:

I need the resourceID as output from the deployment for further processing. Usually I do this with the resourceId() function, but since the names are dynamic and copyIndex is not valid in outputs section, I can't figure out the proper syntax for this:

{
  "code": "DeploymentOutputEvaluationFailed",
  "message": "Unable to evaluate template outputs: 'resourceID'. Please see error details and deployment operations. Please see https://aka.ms/arm-debug for usage details.",
  "details": [
    {
      "code": "DeploymentOutputEvaluationFailed",
      "target": "resourceID",
      "message": "The template output 'resourceID' is not valid: The template function 'copyIndex' is not expected at this location. The function can only be used in a resource with copy specified. Please see https://aka.ms/arm-copy for usage details.."
    }
  ]
}

我想我需要将 resourceID 更改为数组,但是获取动态创建的 VM 的 resourceId 的正确语法是什么?

I guess I need to change resourceID to array, but what is the proper syntax for fetching the resourceId of the dynamically created VMs?

完整的ARM模板如下:

Full ARM template below:

{
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "virtualMachineNamePrefix": {
            "type": "string"
        },
        "virtualMachineSize": {
            "type": "string"
        },
        "virtualMachineCount": {
            "type": "int"
        },
        "adminUsername": {
            "type": "string"
        },
        "adminPassword": {
            "type": "secureString"
        }
    },
    "variables": {
        "resourceGroupName": "[toLower(ResourceGroup().name)]",
        "location": "[resourceGroup().location]",
        "networkSecurityGroupName": "[concat(variables('resourceGroupName'), '-nsg')]",
        "nsgId": "[resourceId(resourceGroup().name, 'Microsoft.Network/networkSecurityGroups', variables('networkSecurityGroupName'))]",
        "subnetName": "default",
        "virtualNetworkId": "[concat('/subscriptions/', subscription().subscriptionId, '/resourceGroups/', ResourceGroup().name, '/providers/Microsoft.Network/virtualNetworks/', variables('resourceGroupName'), '-vnet')]",
        "operatingSystem": "Server2016",
        "operatingSystemValues": {
            "Server2016": {
                "PublisherValue": "MicrosoftWindowsServer",
                "OfferValue": "WindowsServer",
                "SkuValue": "2016-Datacenter"
            }
        },
        "subnetRef": "[concat(variables('virtualNetworkId'), '/subnets/', variables('subnetName'))]"
    },
    "resources": [
        {
            "apiVersion": "2016-03-30",
            "type": "Microsoft.Network/publicIPAddresses",
            "name": "[concat(parameters('virtualMachineNamePrefix'), copyIndex(0), '-ip')]",
            "location": "[variables('location')]",
            "copy": {
                "name": "PIPCopy",
                "count": "[parameters('virtualMachineCount')]"
            },
            "tags": {
                "displayName": "[concat(parameters('virtualMachineNamePrefix'), copyIndex(0), '-ip')]"
            },
            "properties": {
                "publicIPAllocationMethod": "Dynamic"
            }
        },
        {
            "name": "[concat(parameters('virtualMachineNamePrefix'), copyIndex(0), '-nic')]",
            "type": "Microsoft.Network/networkInterfaces",
            "apiVersion": "2016-03-30",
            "location": "[variables('location')]",
            "copy": {
                "name": "NICCopy",
                "count": "[parameters('virtualMachineCount')]"
            },
            "dependsOn": [
                "[concat('Microsoft.Network/publicIpAddresses/', parameters('virtualMachineNamePrefix'), copyIndex(0), '-ip')]"
            ],
            "properties": {
                "ipConfigurations": [
                    {
                        "name": "ipconfig1",
                        "properties": {
                            "privateIPAllocationMethod": "Dynamic",
                            "publicIPAddress": {
                                "id": "[resourceId('Microsoft.Network/publicIPAddresses', concat(parameters('virtualMachineNamePrefix'), copyIndex(0), '-ip'))]"
                            },
                            "subnet": {
                                "id": "[variables('subnetRef')]"
                            }
                        }
                    }
                ],
                "networkSecurityGroup": {
                    "id": "[variables('nsgId')]"
                }
            }
        },
        {
            "type": "Microsoft.Compute/virtualMachines",
            "name": "[concat(parameters('virtualMachineNamePrefix'), copyIndex(0))]",
            "apiVersion": "2017-03-30",
            "location": "[variables('location')]",
            "identity": {
                "type": "SystemAssigned"
            },
            "copy": {
                "name": "VMcopy",
                "count": "[parameters('virtualMachineCount')]"
            },
            "properties": {
                "hardwareProfile": {
                    "vmSize": "[parameters('virtualMachineSize')]"
                },
                "storageProfile": {
                    "imageReference": {
                        "publisher": "[variables('operatingSystemValues')[variables('operatingSystem')].PublisherValue]",
                        "offer": "[variables('operatingSystemValues')[variables('operatingSystem')].OfferValue]",
                        "sku": "[variables('operatingSystemValues')[variables('operatingSystem')].SkuValue]",
                        "version": "latest"
                    },
                    "osDisk": {
                        "name": "[concat(parameters('virtualMachineNamePrefix'),copyIndex(0), '-disk')]",
                        "createOption": "FromImage",
                        "managedDisk": {
                            "storageAccountType": "Premium_LRS"
                        },
                        "caching": "ReadWrite"
                    }
                },
                "osProfile": {
                    "computerName": "[concat(parameters('virtualMachineNamePrefix'),copyIndex(0))]",
                    "adminUsername": "[parameters('adminUsername')]",
                    "windowsConfiguration": {
                        "provisionVMAgent": true
                    },
                    "secrets": [],
                    "adminPassword": "[parameters('adminPassword')]"
                },
                "networkProfile": {
                    "networkInterfaces": [
                        {
                            "id": "[resourceId('Microsoft.Network/networkInterfaces', concat(parameters('virtualMachineNamePrefix'), copyIndex(0), '-nic'))]"
                        }
                    ]
                }
            },
            "dependsOn": [
            "[concat('Microsoft.Network/networkInterfaces/', parameters('virtualMachineNamePrefix'), copyIndex(0), '-nic')]"
            ]
        }
    ],
    "outputs": {
        "resourceID": {
            "type": "string",
            "value": "[resourceId('Microsoft.Network/networkInterfaces', concat(parameters('virtualMachineNamePrefix'), copyIndex(0)))]"
        }
    }
}

更新:感谢 @ 4c74356b41 提供有效答案:

UPDATE: Thanks @ 4c74356b41 for a working answer:

"copy": [
            {
                "name": "resources",
                "count": "[parameters('virtualMachineCount')]",
                "input": {
                    "id": "[resourceId('Microsoft.Compute/virtualMachines', concat(parameters('virtualMachineNamePrefix'), copyIndex('resources')))]"
                }
            }
        ]

推荐答案

可能使用一个变量并输出它?

probably use a variable and output it?

"variables": {
    "copy": [
        {
            "name": "resources",
            "count": "[parameters('virtualMachineCount')]"
            "input": {
                "id": "[resourceId('Microsoft.Network/networkInterfaces', concat(parameters('virtualMachineNamePrefix'), copyIndex('resources')))]"
            }
        }
    ]
}

然后使用那个变量:

"outputs": {
    "resourceID": {
        "type": "array",
        "value": "[variables('resources')]"
    }
}

更新:我刚刚在之前的一个答案中注意到你的评论,所以我不确定这个答案是否是你正在寻找的,如果不是 - 在评论中告诉我你到底想要什么,因为我有点糊涂了.

update: I've just noticed your comment in one of the previous answers, so I'm not sure if this answer is what you are looking for, if not - tell me in the comments what you are after exactly, since I'm a bit confused.

这篇关于使用 copyIndex 时如何获取 ResourceId 作为 VM 的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆