1
1
mirror of https://github.com/adammck/terraform-inventory synced 2024-11-26 11:53:48 +01:00
Terraform State → Ansible Dynamic Inventory
Go to file
2016-11-15 20:16:17 -05:00
bin Fix deprecation warning 2015-11-05 09:45:35 -05:00
fixtures Not outputting the global variables if no outputs are set. 2016-02-18 11:46:40 +01:00
HomebrewFormula Improve Homebrew installation support 2016-06-02 00:38:00 -07: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
.travis.yml Add Travis 2016-08-11 21:58:38 -04:00
cli_test.go Refactor to remove Terraform dependency 2015-02-09 16:37:30 -05:00
cli.go Create all group as per Ansible inventory script conventions (#46) 2016-09-22 00:13:26 -04:00
input_test.go Check .terraform/terraform.tfstate, for remote state (#41) 2016-08-18 22:15:55 -04:00
input.go Check .terraform/terraform.tfstate, for remote state (#41) 2016-08-18 22:15:55 -04:00
LICENSE Add LICENSE 2016-09-22 10:16:27 -04:00
main.go Check .terraform/terraform.tfstate, for remote state (#41) 2016-08-18 22:15:55 -04:00
output.go allow for non-string outputs (#45) 2016-09-06 23:05:30 -04:00
parser_test.go added SoftLayer support (#52) 2016-11-15 20:14:54 -05:00
parser.go allow for non-string outputs (#45) 2016-09-06 23:05:30 -04:00
README.md Update README.md 2016-11-15 20:16:17 -05:00
resource.go added SoftLayer support (#52) 2016-11-15 20:14:54 -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

Terraform Inventory

Build Status GitHub release GitHub release

This is a little Go app which generates a 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. Currently, only AWS, DigitalOcean, CloudStack, VMware, OpenStack, Google Compute Engine, and SoftLayer are supported.

Help Wanted 🙋

This library is stable, but I've been neglecting it somewhat on account of no longer using Ansible at work. Please drop me a line if you'd be interested in helping to maintain this tool.

Installation

On OSX, install it with Homebrew:

brew install terraform-inventory

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

Usage

If you are using remote state (or if your state file happens to be named terraform.tfstate), cd to it and run:

ansible-playbook --inventory-file=/path/to/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. On AWS, resources are also grouped by their tags. For example:

resource "aws_instance" "my_web_server" {
  instance_type = "t2.micro"
  ami = "ami-96a818fe"
  tags = {
    Role = "web"
    Env = "dev"
  }
}

resource "aws_instance" "my_worker" {
  instance_type = "t2.micro"
  ami = "ami-96a818fe"
  tags = {
    Role = "worker"
    Env = "dev"
  }
}

Can be provisioned separately with:

- hosts: role_web
  tasks:
    - command: cowsay this is a web server!

- hosts: role_worker
  tasks:
    - command: cowsay this is a worker server!

- hosts: env_dev
  tasks:
    - command: cowsay this runs on all dev servers!

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=/path/to/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
/path/to/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

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=/path/to/terraform-inventory fixtures/playbook.yml

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

Acknowledgements

Development of #14, #16, and #22 was generously sponsored by Transloadit.

License

MIT.