Skip to content

Instantly share code, notes, and snippets.

@geowa4
Last active March 12, 2019 09:30
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geowa4/e915e421878356784fd215754eb253dc to your computer and use it in GitHub Desktop.
Save geowa4/e915e421878356784fd215754eb253dc to your computer and use it in GitHub Desktop.
Dynamic inventory for DigitalOcean grouping by tag.
#!/usr/bin/env bash
set \
-o nounset \
-o pipefail \
-o errexit
tags=$(doctl compute tag list --output json | jq -r '.[].name')
aggregate=$(jq -n '{ "_meta": { "hostvars": { } } }')
for tag in $tags
do
hosts=$(doctl compute droplet list --tag-name "$tag" --output json | \
jq '[.[].networks.v4[] | select(.type == "public") | .ip_address]'
)
aggregate=$(jq \
-n \
--argjson aggregate "$aggregate" \
--arg tag "$tag" \
--argjson hosts "$hosts" \
'$aggregate + { ($tag): $hosts }' \
)
done
echo "$aggregate"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment