Skip to content

Instantly share code, notes, and snippets.

@donpdonp
Last active August 7, 2018 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donpdonp/388764e85540cb14414e1a0c3352e242 to your computer and use it in GitHub Desktop.
Save donpdonp/388764e85540cb14414e1a0c3352e242 to your computer and use it in GitHub Desktop.
terraform state file to ansible inventory, grouped by first tag value.
JQ='.modules[0].resources
| map(select(.primary.attributes."tags.0")
| { tag: .primary.attributes."tags.0", ip: .primary.attributes.ipv4_address, name: .primary.attributes.name })
| group_by(.tag)
| map (["["+.[0].tag+"]", (map([.name, "ansible_ssh_host=" + .ip] | join(" ")) | join("\n"))]|join("\n") )
| join("\n\n")'
jq -r "$JQ" $1
@donpdonp
Copy link
Author

donpdonp commented Aug 7, 2018

Groups terraform resources by tag value into the ansible inventory format.

$ ./tf2ansible.sh terraform.tfstate
[api]
thing-api-nyc3-bb ansible_ssh_host=159.65.162.3

[db]
thing-sql-nyc3-zz ansible_ssh_host=99.227.102.3
thing-sql-nyc3-bb ansible_ssh_host=165.227.102.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment