Last active
May 29, 2019 05:53
-
-
Save tbjers/11bc02b982f077cf504326eada3b87e9 to your computer and use it in GitHub Desktop.
Create dynamic Ansible inventory from Digital Ocean terraform state
This file contains 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
#!/bin/sh | |
# Assuming your terraform files are in ./ and Ansible in deploy/ | |
# this one-liner extracts ipv4_address and groups them under hostname. | |
# Sample output: | |
# [my_droplet] | |
# 192.168.1.1 | |
terraform state pull | jq -r 'def kv: to_entries[] | "\(.value)"; .resources | .[] | select(.type == "digitalocean_droplet") | { "\(.name)": [.instances[].attributes.ipv4_address] } | [to_entries[] | "[\(.key)]", (.value|kv)] | join("\n")' > deploy/hosts | |
# Want to add these hosts to ~/.ssh/known_hosts? | |
xargs -0 -n 1 ssh-keyscan < <(tail +2 deploy/hosts | tr \\n \\0) > ~/.ssh/known_hosts |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment