自定义脚本失败的 Azure ARM

Azure ARM with custom script failing

我的 arm 模板中有以下代码片段 运行 虚拟机上的自定义脚本

"variables": {
    "installES": "https://sentiencescripts.blob.core.windows.net/script/elasticsearch-centos-install.sh"
},


"resources": [
    {
      "type": "extensions",
      "name": "installelasticsearch",
      "apiVersion": "2015-06-15",
      "location": "[resourceGroup().location]",
      "dependsOn": [
        "[resourceId('Microsoft.Compute/virtualMachines', concat(variables('vmName'), copyindex(1)))]"
      ],
      "properties": {
        "publisher": "Microsoft.Azure.Extensions",
        "type": "CustomScript",
        "typeHandlerVersion": "2.0",
        "settings": {
          "fileUris": "[variables('installES')]",
          "protectedSettings": {
          "commandToExecute": "sh elasticsearch-centos-install.sh",
          "storageAccountName": "myaccount",
          "storageAccountKey": "my-key"
          }

        }
      }
    }
   ]

失败并出现以下错误

New-AzureRmResourceGroupDeployment : 6:23:31 PM - Resource Microsoft.Compute/virtualMachines 'es-master-node1' failed with message '{
"status": "Failed",
"error": {
"code": "ResourceDeploymentFailure",
"message": "The resource operation completed with terminal provisioning state 'Failed'.",
"details": [
  {
    "code": "VMExtensionProvisioningError",
    "message": "VM has reported a failure when processing extension 'installelasticsearch'. Error message: \"Enable failed: failed to get configuration: json validation error: 
invalid public settings JSON: fileUris: Invalid type. Expected: array, given: string\"."
    }
   ]
  }
 }'
At line:1 char:1

更新 1: 我根据@Francois

更新了"fileUris": ["[variables('installES')]"],

但还是报错

New-AzureRmResourceGroupDeployment : 8:13:37 AM - Resource Microsoft.Compute/virtualMachines 'es-master-node2' failed with message '{
"status": "Failed",
"error": {
"code": "ResourceDeploymentFailure",
"message": "The resource operation completed with terminal provisioning state 'Failed'.",
"details": [
  {
    "code": "VMExtensionProvisioningError",
    "message": "VM has reported a failure when processing extension 'installelasticsearch'. Error message: \"Enable failed: failed to get configuration: json validation error: 
invalid public settings JSON: protectedSettings: Additional property protectedSettings is not allowed\"."
      }
    ]
  }
}'

然后我替换了

"protectedSettings": {
      "commandToExecute": "sh elasticsearch-centos-install.sh",
      "storageAccountName": "myaccount",
      "storageAccountKey": "my-key"
      }

"commandToExecute": "sh elasticsearch-centos-install.sh"

但还是一样的错误。

更新 2:

将我的模板修改为以下

{
  "type": "Microsoft.Compute/virtualMachines/extensions",
  "name": "[concat(variables('vmName'), copyindex(1),'/', variables('extensionName'))]",
  "apiVersion": "[variables('apiVersion')]",
  "location": "[resourceGroup().location]",
  "dependsOn": [
    "[concat('Microsoft.Compute/virtualMachines/', variables('vmName'), copyindex(1))]"
  ],
  "properties": {
    "publisher": "Microsoft.Azure.Extensions",
    "type": "CustomScript",
    "typeHandlerVersion": "2.0",
    "autoUpgradeMinorVersion": true,
    "settings": {
      "fileUris": "[split(parameters('fileUris'), ' ')]",
      "commandToExecute": "[parameters('commandToExecute')]"
    },
    "protectedSettings": {
      "storageAccountName": "[parameters('customScriptStorageAccountName')]",
      "storageAccountKey": "[parameters('customScriptStorageAccountKey')]"
    }
  }
}

它适用于单个 VM,但不适用于多个 VM。

New-AzureRmResourceGroupDeployment : 3:35:59 PM - Error: Code=InvalidTemplate; Message=Deployment template validation failed: 
'The template resource '[concat(variables('vmName'), copyindex(1),'/', variables('extensionName'))]' at line '158' and column '10' is not valid:
The template function 'copyIndex' is not expected at this location. The function can only be used in a resource with copy specified. Please see https://aka.ms/arm-copy for usage details..'.
At line:1 char:1
+ New-AzureRmResourceGroupDeployment -Name ElasticSearch -ResourceGroup ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [New-AzureRmResourceGroupDeployment], Exception
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.NewAzureResourceGroupDeploymentCmdlet

New-AzureRmResourceGroupDeployment : The deployment validation failed
At line:1 char:1
+ New-AzureRmResourceGroupDeployment -Name ElasticSearch -ResourceGroup ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : CloseError: (:) [New-AzureRmResourceGroupDeployment], InvalidOperationException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.NewAzureResourceGroupDeploymentCmdlet

任何人都知道 copyindex() 是否在 Microsoft.Compute/virtualMachines/extensions

中受支持

添加方括号使 JSON 键入数组而不是字符串

 "fileUris": ["[variables('installES')]"]

设法使以下块工作

{
  "type": "Microsoft.Compute/virtualMachines/extensions",
  "name": "[concat(variables('vmName'),copyindex(1),'/', variables('extensionName'))]",
  "apiVersion": "[variables('apiVersion')]",
  "location": "[resourceGroup().location]",
  "copy": {
        "name": "virtualMachineInstallationLoop",
        "count": "[variables('vmInstances')]"
    },
  "dependsOn": [
    "[concat('Microsoft.Compute/virtualMachines/', variables('vmName'),copyindex(1))]"
  ],
  "properties": {
    "publisher": "Microsoft.Azure.Extensions",
    "type": "CustomScript",
    "typeHandlerVersion": "2.0",
    "autoUpgradeMinorVersion": true,
    "settings": {
      "fileUris": "[split(parameters('fileUris'), ' ')]",
      "commandToExecute": "[parameters('commandToExecute')]"
    },
    "protectedSettings": {
      "storageAccountName": "[parameters('customScriptStorageAccountName')]",
      "storageAccountKey": "[parameters('customScriptStorageAccountKey')]"
    }
  }
}