az cli 运行 - 使用多个命令行参数的命令调用失败

az cli run-command invoke with multiple command line parameters fails

我正在尝试使用命令行参数执行远程 python 脚本。 python 脚本是使用 argparse 开发的。正如我们所知,argparse 将输入 -/-- 作为参数名称。在 az cli 的 --parameters 中传递它会引发奇怪的错误。

问题 如何使用 az run-command invoke

为 python 脚本传递 python argparse 之类的参数

一些样本尝试和输出

尝试 - 1

az vm run-command invoke     --resource-group MDC_VME_DEV_EUS  \
   --name ubuntumemsql002     --command-id RunShellScript  \
   --scripts /mnt/workspace/scripts/test.py   \
  --parameters "-i /mnt/workspace/data/indata" "-o /mnt/workspace/data/outdata" "--input_compressed_data" "--output_compressed_data" "-a 600" "-p 0.1" "-b 600 1200 1800"  "-s 1543275000"

az vm run-command invoke: ' /mnt/workspace/data/outdata' is not a valid value for '--output'. See 'az vm run-command invoke --help'.

尝试 - 2

 bash-4.4# az vm run-command invoke    \
 --resource-group MDC_VME_DEV_EUS \
    --name ubuntumemsql002     \ 
--command-id RunShellScript    \
 --scripts /mnt/workspace/scripts/test.py  \
   --parameters "-i /mnt/workspace/data/indata -o /mnt/workspace/data/outdata --input_compressed_data --output_compressed_data -a 600 -p 0.1 -b 600 1200 1800  -s 1543275000"

Deployment failed. Correlation ID: 36317037-4cfb-429e-b113-f8995d3419fb. VM has reported a failure when processing extension 'RunCommandLinux'. 
Error message: "Enable failed: failed to execute command: command terminated with exit status=2
    [stdout]

    [stderr]
    /var/lib/waagent/run-command/download/7/script.sh: 1: export: /mnt/workspace/data/indata: bad variable name

".

尝试 - 3

bash-4.4# az vm run-command invoke    \
--resource-group MDC_VME_DEV_EUS \
--name ubuntumemsql002  \
--command-id RunShellScript \
--scripts /mnt/workspace/scripts/test.py \
--parameters "-i /mnt/workspace/data/indata -o /mnt/workspace/data/outdata --input_compressed_data --output_compressed_data -a 600 -p 0.1 -b 600 1200 1800  -s 1543275000" --verbose
    Deployment failed. Correlation ID: 9efaf01c-3d7b-473a-90c8-802d63b6ccf0. VM has reported a failure when processing extension 'RunCommandLinux'. 
Error message: "Enable failed: failed to execute command: command terminated with exit status=2
    [stdout]

    [stderr]
    /var/lib/waagent/run-command/download/8/script.sh: 1: export: /mnt/workspace/data/indata: bad variable name
    ".

尝试 - 4

bash-4.4# az vm run-command invoke \
>     --resource-group MDC_VME_DEV_EUS \
>     --name ubuntumemsql002 \
>     --command-id RunShellScript \
>     --scripts /mnt/workspace/scripts/test.py \
>     --parameters -i /mnt/workspace/data/indata -o /mnt/workspace/data/outdata --input_compressed_data --output_compressed_data -a 600 -p 0.1 -b 600 1200 1800  -s 1543275000
az vm run-command invoke: error: argument --parameters: expected at least one argument
usage: az vm run-command invoke [-h] [--verbose] [--debug]
                                [--output {json,jsonc,table,tsv,yaml}]
                                [--query JMESPATH]
                                [--resource-group RESOURCE_GROUP_NAME]
                                [--name NAME] --command-id COMMAND_ID
                                [--scripts SCRIPTS [SCRIPTS ...]]
                                [--parameters PARAMETERS [PARAMETERS ...]]
                                [--subscription _SUBSCRIPTION]
                                [--ids ID [ID ...]]

尝试 - 5

 bash-4.4# az vm run-command invoke \
>     --resource-group MDC_VME_DEV_EUS \
>     --name ubuntumemsql002 \
>     --command-id RunShellScript \
>     --scripts /mnt/workspace/scripts/test.py \
>     --parameters \-i /mnt/workspace/data/indata \-o /mnt/workspace/data/outdata --input_compressed_data --output_compressed_data \-a 600 \-p 0.1 \-b 600 1200 1800  \-s 1543275000
az vm run-command invoke: error: argument --parameters: expected at least one argument
usage: az vm run-command invoke [-h] [--verbose] [--debug]
                                [--output {json,jsonc,table,tsv,yaml}]
                                [--query JMESPATH]
                                [--resource-group RESOURCE_GROUP_NAME]
                                [--name NAME] --command-id COMMAND_ID
                                [--scripts SCRIPTS [SCRIPTS ...]]
                                [--parameters PARAMETERS [PARAMETERS ...]]
                                [--subscription _SUBSCRIPTION]
                                [--ids ID [ID ...]]
bash-4.4# 

直接 VM CLI 尝试

而且,只是为了确认,当我在 CLI 中 运行 它们时,相同的命令完美执行

/mnt/workspace/scripts/test.py -i /mnt/workspace/data/indata -o /mnt/workspace/data/outdata --input_compressed_data --output_compressed_data -a 600 -p 0.1 -b 600 1200 1800  -s 1543275000
ReportInfo( input_file_name='/mnt/workspace/data/indata/s22k-1543290398518-VV5X13.gz'output_file_name='/mnt/workspace/data/outdata/s22k-1543290398518-VV5X13-data-1543275000.gz'result='Success' )
ReportInfo( input_file_name='/mnt/workspace/data/indata/s22k-1543290398518-VV5X11.gz'output_file_name='/mnt/workspace/data/outdata/s22k-1543290398518-VV5X11-data-1543275000.gz'result='Success' )
ReportInfo( input_file_name='/mnt/workspace/data/indata/s22k-1543290398518-VV5X12.gz'output_file_name='/mnt/workspace/data/outdata/s22k-1543290398518-VV5X12-data-1543275000.gz'result='Success' )
ReportInfo( input_file_name='/mnt/workspace/data/indata/s22k-1543290398518-VV5X15.gz'output_file_name='/mnt/workspace/data/outdata/s22k-1543290398518-VV5X15-data-1543275000.gz'result='Success' )
ReportInfo( input_file_name='/mnt/workspace/data/indata/s22k-1543290398518-VV5X14.gz'output_file_name='/mnt/workspace/data/outdata/s22k-1543290398518-VV5X14-data-1543275000.gz'result='Success' )

一些示例(如评论中所建议)

运行 - 1

bash-4.4# az vm run-command invoke \
>     --resource-group MDC_VME_DEV_EUS \
>     --name ubuntumemsql002 \
>     --command-id RunShellScript \
>     --scripts /mnt/workspace/scripts/test.py \
>     --parameters "\-i" "/mnt/workspace/data/indata" "\-o" "/mnt/workspace/data/outdata" "\--input_compressed_data" "\--output_compressed_data" "\-a" "600" "\-p" "0.1" "\-b" "600 1200 1800"  "\-s" "1543275000"
Deployment failed. Correlation ID: 70f7646e-fa94-4104-8067-e42b92470803. VM has reported a failure when processing extension 'RunCommandLinux'. Error message: "Enable failed: failed to execute command: command terminated with exit status=2
[stdout]

[stderr]
/var/lib/waagent/run-command/download/9/script.sh: 1: export: 1200: bad variable name

运行 - 2

bash-4.4# az vm run-command invoke \
>     --resource-group MDC_VME_DEV_EUS \
>     --name ubuntumemsql002 \
>     --command-id RunShellScript \
>     --scripts /mnt/workspace/scripts/prepare_data_for_memsql_ingest.py \
>     --parameters "\-i" "/mnt/workspace/data/indata" "\-o" "/mnt/workspace/data/outdata" "\--input_compressed_data" "\--output_compressed_data" "\-a" "600" "\-p" "0.1" "\-b" "600" "1200" "1800"  "\-s" "1543275000"
Deployment failed. Correlation ID: e460bfa4-286e-43c3-8a60-6f74b78c7e85. VM has reported a failure when processing extension 'RunCommandLinux'. Error message: "Enable failed: failed to execute command: command terminated with exit status=2
[stdout]

[stderr]
usage: PROG [-h] -i INPUT_DIR -o OUT_DIR
            (--input_compressed_data | --input_uncompressed_data)
            (--output_compressed_data | --output_uncompressed_data) -a
            AGGREGATE_INTERVAL -p {<__main__.Range object at 0x7fd86f8c49d0>}
            -b BACKDATE_INTERVAL_LIST [BACKDATE_INTERVAL_LIST ...] -s
            START_EPOCH
PROG: error: argument -i/--input_dir is required
".

@kumar_m_kiran - 请试试这个,如果有效请告诉我:

az vm run-command invoke     --resource-group MDC_VME_DEV_EUS  \
   --name ubuntumemsql002     --command-id RunShellScript  \
   --scripts "python /mnt/workspace/scripts/test.py -i /mnt/workspace/data/indata -o /mnt/workspace/data/outdata --input_compressed_data --output_compressed_data -a 600 -p 0.1 -b 600 1200 1800 -s 1543275000"

可能您需要 运行 脚本开头带有 "python" 关键字。您还可以在脚本参数本身中为脚本提供所有参数。

通过测试和文档,CLI 命令只是 运行s shell 脚本,其参数使用 Linux VM 中的 --parameters。您可以从 Run Shell Script and Available Commands.

获取详细信息

Run Command uses the VM agent to run shell scripts within an Azure Linux VM.

然后用 --debug 测试,结果在这里:

Shell 脚本 Python 脚本

过程相同,但 shell 脚本有效,而 python 脚本无效。所以看起来你可以 运行 python 脚本作为 shell 脚本,参数如下:

az vm run-command invoke     --resource-group groupName  \
   --name vmName             --command-id RunShellScript  \
   --scripts "python /path/test.py arg1 arg2 arg3"