Azure ARM模板-使用带有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'))]"
}
}
}

@Stringfellow,感谢您早些时候的回复。真的很有帮助。在我发布这篇文章后,我使用以下代码使其部分工作。bash脚本必须提供两个不同的参数(每个VM一个(,因为这些是唯一的令牌代码,不能运行两次(OTAP(。事实上,该脚本在第一个VM中成功运行,但它在同一个VM中传输两个参数,而不是在第二个VM中。例如:(MP1TokenCode(应该在VM1上运行,(MP2TokenCode。两个令牌都由用户作为参数提供。我还必须添加一些换行符,因为否则模板会将两个令牌组合在一起并破坏所有内容。所以,我现在有了以下内容,但正如我所提到的,它在同一个VM中运行两次脚本,而不是在第二个VM上运行。事实上,脚本在第二个虚拟机中的执行失败了。

{
"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'), ' ')]"
}
}

我还试图将脚本分解为两部分,并创建两个虚拟/扩展部分,但没有成功。脚本在两次执行中都失败。显然不是最理想的方式,因为我想避免代码重复。换句话说,我现在唯一需要的是确保每个参数都单独发送到每个VM,MP1TokenCode应该发送到VM1,MP2TokenCode可以发送到VM2。

我的方法如下。将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'
}
)

最新更新