我正在使用AWS CLI获取此类特定实例的BlockDeviceMappings
数组,
aws ec2 describe-instances --filters "Name=tag:Name,Values=mongodb-*" "Name=private-ip-address,Values=$MEMBER_IP" \
--output json --query "Reservations[*].Instances[*].BlockDeviceMappings[*]
输出
[
[
[
{
"DeviceName": "/dev/xvda",
"Ebs": {
"Status": "attached",
"DeleteOnTermination": true,
"VolumeId": "vol-xvda-xxx",
"AttachTime": "2014-10-13T14:40:13.000Z"
}
},
{
"DeviceName": "/dev/sdb",
"Ebs": {
"Status": "attached",
"DeleteOnTermination": false,
"VolumeId": "vol-sdb-xxxx",
"AttachTime": "2014-10-13T14:40:13.000Z"
}
},
{
"DeviceName": "/dev/sdc",
"Ebs": {
"Status": "attached",
"DeleteOnTermination": false,
"VolumeId": "vol-sdc-xxx",
"AttachTime": "2014-10-13T14:40:13.000Z"
}
},
{
"DeviceName": "/dev/sdd",
"Ebs": {
"Status": "attached",
"DeleteOnTermination": false,
"VolumeId": "vol-sdd-xxx",
"AttachTime": "2014-10-13T14:40:13.000Z"
}
}
]
]
]
所需的输出
我想分别列出/dev/sdb
,/dev/sdc
和/dev/sdd
的音量ID。
vol-sdb-xxxxx, vol-sdc-xxxx, vol-sdd-xxxxx
我尝试使用jq
通过将aws-cli输出传递给jq
来进行解析,但我一直得到“jq:error:无法使用字符串索引数组”。
此外,DeviceName
可能并不总是按照我可以假设数组中的第2,第3和第4个元素的顺序正确的顺序排列,所以我想确定所需的volume-id输出始终采用以下形式:sdb,sdc,sdd(如果可能)。
在尝试了Jeff的两个建议之后:
建议1
aws ec2 describe-instances --filters "Name=tag:Name,Values=mongodb-*" "Name=private-ip-address,Values=$MEMBER_IP" --output json --query "Reservations[*].Instances[*].BlockDeviceMappings[*]" | jq 'select(.DeviceName? | test("/dev/sd[bcd]")) | .Ebs.VolumeId'
error: test is not defined
select(.DeviceName? | test("/dev/sd[bcd]")) | .Ebs.VolumeId 1 compile error
[Errno 32] Broken pipe
建议2
aws ec2 describe-instances --filters "Name=tag:Name,Values=mongodb-*" "Name=private-ip-address,Values=$MEMBER_IP" --output json --query "Reservations[*].Instances[*].BlockDeviceMappings[*]" | jq 'select(
.DeviceName? as $dn
| ["b","c","d"]
| map($dn == "/dev/sd\(.)")
| any
)
| .Ebs.VolumeId'
无输出
我正在使用jq-1.4,我相信这是最新的。
以下适用于jq-1.4,
aws ec2 describe-instances --filters "Name=tag:Name,Values=mongodb-*" "Name=private-ip-address,Values=$MEMBER_IP" --output json --query "Reservations[*].Instances[*].BlockDeviceMappings[*]" | jq '.. | select(.DeviceName? as $dn | ["b","c","d"] | map($dn == "/dev/sd\(.)") | any) | .Ebs.VolumeId'
答案 0 :(得分:0)
如果您使用的是实施test
过滤器的最新版本,则可以执行以下操作:
.. | select(.DeviceName? | test("/dev/sd[bcd]")) | .Ebs.VolumeId
否则,您将不得不做出一些调整:
.. | select(.DeviceName? == ("/dev/sdb","/dev/sdc","/dev/sdd")) | .Ebs.VolumeId