简体   繁体   English

描述 EC2 实例 使用 AWS cli 过滤实例名称

[英]Describe EC2 instances Filter instance name using AWS cli

I want to use CLI commands to filter those instances which have particular tags and store it in a file on my local everytime I run the Jenkins job.我想使用 CLI 命令来过滤那些具有特定标签的实例,并在每次运行 Jenkins 作业时将其存储在本地文件中。

aws ec2 describe-instances --filters "Name=tag:Hello,Values=exclude"  --output table --query Reservations[*].Instances[*].{Id.InstanceId, State:State.name,Contact:tag:OwnerContact, InstanceProfileName:InstanceProfileName} > Book.txt 

I get the output in text file successfully, however for two of my parameters InstanceProfileName and OwnerContact , I get the output as None.我成功地获得了文本文件中的输出,但是对于我的两个参数 InstanceProfileName 和 OwnerContact ,我得到的输出为 None。

I tried a variety of combinations, still not able to output the instance name.我尝试了多种组合,仍然无法输出实例名称。

Here's an example that can output various parameters, including tag values: 这是一个可以输出各种参数(包括标记值)的示例:

aws ec2 describe-instances --query "Reservations[*].Instances[*].{Id:InstanceId,State:State.Name,Profile:IamInstanceProfile.Arn,Name:Tags[?Key=='Name'].Value}"

To get the syntax right, just try one element at a time. 要正确使用语法,只需一次尝试一个元素。 Start with: 从...开始:

aws ec2 describe-instances --query "Reservations[*].Instances[*]"

then add parameters, eg: 然后添加参数,例如:

aws ec2 describe-instances --query "Reservations[*].Instances[*].Tags"

Then try each value that you want. 然后尝试所需的每个值。 Finally, string it all together in a big command. 最后,用大命令将它们串在一起。

To understand the --query options, take a look at: JMESPath Tutorial 要了解--query选项,请看一下: JMESPath教程

ok I got this working好的,我开始工作了

instances = conn.instances.filter(
    InstanceIds = failed_instances
)


for instance in instances:
    instance_name = get_tag(instance.tags) 
    print (instance_name, instance.id, instance.instance_type)

but I also want to put the instance_name back into an array.但我也想将 instance_name 放回数组中。 any idea how?知道怎么做吗?

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

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