简体   繁体   English

将 AWS CLI 命令中的 output 分配给第二个 AWS CLI 命令

[英]Assign output from AWS CLI command to 2nd AWS CLI command

I am writing automation task for creating AWS AMI image, the goal is get output from aws import-image (.ova to ami convert) and add the name in 2nd command:我正在编写用于创建 AWS AMI 映像的自动化任务,目标是从aws import-image (.ova 到 ami 转换)获取 output 并在第二个命令中添加名称:

importaskid=$(aws ec2 import-image --disk-containers  Format=ova,UserBucket="{S3Bucket=acp17,S3Key=XXXXX.ova}" | jq -r '.ImportTaskId')

aws ec2 create-tags --resources echo $importaskid --tags 'Key=Name, Value=acp_ami_test'

I am able to $importaskid and see needed output but when use aws ec2 create-tags the AMI image created without name and the output from 2nd command is empty.我能够$importaskid并看到所需的 output 但是当使用aws ec2 create-tags时,创建的 AMI 映像没有名称,并且来自第二个命令的 output 为空。

Appreciate your assistance.感谢您的帮助。

This should work for you:这应该适合你:

# set bash variable "importaskid":
importaskid=$(aws ec2 import-image --disk-containers Format=ova,UserBucket="{S3Bucket=acp17,S3Key=XXXXX.ova}" | jq -r '.ImportTaskId')

# Verify that importaskid is set correctly
echo $importaskid

# Now use it:
aws ec2 create-tags --resources "$importaskid" --tags 'Key=Name, Value=acp_ami_test'

The "$()" syntax for assigning the output of a command to a variable is discussed here: https://www.cyberciti.biz/faq/unix-linux-bsd-appleosx-bash-assign-variable-command-output/这里讨论了将命令的 output 分配给变量的“$()”语法: https://www.cyberciti.biz/faq/unix-linux-bsd-appleosx-bash-assign-variable-command-output /

The double quotes in "$importaskid" would be necessary if the value of "$importaskid" happens to have spaces in it.如果 "$importaskid" 的值恰好有空格,则"$importaskid"中的双引号是必要的。

'Hope that helps! '希望有帮助!

thanks for reply, so when i run the command without echo $ImportTaskId see below感谢您的回复,所以当我在没有 echo $ImportTaskId 的情况下运行命令时,请参见下文

aws ec2 create-tags --resource import-ami-XXXXXXXXXXXXX --tags Key=Name,Value='name_ami_test'

i got empty response and the name is not assigned in aws console so i will speak to AWS support/check syntax /check after the assign name to AMI ID and not to import-ami-XXXXXXXXXXXXXXXX我得到空响应,并且未在 aws 控制台中分配名称,因此我将在将名称分配给 AMI ID 之后与 AWS 支持/检查语法/检查交谈,而不是 import-ami-XXXXXXXXXXXXXXXX

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM