Azure ARM模板-将VirtualMachines/扩展名与CopyIndex一起使用 [英] Azure ARM Template - Using virtualMachines/extensions with CopyIndex

查看:206
本文介绍了Azure ARM模板-将VirtualMachines/扩展名与CopyIndex一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法没有任何问题地部署了此脚本,但是我试图使用virtualMachines/extensions为虚拟机提供bash脚本.您在本节中对如何使用copyIndex有任何建议吗?我尝试了几种方法,但是没有运气,脚本由于语法错误而无法部署.这是我要重新利用的脚本:

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 on how to use the copyIndex in this section? I tried in several ways but I had no luck and the script fails to deploy with syntax errors. This is the script I am trying to repurpose: https://github.com/Azure/azure-quickstart-templates/tree/master/201-vm-copy-index-loops. Any assistance is appreciated. Thank you.

这是我使用的代码,但是没有copyIndex.该脚本需要将参数传递给每个VM.

This is the code I was using, but without the copyIndex. The script needs to pass parameters to each VM.

{
"type": "Microsoft.Compute/virtualMachines/extensions",
"name": "[concat(parameters('MetaPortName'),'/newuserscript')]",
"apiVersion": "2020-06-01",
"location": "[parameters('location')]",
"dependsOn": [
"[resourceId('Microsoft.Compute/virtualMachines/', parameters('MetaPortName'))]"
],
"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'))]"
}
}
}

推荐答案

类似下面的方法将是我的方法.传入 MetaPortNameArray 参数作为VM名称数组.我对令牌代码参数进行了假设,例如每个VM的令牌代码都相同.如果它们对于每个VM来说都是唯一的,那么它们将成为数组的一部分,例如对象数组,而不是代表VM名称的字符串.

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": {}
}

更新1

基于所问的几乎重复的问题,我可以看到您正在为每个VM寻找唯一的令牌.下面是使用对象数组的方法.

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": {}
}

传入的对象数组的PowerShell表示形式如下.

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

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

这篇关于Azure ARM模板-将VirtualMachines/扩展名与CopyIndex一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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