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

跟風遠走 提交于 2020-06-18 15:39:05

问题


I defined a CustomScriptExtension for Azure VM in Terraform:

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

}

However I get (the same error is visible in Azure portal in VM extensions):

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 ''. \"."

The same parameters executed as an Azure deployment works with no problems (relevant excerpts below):

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

Am I missing something, or is it a bug in Terraform?


Some debugging:

  • If I replace the settings with just:

    {
        "commandToExecute": "mkdir C:\\Test"
    }
    

    the directory gets created, so the problem is with fileUris.

  • If I replace fileUris in the settings JSON with fileUri (which should be wrong):

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

    there is no the Encountered 'Text' with name '', namespace ''. \". error, powershell.exe fires and reports missing ConfigureRemotingForAnsible.ps1.


回答1:


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

As I known, the value type of fileUris should be an array, I have tested it with Azure deployment, if I configure fileUris as a string value, then I could get the same error as you provided.

UPDATE

The fileUris should look like as follows:

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



回答2:


Here is an terraform example , just copy-paste it... I commented the RG section along with location ( from case to case, you might not need it ) :

    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
    }


来源:https://stackoverflow.com/questions/42310382/defining-azure-vm-customscriptextension-in-terraform-expecting-state-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!