2015-12-15 04:47:35 +01:00
|
|
|
# Terraform Inventory
|
2014-09-19 23:34:41 +02:00
|
|
|
|
2016-08-12 04:10:03 +02:00
|
|
|
[![Build Status](https://travis-ci.org/adammck/terraform-inventory.svg?branch=master)](https://travis-ci.org/adammck/terraform-inventory)
|
|
|
|
[![GitHub release](https://img.shields.io/github/release/adammck/terraform-inventory.svg?maxAge=2592000)](https://github.com/adammck/terraform-inventory/releases)
|
|
|
|
[![GitHub release](https://img.shields.io/homebrew/v/terraform-inventory.svg?maxAge=2592000)](http://braumeister.org/formula/terraform-inventory)
|
|
|
|
|
2016-08-12 04:13:43 +02:00
|
|
|
This is a little Go app which generates a 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
|
2015-12-15 04:47:35 +01:00
|
|
|
with Terraform, then (re-)provision them with Ansible. Currently, only **AWS**,
|
2016-08-12 04:13:43 +02:00
|
|
|
**DigitalOcean**, **CloudStack**, **VMware**, **OpenStack**, **Google Compute
|
|
|
|
Engine** are supported.
|
2015-06-05 05:17:39 +02:00
|
|
|
|
2014-09-24 20:47:37 +02:00
|
|
|
|
2016-09-22 06:18:17 +02:00
|
|
|
# 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.
|
|
|
|
|
|
|
|
|
2015-02-09 22:51:45 +01:00
|
|
|
# Installation
|
|
|
|
|
|
|
|
On OSX, install it with Homebrew:
|
|
|
|
|
2016-06-02 09:38:00 +02:00
|
|
|
brew install terraform-inventory
|
2014-09-24 20:47:37 +02:00
|
|
|
|
2016-08-12 04:13:43 +02:00
|
|
|
Alternatively, you can download a [release][rel] suitable for 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
|
|
|
|
2016-01-07 23:01:24 +01:00
|
|
|
|
2015-02-09 22:51:45 +01:00
|
|
|
## Usage
|
2014-09-24 20:47:37 +02:00
|
|
|
|
2016-08-19 04:15:55 +02:00
|
|
|
If you are using [remote state][rs] (or if your state file happens to be named
|
|
|
|
`terraform.tfstate`), `cd` to it and run:
|
2015-06-05 06:44:36 +02:00
|
|
|
|
2016-05-19 17:16:49 +02:00
|
|
|
ansible-playbook --inventory-file=/path/to/terraform-inventory deploy/playbook.yml
|
2015-06-05 06:44:36 +02:00
|
|
|
|
|
|
|
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:
|
|
|
|
|
2016-01-07 23:01:24 +01:00
|
|
|
resource "digitalocean_droplet" "my_web_server" {
|
2015-06-05 06:44:36 +02:00
|
|
|
image = "centos-7-0-x64"
|
|
|
|
name = "web-1"
|
|
|
|
region = "nyc1"
|
|
|
|
size = "512mb"
|
|
|
|
}
|
|
|
|
|
|
|
|
The corresponding playbook might look like:
|
|
|
|
|
2016-01-07 23:01:24 +01:00
|
|
|
- hosts: my_web_server
|
2015-06-05 06:44:36 +02:00
|
|
|
tasks:
|
|
|
|
- yum: name=cowsay
|
|
|
|
- command: cowsay hello, world!
|
|
|
|
|
|
|
|
Note that the instance was identified by its _resource name_ from the Terraform
|
2016-01-07 23:01:24 +01:00
|
|
|
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 = {
|
2016-01-07 23:02:26 +01:00
|
|
|
Role = "web"
|
|
|
|
Env = "dev"
|
2016-01-07 23:01:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_instance" "my_worker" {
|
|
|
|
instance_type = "t2.micro"
|
|
|
|
ami = "ami-96a818fe"
|
|
|
|
tags = {
|
2016-01-07 23:02:26 +01:00
|
|
|
Role = "worker"
|
|
|
|
Env = "dev"
|
2016-01-07 23:01:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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!
|
2015-06-05 06:44:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
## 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
|
|
|
|
2016-05-19 17:16:49 +02:00
|
|
|
TF_STATE=deploy/terraform.tfstate ansible-playbook --inventory-file=/path/to/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
|
2016-05-19 17:16:49 +02:00
|
|
|
/path/to/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
|
2014-09-19 23:34:41 +02:00
|
|
|
|
2015-06-05 04:43:56 +02:00
|
|
|
To test against an example statefile, run:
|
|
|
|
|
|
|
|
terraform-inventory --list fixtures/example.tfstate
|
2015-12-10 05:38:52 +01:00
|
|
|
terraform-inventory --host=52.7.58.202 fixtures/example.tfstate
|
2015-06-05 04:43:56 +02:00
|
|
|
|
2015-06-05 03:48:53 +02:00
|
|
|
To update the fixtures, populate `fixtures/secrets.tfvars` with your DO and AWS
|
2015-12-10 05:48:03 +01:00
|
|
|
account details, and run `fixtures/update`. To run a tiny Ansible playbook on
|
|
|
|
the example resourecs, run:
|
|
|
|
|
2016-05-19 17:16:49 +02:00
|
|
|
TF_STATE=fixtures/example.tfstate ansible-playbook --inventory-file=/path/to/terraform-inventory fixtures/playbook.yml
|
2015-12-10 05:48:03 +01:00
|
|
|
|
|
|
|
You almost certainly don't need to do any of this. Use the tests instead.
|
2015-06-05 03:48:53 +02:00
|
|
|
|
2014-09-19 23:34:41 +02:00
|
|
|
|
2016-01-05 16:25:36 +01:00
|
|
|
## Acknowledgements
|
2014-09-19 23:34:41 +02:00
|
|
|
|
2016-01-05 16:25:36 +01:00
|
|
|
Development of
|
|
|
|
[#14](https://github.com/adammck/terraform-inventory/issues/14),
|
|
|
|
[#16](https://github.com/adammck/terraform-inventory/issues/16),
|
|
|
|
and [#22](https://github.com/adammck/terraform-inventory/issues/22)
|
|
|
|
was generously sponsored by [Transloadit](https://transloadit.com).
|
2014-09-19 23:34:41 +02:00
|
|
|
|
|
|
|
|
2016-01-05 16:25:36 +01:00
|
|
|
## License
|
2014-09-19 23:34:41 +02:00
|
|
|
|
2016-01-05 16:25:36 +01:00
|
|
|
MIT.
|
2014-09-19 23:34:41 +02:00
|
|
|
|
2016-06-02 09:38:00 +02:00
|
|
|
[ansible]: https://www.ansible.com
|
|
|
|
[tf]: https://www.terraform.io
|
2016-08-19 04:15:55 +02:00
|
|
|
[rel]: https://github.com/adammck/terraform-inventory/releases
|
|
|
|
[rs]: https://www.terraform.io/docs/state/remote/index.html
|