AZ CLI(az vm 运行-命令调用)在使用参数执行时丢失变量数据

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

场景:

命令:

for i in `cat azuregateways.txt | awk -F "-00" {'print '}`; 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

丢失的变量是$newtoken。使用 --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']

因此,如果我们查看命令参数部分,我们会注意到 AZ CLI 正在丢失 jq 变量:

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

我试过将变量导出为全局变量,以各种可能的方式引用,编写脚本并 运行 改为这样做,但是当我将令牌作为变量引入时,所有行为都相同。

已联系 Azure 支持,他们建议使用字典传递参数,毕竟 az cli 在后端运行 py:

az vm run-command invoke -g $RGROUP -n $RGROUP-00 --command-id RunShellScript  --scripts 'echo "" ""' --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
    }
  ]
}