How to get ResourceId as output for VM when using copyIndex

我怕爱的太早我们不能终老 提交于 2020-04-18 12:32:30

问题


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

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.."
    }
  ]
}

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

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)))]"
        }
    }
}

UPDATE: Thanks @ 4c74356b41 for a working answer:

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

回答1:


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')))]"
            }
        }
    ]
}

and just use that var:

"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.



来源:https://stackoverflow.com/questions/60394678/how-to-get-resourceid-as-output-for-vm-when-using-copyindex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!