Search code examples
azuregroovyazure-cligroovyshell

Groovy: Executing azure CLI Command with JSON - Parsing Issue?


Currently I fail to run an azure CLI command from Groovy because of the JSON Part in the Command.

There is an azure command to run custom scripts on a virtual machine. The CommandToExecute on the Machine is passes as JSON.

WORKING Example:

REQUEST-CALL in Console:az vm extension set -g demo --vm-name demo-cfg01 --name CustomScript --publisher Microsoft.Azure.Extensions --settings '{"commandToExecute":"ls"}'

RESPONSE: {
  "autoUpgradeMinorVersion": true,
  "forceUpdateTag": null,
  "id": "/subscriptions/xxxxxxxxxx-xxxxxx-xxxx-xxxx-xxxxxxxxxxxxx/resourceGroups/demo/providers/Microsoft.Compute/virtualMachines/demo-cfg01/extensions/CustomScript",
  "instanceView": null,
  "location": "germanycentral",
  "name": "CustomScript",
  "protectedSettings": null,
  "provisioningState": "Succeeded",
  "publisher": "Microsoft.Azure.Extensions",
  "resourceGroup": "demo",
  "settings": {
    "commandToExecute": "ls"
  },
  "tags": null,
  "type": "Microsoft.Compute/virtualMachines/extensions",
  "typeHandlerVersion": "2.0",
  "virtualMachineExtensionType": "CustomScript"
}

This script works fine.

"Same" Command executed with Groovy leads to following:

def process
        StopWatch.withTimeRecording("EXECUTING COMMAND '" + cargs + "'",_logger, Level.ALL) {
            process = (cargs).execute(null,null);
            process.waitForProcessOutput(sout, serr)
        }

Please notice the StopWatch which logs the StringArray containing the params:

EXECUTING COMMAND '[az, vm, extension, set, -g, demo, --vm-name, demo-cfg01, --name, CustomScript, --publisher, Microsoft.Azure.Extensions, --settings, '{"commandToExecute":"ls"}']'

The Params looks the same as in the console

The Response from Azure is:

VM has reported a failure when processing extension 'CustomScript'. Error message: "Enable failed: failed to get configuration: error reading extension configuration: error parsing settings file: error parsing json: json: cannot unmarshal string into Go value of type map[string]interface {}

I think groovy somehow escapes the characters before execution, i cannot figure out what went wrong. Any suggestion?


Solution

  • Found a "workaround". The az command also accept an *.json file as settings parameter. Therefor i first create the command in a temporary json file and passes the json file as parameter. Works!