Azure ARM 模板 - 使用带有 CopyIndex 的虚拟机/扩展 [英] Azure ARM Template - Using virtualMachines/extensions with CopyIndex

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

问题描述

我设法毫无问题地部署了此脚本,但我正在尝试使用 virtualMachines/extensions 为 VM 提供 bash 脚本.您对如何使用本节中的 copyIndex 有什么建议吗?我尝试了多种方法,但运气不佳,脚本无法部署并出现语法错误.这是我试图重新调整用途的脚本:https://github.com/Azure/azure-quickstart-templates/tree/master/201-vm-copy-index-loops.任何帮助表示赞赏.谢谢.

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.脚本需要向每个虚拟机传递参数.

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 模板 - 使用带有 CopyIndex 的虚拟机/扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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