[使用copyIndex时如何获取ResourceId作为VM的输出

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

我使用copyIndex(0)创建几个virtualMachine资源(以及publicIP地址,nic ...)

我需要resourceID作为部署的输出以进行进一步处理。通常,我使用resourceId()函数执行此操作,但是由于名称是动态的,而outputIndex中的copyIndex无效,因此我无法为此找到正确的语法:

{
  "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的正确语法是什么?

下面的完整ARM模板:

{
    "$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一个有效的答案:

"copy": [
            {
                "name": "resources",
                "count": "[parameters('virtualMachineCount')]",
                "input": {
                    "id": "[resourceId('Microsoft.Compute/virtualMachines', concat(parameters('virtualMachineNamePrefix'), copyIndex('resources')))]"
                }
            }
        ]
azure arm-template azure-template
1个回答
1
投票

可能使用变量并将其输出?

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

并且只需使用该var:

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

更新:我刚刚在先前的答案之一中注意到您的评论,所以我不确定该答案是否是您想要的,如果不是,请在评论中告诉我您的确切想法,因为我有点困惑。

© www.soinside.com 2019 - 2024. All rights reserved.