Skip to content

Instantly share code, notes, and snippets.

@lucasassisrosa
Last active July 12, 2024 22:09
Show Gist options
  • Save lucasassisrosa/473ece1f38a7020a2af65ff32f4960fe to your computer and use it in GitHub Desktop.
Save lucasassisrosa/473ece1f38a7020a2af65ff32f4960fe to your computer and use it in GitHub Desktop.
Obtain all hosts in all datacenters for a given service in Consul
# https://developer.hashicorp.com/consul/api-docs/catalog#list-datacenters
# https://developer.hashicorp.com/consul/api-docs/health#list-service-instances-for-service
# requires jq: https://jqlang.github.io/jq/
SERVICE=
CONSUL_HOST=
for DC in `curl -s "https://$CONSUL_HOST/v1/catalog/datacenters" | jq '.[]' | sed -e 's/"//g'`; do curl -s "https://$CONSUL_HOST/v1/health/service/$SERVICE?dc=$DC" | jq '.[] | "\(.Node.Node) \(.Service.ID)"' | sed -e 's/"//g'; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment