简体   繁体   English

aws cli ec2 describe-instances表输出

[英]aws cli ec2 describe-instances table output

I want to run an ec2 describe-instances command and get output in a table format as follows (where name is the Value of the Tag with Key 'Name'): 我想运行ec2 describe-instances命令并以表格格式获取输出,如下所示(其中name是带有Key'Name'的Tag的值):

----------------------------------------------------------
|                    DescribeInstances                   |
+-------------+----------------+--------------+----------+
| instance_id |  ip_address    |    name      |  state   |
+-------------+----------------+--------------+----------+
|  i-g93g494d |  99.99.99.01   |  name1       |  running |
|  i-a93f754c |  99.99.99.02   |  name2       |  running |
+-------------+----------------+--------------+----------+

I can run the following command: 我可以运行以下命令:

aws ec2 describe-instances --instance-ids i-g93g494d i-a93f754c --query "Reservations[*].Instances[*].{name: Tags[?Key=='Name'].Value, instance_id: InstanceId, ip_address: PrivateIpAddress, state: State.Name}" --output json

and obtain output: 并获得输出:

[
    [
        {
            "instance_id": "i-g93g494d",
            "state": "running",
            "ip_address": "99.99.99.01",
            "name": [
                "name1"
            ]
        }
    ],
    [
        {
            "instance_id": "i-a93f754c",
            "state": "running",
            "ip_address": "99.99.99.02",
            "name": [
                "name2"
            ]
        }
    ]
]

However, when I run the same command with --output table rather than --output json I get an error. 但是,当我使用--output表而不是--output json运行相同的命令时,我得到一个错误。

command: 命令:

aws ec2 describe-instances --instance-ids i-g93g494d i-a93f754c --query "Reservations[*].Instances[*].{name: Tags[?Key=='Name'].Value, instance_id: InstanceId, ip_address: PrivateIpAddress, state: State.Name}" --output json

output: 输出:

list index out of range

I would like the table output to look like the above example but I'm having difficulty solving this. 我希望表输出看起来像上面的例子,但我很难解决这个问题。 I'd very much appreciate any help anyone can offer on this. 我非常感谢任何人都能提供的任何帮助。

You need to use pipe expression to filter the Tag results and get the first value such as: 您需要使用管道表达式来过滤标记结果并获取第一个值,例如:

aws ec2 describe-instances --instance-ids i-g93g494d i-a93f754c --query "Reservations[*].Instances[*].{name: Tags[?Key=='Name'] | [0].Value, instance_id: InstanceId, ip_address: PrivateIpAddress, state: State.Name}" --output table

There is a nice related blog post here: Get a list of instance with id, name and type 这里有一篇很好的相关博客文章: 获取一个包含id,name和type的实例列表

Here's a python program that can be used to generate listings from the descibe-instances command: 这是一个python程序,可用于从descibe-instances命令生成列表:

import json
import sys

with open( sys.argv[1] ) as f:
  aws = json.load( f ) 
  Reservations = aws['Reservations']
  for Reservation in Reservations:
    Instances = Reservation['Instances']
    for Instance in Instances:
      Tags = Instance['Tags']
      for Tag in Tags:
        if Tag['Key'] == "Name":
          print Tag['Value'],Instance['PrivateIpAddress'],Instance['InstanceId'],Instance['State']['Name']

BTW, I absolutely LOVE Volkan Paksoy's answer. 顺便说一下,我绝对喜欢Volkan Paksoy的回答。 That's going into my bag o' tricks. 这是我的包包的技巧。 The above python may have the advantage of allowing you to be more expressive with your search criteria or somehow combining results. 上面的python可能具有以下优势:允许您使用搜索条件更具表现力或以某种方式组合结果。 In short, it's python, and you don't have to figure out how to use the aws syntax. 简而言之,它是python,你不必弄清楚如何使用aws语法。

Here's how to invoke the python script above: 以下是调用上面的python脚本的方法:

python parse.py  <( aws ec2 describe-instances  )

Of course, your mileage may vary. 当然,您的里程可能会有所不同。 For instance, you may not have your region defaulted so, you may need to add an extra parameter as follows: 例如,您可能没有默认区域,您可能需要添加一个额外的参数,如下所示:

python parse.py  <( aws ec2 --region us-east-2 describe-instances  )

And you can than manipulate the output a bit to select only running instances and put in a well formatted column list: 您可以稍微操作输出以仅选择正在运行的实例并放入格式良好的列列表:

python parse.py  <( aws ec2 --region us-east-2 describe-instances  ) | column -t | sort -k1,1 | cat -n  | grep running  

One last note, as I stated, I am a fan of the one liner provided by Volkan. 最后一点,正如我所说,我是Volkan提供的一个班轮的粉丝。 I was just wondering how if the column ordering can be manipulated. 我只是想知道如何操纵列排序。 I found that aws is sorting the columns alphabetically. 我发现aws按字母顺序对列进行排序。 To make the ordering obvious, I put numbers in front of the attribute names. 为了使排序明显,我将数字放在属性名称前面。 However, these attribute names weren't interpreted properly as identifiers, so I had to quote them with double quotes. 但是,这些属性名称未被正确解释为标识符,因此我不得不用双引号引用它们。 Here's what I wound up with, please note the subtle quoting changes ( and not so subtle quoting around 'Name' ): 这就是我最后的内容,请注意细微的引用变化(而不是在“名称”周围引用如此微妙的引用):

aws ec2 describe-instances  --query 'Reservations[*].Instances[*].{"01-name": Tags[?Key=='"'Name'"'] | [0].Value, "03-instance_id": InstanceId, "02-ip_address": PrivateIpAddress, "04-state" : State.Name}' --output table 

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

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