Azure ARM Template - Using virtualMachines/extensions with CopyIndex

后端 未结 2 1685
野的像风
野的像风 2021-01-25 14:31

I managed to deploy this script without any problems, but I am trying to provision the VMs with a bash script using the virtualMachines/extensions. Do you have any suggestions o

2条回答
  •  无人共我
    2021-01-25 14:59

    @Stringfellow, Thank you for your reply earlier. Really helpful. After I posted this I got it partially working by using the below code. The bash script, must provide two distinct parameters (One for each VM), as those are unique token codes that cannot be ran twice (OTAP).The script in fact is running successfully in the first VM, but it rans both parameters in the same VM and not in the second VM. For example: (MP1TokenCode) should run on VM1 and (MP2TokenCode) should run on VM2. Both Tokens are provided as parameters by the user. I also had to add some line breakers because otherwise the template was combining both tokens together and breaking everything. So, I have now the following, but as I mentioned, it is running the script twice in the same VM and not running on the second. In fact, the execution of the script in the second VM fails.

                {
            "type": "Microsoft.Compute/virtualMachines",
            "name": "[concat('metaport-', copyIndex())]",
            "apiVersion": "2020-06-01",
            "location": "[parameters('location')]",
            "copy": {
                "name": "virtualMachineLoop",
                "count": "[parameters('numberOfInstances')]"
            },
            "dependsOn": [
                "nicLoop"
            ],
            "properties": {
                "availabilitySet": {
                    "id": "[resourceId('Microsoft.Compute/availabilitySets', variables('availabilitySetName'))]"
                },
                "hardwareProfile": {
                    "vmSize": "[parameters('vmSize')]"
                },
                "osProfile": {
                    "computerName": "[concat('vm', copyIndex())]",
                    "adminUsername": "[parameters('adminUsername')]",
                    "adminPassword": "[parameters('adminPasswordOrKey')]",
                    "linuxConfiguration": "[if(equals(parameters('authenticationType'), 'password'), json('null'), variables('linuxConfiguration'))]"
                },
                "storageProfile": {
                    "imageReference": "[variables('imageReference')[parameters('OS')]]",
                    "osDisk": {
                        "createOption": "FromImage"
                    }
                },
                "networkProfile": {
                    "networkInterfaces": [
                        {
                            "id": "[resourceId('Microsoft.Network/networkInterfaces',concat('nic', copyindex()))]"
                        }
                    ]
                }
            }
        },
    {
        "type": "Microsoft.Compute/virtualMachines/extensions",
        "name": "[concat('metaport', copyIndex(), '/newuserscript')]",
        "apiVersion": "2020-06-01",
        "location": "[parameters('location')]",
        "copy": {
            "name": "metaport",
            "count": "[parameters('numberOfInstances')]"
            },
        "dependsOn":[
            "virtualMachineLoop"
        ],
        "properties": {
            "publisher": "Microsoft.Azure.Extensions",
            "type": "CustomScript",
            "typeHandlerVersion": "2.0",
            "autoUpgradeMinorVersion": true,
            "settings": {
            "fileUris": ["https://raw.githubusercontent.com/willguibr/azure/main/MetaPort-Dual-AvailabilitySet-v2.0/install_metaport.sh"]
            },
            "protectedSettings": {
            "commandToExecute": "[concat('sh install_metaport.sh ', parameters('MP1TokenCode'), ' ', parameters('MP2TokenCode'), ' ')]"
        }
      }
    

    I also tried to break down the script in two parts and create two virtualmachines/extension sections but no luck. The script fails in both executions. Obviously not the most optimal way, as I want to avoid code repetition. In other words, the only thing I need now, is a way to make sure that each parameter is sent to each VM individually, MP1TokenCode should be sent to VM1 and MP2TokenCode should be sent to VM2.

提交回复
热议问题