Skip to content

Instantly share code, notes, and snippets.

@mattmattox
Last active January 6, 2021 03:53
Show Gist options
  • Select an option

  • Save mattmattox/b1facfd271d21026b3b96c79c337bbe8 to your computer and use it in GitHub Desktop.

Select an option

Save mattmattox/b1facfd271d21026b3b96c79c337bbe8 to your computer and use it in GitHub Desktop.
rancher cluster-to-node
#!/bin/bash
CATTLE_ACCESS_KEY='token-abcde'
CATTLE_SECRET_KEY='1234567890'
CATTLE_URL='rancher.example.com
clusterids=$(
curl -q -s -u "${CATTLE_ACCESS_KEY}:${CATTLE_SECRET_KEY}" \
-X GET \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
"https://"${CATTLE_URL}"/v3/clusters" | jq -r .data | jq -r .[].id
)
for clusterid in $clusterids
do
echo "##############################################################################"
clusterdata=$(
curl -q -s -u "${CATTLE_ACCESS_KEY}:${CATTLE_SECRET_KEY}" \
-X GET \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
"https://"${CATTLE_URL}"/v3/clusters/"${clusterid}
)
clustername=$(echo $clusterdata | jq -r .name)
nodecount=$(echo $clusterdata | jq -r .nodeCount)
echo "Name: $clustername"
echo "ID: $clusterid"
echo "Number of nodes: $nodecount"
nodesdata=$(
curl -q -s -u "${CATTLE_ACCESS_KEY}:${CATTLE_SECRET_KEY}" \
-X GET \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
"https://"${CATTLE_URL}"/v3/clusters/"${clusterid}"/nodes" | jq -r .data
)
echo "Nodes:"
echo "Hostname IPAddress State"
jq -M -r '
.[] | .hostname, .externalIpAddress, .state
' <<< "$nodesdata" | \
while \
read -r hostname; \
read -r externalIpAddress; \
read -r state; \
do
echo "$hostname $externalIpAddress $state"
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment