我正在编写一个脚本来自动创建AWS卷。我正在尝试使用echo和jq过滤掉volumeId的值
[root@ip-172-41-23-8 ~]# aws ec2 describe-volumes --filters Name=status,Values=available Name=availability-zone,Values=us-east-1d Name=tag:Name,Values=*USEPROD*|jq ".VolumeId"
null
[root@ip-172-41-23-8 ~]# echo $value | jq
{
"Volumes": [
{
"AvailabilityZone": "us-east-1d",
"Attachments": [],
"Tags": [
{
"Value": "Dev",
"Key": "Env"
},
{
"Value": "Shell",
"Key": "Deployment"
},
{
"Value": "cnx30au - Kafka-Data-Node - /dev/sdg - /abc/data/sda",
"Key": "Name"
}
],
"Encrypted": true,
"VolumeType": "gp2",
"VolumeId": "vol-095ac513f0f049ba5",
"State": "available",
"KmsKeyId": "arn:aws:kms:us-east-1:709721360603:key/xvfgfr-dsdsd-dsdsds-a00c-dsdsdsd34dsd",
"SnapshotId": "",
"Iops": 100,
"CreateTime": "2019-04-08T21:52:31.473Z",
"Size": 10
}
]
}
json="aws ec2 describe-volumes --filters Name=status,Values=available Name=availability-zone,Values=us-east-1d Name=tag:Name,Values=*USEPROD*"
echo "Executing: ${json}"
value=`${json}`
valueid=`echo ${value}|jq '.VolumeId'`
[root@ip-172-31-87-1 ~]# echo $valueid
null
当我过滤可用卷时,我只希望输出如下
vol-095ac513f0f049ba5
答案 0 :(得分:2)
VolumeId嵌套了两次,您需要通过查询来识别它
valueid=`echo ${value} | jq '.[]|.[]|.VolumeId'`
答案 1 :(得分:1)
或者简单地:
jq '.Volumes[].VolumeId'