Last active
March 12, 2019 09:30
-
-
Save geowa4/e915e421878356784fd215754eb253dc to your computer and use it in GitHub Desktop.
Dynamic inventory for DigitalOcean grouping by tag.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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