I am writing some bash scripts to help automate the management of AWS resources. I am using aws-cli
and jq
, and so far things have been great.
I am tagging my resources with custom tags. In certain circumstances I would like to filter a list of resources based on both Key
and Value
of the custom tag. But I am having trouble working out a succinct jq
query to do it.
So, for example, if the (trimmed) JSON output for my ec2 instances is like:
[
{
"PublicIpAddress": "11.22.33.44",
"PrivateIpAddress": "55.66.77.88",
"Tags": [
{
"Value": "live199.blah.com",
"Key": "Name"
},
{
"Value": "live-standalone",
"Key": "hc-class"
}
]
}
]
[
{
"PublicIpAddress": "111.222.333.444",
"PrivateIpAddress": "555.666.777.888",
"Tags": [
{
"Value": "staging99.blah.com",
"Key": "Name"
},
{
"Value": "staging-standalone",
"Key": "hc-class"
}
]
}
]
...and I need find the entry where Tags.Key == "hc-class"
and Tags.Value = "staging-standalone"
, how do I do it in a succinct way with jq
?
Any help greatly appreciated.
With the given input, the following filter produces the output as shown below:
.[] | select(any(.Tags[]; .Key == "hc-class" and .Value == "staging-standalone"))
Output:
{
"PublicIpAddress": "111.222.333.444",
"PrivateIpAddress": "555.666.777.888",
"Tags": [
{
"Value": "staging99.blah.com",
"Key": "Name"
},
{
"Value": "staging-standalone",
"Key": "hc-class"
}
]
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With