11# Terraformed Inventory
22
33This is a little Go app which generates an dynamic [ Ansible] [ ansible] inventory
4- from a [ Terraform] [ tf] state file. It allows one to spawn a bunch of VMs with
5- Terraform, then (re-)provision them with Ansible. It's pretty neat.
4+ from a [ Terraform] [ tf] state file. It allows one to spawn a bunch of instances
5+ with Terraform, then (re-)provision them with Ansible. It's pretty neat.
66
77Currently, only ** AWS** and ** DigitalOcean** are supported.
88
@@ -19,13 +19,44 @@ add it, if you think that would be useful.
1919
2020## Usage
2121
22- Ansible doesn't (seem to) support calling the inventory script with parameters,
23- so you can specify the path to the state file using the ` TF_STATE ` environment
24- variable, like so:
22+ If your Terraform state file is named ` terraform.tfstate ` (the default), ` cd ` to
23+ it and run:
24+
25+ ansible-playbook --inventory-file=terraform-inventory deploy/playbook.yml
26+
27+ This will provide the resource names and IP addresses of any instances found in
28+ the state file to Ansible, which can then be used as hosts patterns in your
29+ playbooks. For example, given for the following Terraform config:
30+
31+ resource "digitalocean_droplet" "my-web-server" {
32+ image = "centos-7-0-x64"
33+ name = "web-1"
34+ region = "nyc1"
35+ size = "512mb"
36+ }
37+
38+ The corresponding playbook might look like:
39+
40+ - hosts: my-web-server
41+ tasks:
42+ - yum: name=cowsay
43+ - command: cowsay hello, world!
44+
45+ Note that the instance was identified by its _ resource name_ from the Terraform
46+ config, not its _ instance name_ from the provider.
47+
48+
49+ ## More Usage
50+
51+ Ansible doesn't seem to support calling a dynamic inventory script with params,
52+ so if you need to specify the location of your state file, set the ` TF_STATE `
53+ environment variable before running ` ansible-playbook ` , like:
2554
2655 TF_STATE=deploy/terraform.tfstate ansible-playbook --inventory-file=terraform-inventory deploy/playbook.yml
2756
28- Alternately, you can create a little shell script and call that. Something like:
57+ Alternately, if you need to do something fancier (like downloading your state
58+ file from S3 before running), you might wrap this tool with a shell script, and
59+ call that instead. Something like:
2960
3061 #!/bin/bash
3162 terraform-inventory $@ deploy/terraform.tfstate
@@ -49,7 +80,8 @@ To test against an example statefile, run:
4980 terraform-inventory --host=web-aws fixtures/example.tfstate
5081
5182To update the fixtures, populate ` fixtures/secrets.tfvars ` with your DO and AWS
52- account details, and run ` fixtures/update ` . You probably don't need to do this.
83+ account details, and run ` fixtures/update ` . You almost certainly don't need to
84+ do this.
5385
5486
5587## License
0 commit comments