我正在尝试使用aws API将VolumeId和卷的状态附加到计算机上。
代码
#!/usr/local/bin/ruby
require "aws-sdk"
require "rubygems"
list=Aws::EC2::Client.new(region: "us-east-1")
volume=list.describe_volumes()
volumes=%x( aws ec2 describe-volumes --region='us-east-1' )
puts volumes
下面是命令的示例输出
aws ec2 describe-volumes --region='us-east-1'
。
请帮助从下面获取VolumeID和状态
API(JSON)的示例输出:
{
"Volumes": [
{
"AvailabilityZone": "us-east-1d",
"Attachments": [
{
"AttachTime": "2015-02-02T07:31:36.000Z",
"InstanceId": "i-bca66353",
"VolumeId": "vol-892a2acd",
"State": "attached",
"DeleteOnTermination": true,
"Device": "/dev/sda1"
}
],
"Encrypted": false,
"VolumeType": "gp2",
"VolumeId": "vol-892a2acd",
"State": "in-use",
"Iops": 100,
"SnapshotId": "snap-df910966",
"CreateTime": "2015-02-02T07:31:36.380Z",
"Size": 8
},
]
}
答案 0 :(得分:1)
仅获取volume_ids
->
JSON.parse(volumes)['Volumes'].map{|v|v["VolumeId"]}
仅获取states
->
JSON.parse(volumes)['Volumes'].map{|v|v["state"]}
用于获取以卷ID为键,其状态为值的哈希/映射->
JSON.parse(volumes)['Volumes'].map{|v| [v["VolumeId"],v["state"]] }.to_h