在 Terraform 中定义 Azure VM CustomScriptExtension(预期状态 'Element'.. 遇到 'Text' 名称为“”,命名空间为“。\”。”)

Defining Azure VM CustomScriptExtension in Terraform (Expecting state 'Element'.. Encountered 'Text' with name '', namespace ''. \".")

我在 Terraform 中为 Azure VM 定义了 CustomScriptExtension:

resource "azurerm_virtual_machine_extension" "test" {
    name = "WinRM"
    location = "South Central US"
    resource_group_name = "${azurerm_resource_group.test.name}"
    virtual_machine_name = "${azurerm_virtual_machine.test.name}"
    publisher = "Microsoft.Compute"
    type = "CustomScriptExtension"
    type_handler_version = "1.8"

    settings = <<SETTINGS
    {
        "fileUris": "https://raw.githubusercontent.com/ansible/ansible/devel/examples/scripts/ConfigureRemotingForAnsible.ps1",
        "commandToExecute": "powershell.exe -ExecutionPolicy Unrestricted -File ConfigureRemotingForAnsible.ps1"
    }
SETTINGS

}

但是我得到(在 VM 扩展的 Azure 门户中可以看到相同的错误):

azurerm_virtual_machine_extension.test: compute.VirtualMachineExtensionsClient#CreateOrUpdate: Failure sending request: StatusCode=200 -- Original Error: Long running operation terminated with status 'Failed': Code="VMExtensionProvisioningError" Message="VM has reported a failure when processing extension 'WinRM'. Error message: \"Invalid handler configuration. Exiting. Error Message: Expecting state 'Element'.. Encountered 'Text' with name '', namespace ''. \"."

作为 Azure deployment 执行的相同参数没有问题(相关摘录如下):

"fileUris": {
  "type": "string",
  "defaultValue": "https://raw.githubusercontent.com/ansible/ansible/devel/examples/scripts/ConfigureRemotingForAnsible.ps1",
  "metadata": {
    "description": "The uri list of files. Split by a space."
  }
},
"settings": {
  "fileUris": "[split(parameters('fileUris'), ' ')]",
  "commandToExecute": "[parameters('commandToExecute')]"
}

我是不是遗漏了什么,或者是 Terraform 中的错误?


一些调试:

Error message: \"Invalid handler configuration. Exiting. Error Message: Expecting state 'Element'.. Encountered 'Text' with name '', namespace ''. \"."

据我所知,fileUris 的值类型应该是 array,我已经使用 Azure 部署对其进行了测试,如果我将 fileUris 配置为一个字符串值,那么我可能会得到与你提供的相同的错误。

更新

fileUris 应该如下所示:

"settings": {
  "fileUris": [
    "https://raw.githubusercontent.com/ansible/ansible/devel/examples/scripts/ConfigureRemotingForAnsible.ps1"
  ],
  "commandToExecute": "powershell.exe -ExecutionPolicy Unrestricted -File ConfigureRemotingForAnsible.ps1"
}

这是一个 terraform 示例,只需复制粘贴它...我评论了 RG 部分和位置(根据具体情况,您可能不需要它):

    resource "azurerm_virtual_machine_extension" "win-installansibleclient" {
        name                  = "${var.current-name-convention-core-module}-mtwin-installansibleclient"
        #location              = "${var.preferred-location-module}" 
        #resource_group_name   = "${var.current-name-convention-core-module}-rg"
        virtual_machine_id = "${azurerm_virtual_machine.dcaddns-w2k16.id}"
        publisher = "Microsoft.Compute"
        type = "CustomScriptExtension"
        type_handler_version = "1.8"
        settings = <<SETTINGS
        {
            "fileUris": [
                "https://raw.githubusercontent.com/ansible/ansible/devel/examples/scripts/ConfigureRemotingForAnsible.ps1"
                ],
            "commandToExecute": "powershell.exe -ExecutionPolicy Unrestricted -File ConfigureRemotingForAnsible.ps1"
        }
    SETTINGS
    }