Search code examples
shellcommand-line-interfacejqaz

AZ CLI (az vm run-command invoke) losing variable data when executed with parameters


Scenario:

  • multiple servers with 1 json file that must have a token replaced
  • using jq for json replace
  • az vm run-command for running it for all machines
  • azuregateways.txt is the list of Azure VMs
  • newtokens.csv is the list of new tokens for each gw

The command:

for i in `cat azuregateways.txt | awk -F "-00" {'print $1'}`; do newtoken=$(grep $i newtokens.csv | cut -d : -f2); az vm run-command invoke -g $i -n $i-00 --command-id RunShellScript --scripts """cp /home/ops/data/registrationticket.json /home/ops/data/registrationticket.json_old; sudo apt install -y jq; q --arg httpsas "$newtoken" '.OutboxAccessTicket.HttpSas=$httpsas' registrationticket.json_old > registrationticket.json; docker restart mygateway""";done

The variable which is being lost is $newtoken. The command when executed with --debug:

Command arguments: ['vm', 'run-command', 'invoke', '-g', 'ANDROIDGW', '-n', 'ANDROIDGW-00', '--command-id', 'RunShellScript', '--scripts', "cp /home/ops/data/registrationticket.json /home/ops/data/registrationticket.json_old; jq --arg httpsas 'ASDASD ASD ASD ASD ASD' '.OutboxAccessTicket.HttpSas=' /home/ops/data/registrationticket.json_old > /home/ops/data/registrationticket.json", '--debug']

So if we can look over the Comnmand arguments section, we notice that AZ CLI is losing the jq variable:

jq --arg httpsas 'ASDASD ASD ASD ASD ASD' '.OutboxAccessTicket.HttpSas='

I have tried exporting the variables as global variables, quoting in every possible way, doing a script and running that instead but all behaves the same when I introduce the token as being a variable.


Solution

  • Azure Support has been contacted, they recommended using dictionaries to pass arguments, since after all, az cli runs py in backend:

    az vm run-command invoke -g $RGROUP -n $RGROUP-00 --command-id RunShellScript  --scripts 'echo "$1" "$2"' --parameters "hello" "'this is just a test'"
    {
      "value": [
        {
          "code": "ProvisioningState/succeeded",
          "displayStatus": "Provisioning succeeded",
          "level": "Info",
          "message": "Enable succeeded: \n[stdout]\nhello this is just a test\n\n[stderr]\n",
          "time": null
        }
      ]
    }