1
1
mirror of https://github.com/adammck/terraform-inventory synced 2024-11-26 07:43:46 +01:00
Terraform State → Ansible Dynamic Inventory
Go to file
2015-12-09 23:51:50 -05:00
bin Fix deprecation warning 2015-11-05 09:45:35 -05:00
fixtures Update example tfstate with count=n resource 2015-12-09 23:51:50 -05:00
homebrew Update homebrew script to download version 0.5 2015-07-22 14:17:19 +01:00
pkg Add bin/dist 2015-07-05 12:28:44 -04:00
.gitignore Add bin/dist 2015-07-05 12:28:44 -04:00
cli_test.go Refactor to remove Terraform dependency 2015-02-09 16:37:30 -05:00
cli.go Match host command by Address, not Name 2015-12-09 23:34:25 -05:00
main.go Also check for terraform.tfstate in pwd 2015-06-05 00:22:08 -04:00
parser_test.go Add integration test for host command 2015-12-09 23:34:17 -05:00
parser.go Parse counters out of resource names 2015-12-09 22:52:47 -05:00
README.md Add reminder on how to run Ansible with fixture 2015-12-09 23:48:03 -05:00
version_test.go Add version info to binaries 2015-02-09 16:37:43 -05:00
version.go Add version info to binaries 2015-02-09 16:37:43 -05:00

Terraformed Inventory

This is a little Go app which generates an dynamic Ansible ansible inventory 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.

Currently, only AWS, DigitalOcean, CloudStack, VMware, and OpenStack are supported.

Installation

On OSX, install it with Homebrew:

brew install https://raw.github.com/adammck/terraform-inventory/master/homebrew/terraform-inventory.rb

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.

Alternatively, you can download a release suitable to your platform and unzip it. Make sure the terraform-inventory binary is executable and you're ready to go.

Usage

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:

TF_STATE=deploy/terraform.tfstate ansible-playbook --inventory-file=terraform-inventory deploy/playbook.yml

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:

#!/bin/bash
terraform-inventory $@ deploy/terraform.tfstate

Then run Ansible with the script as an inventory:

ansible-playbook --inventory-file=bin/inventory deploy/playbook.yml

Development

It's just a Go app, so the usual:

go get github.com/adammck/terraform-inventory
cd $GOPATH/adammck/terraform-inventory
go build

To test against an example statefile, run:

terraform-inventory --list fixtures/example.tfstate
terraform-inventory --host=52.7.58.202 fixtures/example.tfstate

To update the fixtures, populate fixtures/secrets.tfvars with your DO and AWS account details, and run fixtures/update. To run a tiny Ansible playbook on the example resourecs, run:

TF_STATE=fixtures/example.tfstate ansible-playbook --inventory-file=terraform-inventory fixtures/playbook.yml

You almost certainly don't need to do any of this. Use the tests instead.

License

MIT.