Naxi
Naxi

Reputation: 2044

How to print InternalIP of nodes using jq in k8s?

I want to print the internal IP of all nodes in the same line separated by a space using jq in k8s. How can I do this?

Using jsonpath I can filter using .addresses[?(@.type=="InternalIP")]. How to achieve the same with jq?

Upvotes: 1

Views: 2062

Answers (2)

Abu Hanifa
Abu Hanifa

Reputation: 3057

You could use select and pipe to achieve desired output.

below command shows the internal ip separated by new-line

kubectl get nodes -o json | jq '.items[].status.addresses[] | select(.type=="InternalIP") | .address'

for space separated internal-ips:

kubectl get nodes -o json | jq '.items[].status.addresses[] | select(.type=="InternalIP") | .address' | tr -d '\"' | tr '\n' ' '

Upvotes: 5

Shawlz
Shawlz

Reputation: 658

You can achieve this using the following command

 kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="InternalIP")].address}'

Checkout kubectl Cheat sheet for more examples

Upvotes: 6

Related Questions