• Stars
    star
    1,817
  • Rank 25,552 (Top 0.6 %)
  • Language
    Go
  • License
    MIT License
  • Created about 10 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Terraform State → Ansible Dynamic Inventory

Terraform Inventory

Build Status GitHub release GitHub release

This is a little Go app which generates a dynamic Ansible inventory from a Terraform state file. It allows one to spawn a bunch of instances with Terraform, then (re-)provision them with Ansible.

The following providers are supported:

  • AWS
  • CloudStack
  • DigitalOcean
  • Docker
  • Exoscale
  • Google Compute Engine
  • Hetzner Cloud
  • libvirt
  • Linode
  • OpenStack
  • Packet
  • ProfitBricks
  • Scaleway
  • SoftLayer
  • VMware
  • Nutanix
  • Open Telekom Cloud
  • Yandex.Cloud
  • Telmate/Proxmox

It's very simple to add support for new providers. See pull requests with the provider label for examples.

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 or terraform directory, 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

or

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

If TF_STATE is a file, it parses the file as json, if TF_STATE is a directory, it runs terraform state pull inside the directory, which is supports both local and remote terraform state.

It looks for state config in this order

  • TF_STATE: environment variable of where to find either a statefile or a terraform project
  • TI_TFSTATE: another environment variable similar to TF_STATE
  • terraform.tfstate: it looks in the state file in the current directory.
  • .: lastly it assumes you are at the root of a terraform project.

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

This tool returns the public IP of the host by default. If you require the private IP of the instance to run Ansible, set the TF_KEY_NAME environment variable to private_ip before running the playbook, like:

TF_KEY_NAME=private_ip ansible-playbook --inventory-file=/path/to/terraform-inventory deploy/playbook.yml

By default, the ip address is the ansible inventory name. The TF_HOSTNAME_KEY_NAME environment variable allows you to overwrite the source of the ansible inventory name.

TF_HOSTNAME_KEY_NAME=name ansible-playbook --inventory-file=/path/to/terraform-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.

More Repositories

1

pygsm

Send and receive SMS via a GSM modem in Python
Python
132
star
2

ruby-icloud

Ruby library to access Apple's iCloud (Just reminders, for now)
Ruby
126
star
3

hexapod

Golang program for my junky robot
Go
63
star
4

rubygsm

Ruby interface to AT-compatible GSM modems
Ruby
48
star
5

minitest-stub-const

Stub constants for the duration of a block in MiniTest
Ruby
39
star
6

gh-news-feed-filters

Chrome extension to add per-repo alert filtering to GitHub's news feed
CoffeeScript
23
star
7

rubysms

Build SMS applications with Ruby
Ruby
20
star
8

pykannel

Kannel helper class (to send and receive SMS messages) in Python
Python
18
star
9

djtables

Declarative HTML table builder for Django
Python
16
star
10

forkreadme

Generate useful READMEs for GitHub forks
Ruby
16
star
11

djappsettings

Per-app default settings for Django
Python
15
star
12

icloud-reminders-cli

Command-line tool for managing iCloud reminders
Ruby
13
star
13

djtokeninput

jQuery Tokeninput for Django forms
Python
13
star
14

ranger

Generic range-based sharding prototype
Go
12
star
15

fuzz

Extract tokens from arbitrary strings in Ruby
Ruby
10
star
16

bam

Pow.cx for Django
Python
9
star
17

django-monkey-patching

Sandbox for extending Django models in a sane way
Python
8
star
18

pygnokii

Gnokii helper class (to send and receive SMS messages) in Python
Python
8
star
19

venv

Golang library to abstract and mock ENV access
Go
8
star
20

freshdirect

Golang interface to FreshDirect.com
Go
8
star
21

headless-raspbian

Create a Raspbian image with wifi and SSH
Shell
8
star
22

plumpynut

Delivery monitoring of Plumpynuts via SMS
JavaScript
7
star
23

spomskyd

RubySMS application to implement the OMC SMS Proxy Protocol
Ruby
6
star
24

socialsms

Anonymous SMS-based group messaging system
Python
6
star
25

dm-fuzz

Rich property types for DataMapper, with fancy string parsing
Ruby
6
star
26

reefer

Ruby interface to Etherpad
Ruby
6
star
27

columbawawi

Child growth monitoring over SMS
Ruby
6
star
28

restartomatic

Restart (Or re-run) a process every time the contents of a directory change
5
star
29

rapidsms-example-project

Python
5
star
30

sixaxis

Golang interface to the Sixaxis (PS3 controller)
Go
5
star
31

smsapp

Generic abstraction layer to implement SMS applications via PyKannel or PyGnokii
Python
5
star
32

crabgrass

My working copy of git://labs.riseup.net/crabgrass.git
Ruby
5
star
33

pull-status

Pull request status images (open, merged, or rejected)
Ruby
5
star
34

django-app-dependencies

Manage the dependencies between Django apps
Python
5
star
35

dynamixel

Go interface to Dynamixel servos
Go
5
star
36

minitest-pretty_diff

Pretty-print hashes and arrays before diffing them in MiniTest
Ruby
4
star
37

rapidsms-search-app

RapidSMS library for finding references to models in fuzzy strings
Python
4
star
38

gmaps-v3-mask

Inverse polygons for Google Maps v3
JavaScript
4
star
39

django-dbus-signals

Interface between Django signals and D-Bus
Python
4
star
40

rapidsms-questions-app

Python
4
star
41

gitfeeds

Git via RSS, because GitHub's News Feed is too noisy (for me)
Ruby
4
star
42

hexapod-parts

4
star
43

unicefinnovation

UNICEF Innovation dot org
PHP
3
star
44

rapidsms-community-apps

Python
3
star
45

djsms

Here be dragons
Python
3
star
46

mvln

Move a file, and create a symlink to its new location
3
star
47

dotfiles

These aren't the dotfiles you're looking for.
Shell
3
star
48

yjir

Youth Journalist Information Resource
JavaScript
3
star
49

smspoll

an Iraq Fit For Children: SMS Poll
Python
2
star
50

relreq

Require files using relative paths in Ruby
Ruby
2
star
51

git-grep-service

Sinatra JSON API to grep remote Git repos
Ruby
2
star
52

gmaps-v3-label-marker

Markers with HTML content for Google Maps v3
JavaScript
2
star
53

pants

Command-line application to transform an RSS feed into a chunk of HTML via ERB
Ruby
2
star
54

ik

Inverse Kinematics in Golang
Go
2
star
55

figleaf-pkgbuild

Arch Linux PKGBUILD for figleaf (a Python code coverage analysis tool)
1
star
56

sudoku

Rather crappy sudoku board generator in Python.
Python
1
star
57

retroport

Golang interface to USB RetroPort adapters
Go
1
star
58

rk-dont-break-the-chain

Don't Break the Chain calendar via the RunKeeper/Health Graph API
Ruby
1
star
59

hexapod-infra

Shell
1
star
60

gorubiks

Rubiks cube solver in Go. It doesn't work.
Go
1
star
61

iffc-wireframe

Python
1
star
62

krang

Etherpad plus Markdown minus like 90% of the features
JavaScript
1
star
63

handlebars

Handlebars in Go
Go
1
star
64

remotefile

Go library to read/write remote files via the local filesystem
Go
1
star
65

unicef-mapping-demo

Project Mapping and Regional Information UI
JavaScript
1
star