Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jtopjian committed Feb 26, 2018
0 parents commit 4e78e24
Show file tree
Hide file tree
Showing 7 changed files with 586 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
sudo: true
dist: xenial
language: go
go:
- 1.9
install:
- go get github.com/mitchellh/gox
script:
- gox -osarch="darwin/amd64 linux/amd64" -output "dist/ansible-terraform-inventory-{{.OS}}-{{.Arch}}"
github.com/jtopjian/ansible-terraform-inventory
deploy:
provider: releases
skip_cleanup: true
api_key:
secure: SEVEmlPdHHrUZ2VPyStLFIe7I9+EuFSCF/8BZOpALhALR91tWK8gIWRcdgXErzEV8nUmp7FFePo6OkCf4bqXwwrm5su9YXOuYu6JX9HdJU7SWP9R8AXLHMOUBMBK7erqmo+M8n4D9rDbCTelu+Ewy2K7Wv7BHzYEA7CSTkb8WG8mf1JZzLs85IsOEjtOHk1f6eKtwLfG+oI+y30eReTov7tcD654LaERu3N41dzo7pwTsgqxlPQCYXk3elKQeLWOHWXAl7taWBuHKGZ+EdAGp3BIQ52YYjeM0kyyuUb12BrOXYgM0k2JOEMPSlOg/wUdPbUbpT90Ae1NM4AEnRw3rVGdnYCYP75m3HSg58jCeWTCzKrMDDcqVocK16CfqhCYiQeu2dPkRzJdycBxY+ZjoKBEZMnBH6TFmUbleATw5zrevTDof24QfDitrU4Di8EdghddUFweFjLJSZ6Gg2lJNtJ6hJJ6HSaIZ4r+J+Tbccq3PlZGQ2KMUlx92hOnv9ZRRrdxLuad2aEmuppEzmfsr3rhID97fgULNQfGp1BVTy9ri0By+8nufxriDJ6QkzQN5XqYLu7+KB7lS2POhTACs7CHp4w90JrqW56McfntN/LGiHJjdmjwCP8kDGwI3m2M6UYPDzX7ozNSximnQQsr0jOp9lBkLFUapafKhfYM36g=
file: 'dist/*'
file_glob: true
on:
repo: jtopjian/ansible-terraform-inventory
tags: true
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
ansible-terraform-inventory
===========================

A dynamic inventory script for Ansible and Terraform.

Quickstart
----------

To use this inventory script, you must first create Terraform resources
using the [terraform-provider-ansible](https://github.com/nbering/terraform-provider-ansible)
plugin:

```hcl
resource "ansible_host" "example" {
inventory_hostname = "example.com"
groups = ["web"]
vars {
ansible_user = "admin"
}
}
resource "ansible_group" "web" {
inventory_group_name = "web"
children = ["foo", "bar", "baz"]
vars {
foo = "bar"
bar = 2
}
}
```

Next, use this script as your Ansible dynamic inventory script.

If your Ansible playbooks are in a different directory than your Terraform
resources, then set the `TF_STATE` environment variable to the location
of the Terraform directory.

Installation
------------

Download the latest [release](https://github.com/jtopjian/ansible-terraform-inventory/releases).

Building From Source
--------------------

```shell
$ go get https://github.com/jtopjian/ansible-terraform-inventory
$ go build -o $GOPATH/bin/terraform-inventory
$ ln -s $GOPATH/bin/terraform-inventory /path/to/ansible/hosts
```
22 changes: 22 additions & 0 deletions fixtures/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
resource "ansible_group" "group_1" {
inventory_group_name = "group_1"
children = ["group_2"]

vars {
foo = "bar"
}
}

resource "ansible_group" "group_2" {
inventory_group_name = "group_2"
}

resource "ansible_host" "host_1" {
inventory_hostname = "host_1"
groups = ["group_1"]

vars {
ansible_user = "ubuntu"
ansible_host = "1.2.3.4"
}
}
71 changes: 71 additions & 0 deletions fixtures/terraform.tfstate
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"version": 3,
"terraform_version": "0.11.1",
"serial": 3,
"lineage": "4da608b9-cfb2-4135-9a02-cb0326eed951",
"modules": [
{
"path": [
"root"
],
"outputs": {},
"resources": {
"ansible_group.group_1": {
"type": "ansible_group",
"depends_on": [],
"primary": {
"id": "group_1",
"attributes": {
"children.#": "1",
"children.0": "group_2",
"id": "group_1",
"inventory_group_name": "group_1",
"vars.%": "1",
"vars.foo": "bar"
},
"meta": {},
"tainted": false
},
"deposed": [],
"provider": "provider.ansible"
},
"ansible_group.group_2": {
"type": "ansible_group",
"depends_on": [],
"primary": {
"id": "group_2",
"attributes": {
"id": "group_2",
"inventory_group_name": "group_2"
},
"meta": {},
"tainted": false
},
"deposed": [],
"provider": "provider.ansible"
},
"ansible_host.host_1": {
"type": "ansible_host",
"depends_on": [],
"primary": {
"id": "host_1",
"attributes": {
"groups.#": "1",
"groups.0": "group_1",
"id": "host_1",
"inventory_hostname": "host_1",
"vars.%": "2",
"vars.ansible_host": "1.2.3.4",
"vars.ansible_user": "ubuntu"
},
"meta": {},
"tainted": false
},
"deposed": [],
"provider": "provider.ansible"
}
},
"depends_on": []
}
]
}
61 changes: 61 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package main

import (
"flag"
"fmt"
"os"
"path/filepath"
)

var (
list = flag.Bool("list", false, "list mode")
)

func main() {
flag.Parse()

if *list {
file := getStatePath()
path, err := filepath.Abs(file)
if err != nil {
errAndExit(fmt.Errorf("Error determining directory: %s", err))
}

f, err := os.Stat(path)
if err != nil {
errAndExit(fmt.Errorf("Error determining directory: %s", err))
}

if !f.IsDir() {
errAndExit(fmt.Errorf("Invalid directory: %s", file))
}

s, err := getState(path)
if err != nil {
errAndExit(err)
}

j, err := s.ToJSON()
if err != nil {
errAndExit(err)
}

fmt.Println(j)
}
}

func getStatePath() string {
var v string

v = os.Getenv("TF_STATE")
if v != "" {
return v
}

return "."
}

func errAndExit(err error) {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
Loading

0 comments on commit 4e78e24

Please sign in to comment.