带有多个命令行参数的az cli运行命令调用失败

时间:2018-12-03 06:52:55

标签: python azure argparse azure-cli

我正在尝试使用命令行参数执行远程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
".

2 个答案:

答案 0 :(得分:1)

@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”关键字运行脚本。您还可以在scripts参数本身内提供脚本的所有参数。

答案 1 :(得分:1)

有了测试和文档,CLI命令仅在Linux VM中使用--parameters运行带有参数的shell脚本。您可以从Run Shell ScriptAvailable Commands获取详细信息。

  

运行命令使用VM代理在Azure中运行Shell脚本   Linux VM。

然后使用--debug和结果进行测试:

Shell脚本 enter image description here enter image description here Python脚本 enter image description here enter image description here

过程相同,但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"