Azure ARM Template - Using virtualMachines/extensions with CopyIndex

后端 未结 2 1680
野的像风
野的像风 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 15:15

    Something like the following would be my approach. Pass in the MetaPortNameArray parameter as an array of VM names. I made assumptions about the token code parameters, such as the token code being the same for each VM. If they need to be unique to each VM, then they would be part of the array, such as an array of objects instead of strings representing VM names.

    {
      "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
      "contentVersion": "1.0.0.0",
      "parameters": {
        "MetaPortNameArray": {
          "type": "array"
        },
        "location": {
          "type": "string"
        },
        "MP1TokenCode": {
          "type": "securestring"
        },
        "MP2TokenCode": {
          "type": "securestring"
        }
      },
      "variables": {
      },
      "resources": [
        {
          "type": "Microsoft.Compute/virtualMachines/extensions",
          "name": "[concat(parameters('MetaPortNameArray')[copyIndex()],'/newuserscript')]",
          "apiVersion": "2020-06-01",
          "location": "[parameters('location')]",
          "dependsOn": [
            "[resourceId('Microsoft.Compute/virtualMachines/', parameters('MetaPortNameArray')[copyIndex()])]"
          ],
          "copy": {
            "name": "vmExtCopy",
            "count": "[length(parameters('MetaPortNameArray'))]"
          },
          "properties": {
            "publisher": "Microsoft.Azure.Extensions",
            "type": "CustomScript",
            "typeHandlerVersion": "2.0",
            "autoUpgradeMinorVersion": true,
            "settings": {
              "fileUris": [ "https://raw.githubusercontent.com/willguibr/azure/main/MetaPort-Standalone-NATGW-v1.0/install_metaport.sh" ]
            },
            "protectedSettings": {
              "commandToExecute": "[concat('sh install_metaport.sh ', parameters('MP1TokenCode'), parameters('MP2TokenCode'))]"
            }
          }
        }
      ],
      "outputs": {}
    }
    

    Update 1

    Based on the nearly duplicate question asked, I can see you are looking for unique tokens for each VM. The following is the approach to take, using an array of object.

    {
      "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
      "contentVersion": "1.0.0.0",
      "parameters": {
        "MetaPortNameArray": {
          "type": "array"
        },
        "location": {
          "type": "string"
        }
      },
      "variables": {
      },
      "resources": [
        {
          "type": "Microsoft.Compute/virtualMachines/extensions",
          "name": "[concat(parameters('MetaPortNameArray')[copyIndex()].VmName,'/newuserscript')]",
          "apiVersion": "2020-06-01",
          "location": "[parameters('location')]",
          "dependsOn": [
            "[resourceId('Microsoft.Compute/virtualMachines/', parameters('MetaPortNameArray')[copyIndex()].VmName)]"
          ],
          "copy": {
            "name": "vmExtCopy",
            "count": "[length(parameters('MetaPortNameArray'))]"
          },
          "properties": {
            "publisher": "Microsoft.Azure.Extensions",
            "type": "CustomScript",
            "typeHandlerVersion": "2.0",
            "autoUpgradeMinorVersion": true,
            "settings": {
              "fileUris": [ "https://raw.githubusercontent.com/willguibr/azure/main/MetaPort-Standalone-NATGW-v1.0/install_metaport.sh" ]
            },
            "protectedSettings": {
              "commandToExecute": "[concat('sh install_metaport.sh ', parameters('MetaPortNameArray')[copyIndex()].MPTokenCode)]"
            }
          }
        }
      ],
      "outputs": {}
    }
    

    The PowerShell representation of the object array passed in would look like.

    $MetaPortNameArray = @(
        @{
            'VmName'      = 'OneMachine'
            'MPTokenCode' = 'SomeTokenCodeOne'
        },
        @{
            'VmName'      = 'TwoMachine'
            'MPTokenCode' = 'AnotherTokenCodeTwo'
        }
    )
    

提交回复
热议问题