我正在嘗試使用帶有標簽名稱的 awscli 從 AWS 獲取所有 ec2 實體。
我使用的命令是
aws ec2 describe-instances | \
jq -r '.Reservations[].Instances[] | (.Tags[] | select(.Key == "Name") | .Value) " " \
.InstanceId " " .InstanceType " " .KeyName " " .PrivateIpAddress " " .PublicIpAddress'
在這里,jq 只列印帶有標簽名稱的實體,其他沒有標簽名稱的實體不會列印。
我是否以錯誤的方式使用了 select()?
實際輸出:
master-bastion i-026b52da57ae3a85 t2.micro aus 10.90.0.68 52.62.76.17
master-mongodb_1 i-083bceea3aea1832 t3.medium aus 10.90.100.25
master-mongo i-06d669ba5cda0c74 t3.medium aus 10.90.100.12
master-solr1 i-09752d54fe143fec t2.medium aus 10.90.100.12
master-solr2 i-039bf2028ec15d97 t2.medium aus 10.90.101.22
master-solr3 i-09fc04ceeeb1efae t2.medium aus 10.90.100.6
rabbitmq-1 i-0125de65ba60627a t2.small aus 10.90.100.10
rabbitmq-2 i-069d546deb4a1c23 t2.small aus 10.90.101.11
預期輸出:
master-bastion i-026b52da57ae3a85 t2.micro aus 10.90.0.68 52.62.76.17
i-06d669ba5cda0c4d t3.medium aus 10.90.100.142
i-062669ba5cda0sfs t3.medium aus 10.90.100.147
master-mongodb_1 i-083bceea3aea1832 t3.medium aus 10.90.100.25
master-mongo i-06d669ba5cda0c74 t3.medium aus 10.90.100.12
master-solr1 i-09752d54fe143fec t2.medium aus 10.90.100.12
master-solr2 i-039bf2028ec15d97 t2.medium aus 10.90.101.22
master-solr3 i-09fc04ceeeb1efae t2.medium aus 10.90.100.6
rabbitmq-1 i-0125de65ba60627a t2.small aus 10.90.100.10
rabbitmq-2 i-069d546deb4a1c23 t2.small aus 10.90.101.11
uj5u.com熱心網友回復:
如果我們只關注標簽,我會看到兩種解決方案:
(.Tags[] | select(.Key == "Name")).value // ""
如果 .value 不存在,這將使用//運算子回傳""。
或者,您可以先將標簽硬塞到一個物件中:
.Tags | map({key: .Key, value: .Value}) | from_entries | .Name // ""
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/390505.html
上一篇:基于列串列對資料進行分組和聚合
