A dynamic inventory script for Ansible and Terraform.
To use this inventory script, you must first create Terraform resources using the terraform-provider-ansible plugin:
resource "ansible_host" "example" {
inventory_hostname = "example.com"
groups = ["web"]
vars {
ansible_user = "admin"
}
}
resource "ansible_group" "web" {
inventory_group_name = "web"
children = ["foo", "bar", "baz"]
vars {
foo = "bar"
bar = 2
}
}
Next, use this script as your Ansible dynamic inventory script.
If your Ansible playbooks are in a different directory than your Terraform
resources, then set the TF_STATE
environment variable to the location
of the Terraform directory.
If you want to use terragrunt instead of
terraform, set the TF_TERRAGRUNT
environment variable to any non-empty
value and set TF_STATE
to the directory where the terragrunt.hcl
file is
located.
Download the latest release.
$ go get github.com/jtopjian/ansible-terraform-inventory
$ go build -o $GOPATH/bin/terraform-inventory
$ ln -s $GOPATH/bin/terraform-inventory /path/to/ansible/hosts
The following is the public key which can be used to validate releases in this repository:
mQENBGMfm0gBCAC4HubI5t9Dt21PibwDlmW1yqD6R4ix1c/mX9k3DbD2rBD4y5zY
DcVxqEbdhGkWg00diPkoTJAvjYs6Byy0Y32PDYtubK4qck33qaWcM4dwh5X+Y2n0
1/YV/UBaKdrVgS8f8Haza6wLXTB/vkCb6/3Vs3NUhNonjmkF6W2RVW0WxfJ97oO3
PP6mXocT5ES6Xnbphfih2U4C92owep0+JRUiZs9OCfG9nroGVEAZuvELJUfZVnwh
TyucBBcDCXclpG+EcKnETKZTGFVd55iVKA7OEGQJxi/1Wi5vUS1nOpNcbK6j/n+6
Z8y3aokGf6iNoiAaFOcjmmxF+yIQELr5SuRdABEBAAG0HUpvZSBUb3BqaWFuIDxq
b2VAdG9wamlhbi5uZXQ+iQFOBBMBCgA4FiEEfRTM4xWef6ivXegDh+r7T9BsimcF
AmMfm0gCGwMFCwkIBwIGFQoJCAsCBBYCAwECHgECF4AACgkQh+r7T9BsimfS4QgA
osyLeHHSEn1FZcgGizi3cPwdcAfcbFP2bSdw7fQkE77LQYPLjHZ4zXSzyqgUU7Gi
qan5Q/tjJ72Z2vZqCfC99VI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