A command line tool for transforming terraform state into ansible inventory files
Terraform is great at deploying infrastructure. This project aims to make using ansible and terraform together easy.
pip install git+git://github.com/RobotsAndPencils/terrible
Write your terraform config as you usually would
# app.tf
resource "aws_instance" "app-server" {
ami = "aminumber"
instance_type = "t2.small"
key_name = "keyname"
count = 1
tags {
Name = "server"
}
connection {
user = "ubuntu"
key_file = "key_path"
}
}
Create a Jinja2 template for your ansible inventory file
# ansible-inventory.j2
# Inventory for provisioning app-server
#
{% for resource in resources %}
{% for key, value in resource.iteritems() -%}
{% if "aws_instance.app-server" in key %}
[app-server]
# you can reference any terraform attribute
# https://www.terraform.io/docs/providers/aws/r/instance.html
{{ value.primary.attributes.public_ip }} ansible_ssh_user=ubuntu
{%- endif %}
{%- endfor %}
{%- endfor %}
Run terraform normally, then use terrible to convert terraform state to ansible inventory
terraform plan
terraform apply
terrible --template-path $PWD \
--template ansible-inventory.j2 \
--tfstate terraform.tfstate \
--inventory-output inventory.ini \
Now you can do normal ansible commands
ansible app-server -m ping --inventory-file=inventory.ini
git clone [email protected]:RobotsAndPencils/terrible.git
You may want to set up a virtualenv by running virtualenv .venv; source .venv/bin/activate
before you use the make file.
make setup
make test
Made with ❤️ by Robots & Pencils (@robotsNpencils)