首页 > 解决方案 > Azure ARM 模板 - 将 virtualMachines/extensions 与 CopyIndex 一起使用

问题描述

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

这是我使用的代码,但没有 copyIndex。该脚本需要将参数传递给每个 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'))]"
}
}
}

标签: azure-resource-managerarm-template

解决方案


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

{
  "$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 寻找唯一的令牌。以下是使用对象数组的方法。

{
  "$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 表示形式如下所示。

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

推荐阅读