2014-09-19 23:34:41 +02:00
|
|
|
# Terraformed Inventory
|
|
|
|
|
2014-09-24 20:47:37 +02:00
|
|
|
This is a little Go app which generates an dynamic [Ansible] [ansible] inventory
|
2015-06-05 06:44:36 +02:00
|
|
|
from a [Terraform] [tf] state file. It allows one to spawn a bunch of instances
|
|
|
|
with Terraform, then (re-)provision them with Ansible. It's pretty neat.
|
2014-09-24 20:47:37 +02:00
|
|
|
|
2015-11-05 15:40:17 +01:00
|
|
|
Currently, only **AWS**, **DigitalOcean**, **CloudStack**, **VMware**, and
|
|
|
|
**OpenStack** are supported.
|
2015-06-05 05:17:39 +02:00
|
|
|
|
2014-09-24 20:47:37 +02:00
|
|
|
|
2015-02-09 22:51:45 +01:00
|
|
|
# Installation
|
|
|
|
|
|
|
|
On OSX, install it with Homebrew:
|
|
|
|
|
|
|
|
brew install https://raw.github.com/adammck/terraform-inventory/master/homebrew/terraform-inventory.rb
|
2014-09-24 20:47:37 +02:00
|
|
|
|
2015-02-09 22:51:45 +01:00
|
|
|
This is only a tiny tool, so it's not in the main Homebrew repo. Feel free to
|
|
|
|
add it, if you think that would be useful.
|
2014-09-24 20:47:37 +02:00
|
|
|
|
2015-10-03 09:44:09 +02:00
|
|
|
Alternatively, you can download a [release](https://github.com/adammck/terraform-inventory/releases) suitable
|
|
|
|
to your platform and unzip it. Make sure the `terraform-inventory` binary is executable and you're ready to go.
|
2015-02-09 22:51:45 +01:00
|
|
|
|
|
|
|
## Usage
|
2014-09-24 20:47:37 +02:00
|
|
|
|
2015-06-05 06:44:36 +02:00
|
|
|
If your Terraform state file is named `terraform.tfstate` (the default), `cd` to
|
|
|
|
it and run:
|
|
|
|
|
|
|
|
ansible-playbook --inventory-file=terraform-inventory deploy/playbook.yml
|
|
|
|
|
|
|
|
This will provide the resource names and IP addresses of any instances found in
|
|
|
|
the state file to Ansible, which can then be used as hosts patterns in your
|
|
|
|
playbooks. For example, given for the following Terraform config:
|
|
|
|
|
|
|
|
resource "digitalocean_droplet" "my-web-server" {
|
|
|
|
image = "centos-7-0-x64"
|
|
|
|
name = "web-1"
|
|
|
|
region = "nyc1"
|
|
|
|
size = "512mb"
|
|
|
|
}
|
|
|
|
|
|
|
|
The corresponding playbook might look like:
|
|
|
|
|
|
|
|
- hosts: my-web-server
|
|
|
|
tasks:
|
|
|
|
- yum: name=cowsay
|
|
|
|
- command: cowsay hello, world!
|
|
|
|
|
|
|
|
Note that the instance was identified by its _resource name_ from the Terraform
|
|
|
|
config, not its _instance name_ from the provider.
|
|
|
|
|
|
|
|
|
|
|
|
## More Usage
|
|
|
|
|
|
|
|
Ansible doesn't seem to support calling a dynamic inventory script with params,
|
|
|
|
so if you need to specify the location of your state file, set the `TF_STATE`
|
|
|
|
environment variable before running `ansible-playbook`, like:
|
2015-05-27 20:10:43 +02:00
|
|
|
|
2015-06-05 06:17:23 +02:00
|
|
|
TF_STATE=deploy/terraform.tfstate ansible-playbook --inventory-file=terraform-inventory deploy/playbook.yml
|
2015-05-27 20:10:43 +02:00
|
|
|
|
2015-06-05 06:44:36 +02:00
|
|
|
Alternately, if you need to do something fancier (like downloading your state
|
|
|
|
file from S3 before running), you might wrap this tool with a shell script, and
|
|
|
|
call that instead. Something like:
|
2014-09-24 20:47:37 +02:00
|
|
|
|
|
|
|
#!/bin/bash
|
2014-09-25 00:54:39 +02:00
|
|
|
terraform-inventory $@ deploy/terraform.tfstate
|
2014-09-24 20:47:37 +02:00
|
|
|
|
2014-09-25 00:54:39 +02:00
|
|
|
Then run Ansible with the script as an inventory:
|
|
|
|
|
|
|
|
ansible-playbook --inventory-file=bin/inventory deploy/playbook.yml
|
2014-09-19 23:34:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
## Development
|
|
|
|
|
2015-02-09 22:51:45 +01:00
|
|
|
It's just a Go app, so the usual:
|
2014-09-19 23:34:41 +02:00
|
|
|
|
2015-02-09 22:51:45 +01:00
|
|
|
go get github.com/adammck/terraform-inventory
|
|
|
|
cd $GOPATH/adammck/terraform-inventory
|
2014-09-19 23:34:41 +02:00
|
|
|
go build
|
|
|
|
|
2015-06-05 04:43:56 +02:00
|
|
|
To test against an example statefile, run:
|
|
|
|
|
|
|
|
terraform-inventory --list fixtures/example.tfstate
|
|
|
|
terraform-inventory --host=web-aws fixtures/example.tfstate
|
|
|
|
|
2015-06-05 03:48:53 +02:00
|
|
|
To update the fixtures, populate `fixtures/secrets.tfvars` with your DO and AWS
|
2015-06-05 06:44:36 +02:00
|
|
|
account details, and run `fixtures/update`. You almost certainly don't need to
|
|
|
|
do this.
|
2015-06-05 03:48:53 +02:00
|
|
|
|
2014-09-19 23:34:41 +02:00
|
|
|
|
|
|
|
## License
|
|
|
|
|
|
|
|
MIT.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[ansible]: http://www.ansible.com
|
|
|
|
[tf]: http://www.terraform.io
|