Yearly Archives: 2016

Run Ansible Playbooks with Terraform

Terraform from Hashicorp as the slogan is to write,plan and create structure as code.
Terraform can describe an execution plan generated from configurations of what it will do to reach the desired state, and then executes it to construct the infrastructure. It can identify what are the configuration changes, and create incremental execution plans which can be applied.

Terraform has several provisioners but it would be nice to have Ansible.

Presented here is Terraform Ansible provisioner which is a plugin provisioner that will able to run playbooks to setup the machine. Currently only supports Ubuntu for it’s bootstrap command but can be modified further to support installing Ansible for distro.

As of this writing, Terraform is on version 0.7.13 and the problem is that the Ansible provisioner, which is written in Golang, needs to be compiled to work for this Terraform version.

Here are the steps using Ubuntu Trusty using Vagrant box:

$ mkdir terraform-trusty64
$ cd terraform-trusty64/
$ vagrant init ubuntu/trusty64
$ vagrant up
$ vagrant ssh

Run this script in order to setup Golang and the rest of needed work.

$ curl -o setup.sh https://gist.githubusercontent.com/cocoy/5e22a249cbb78eeeee126473b728eaaa/raw/6678f478700aa7a07942ec651b302fdc65689161/gistfile1.txt 
$ chmod +x setup.sh
$ ./setup.sh

This will complete the compilation and setup terraform with the ansible provisioner.

$ cd ~/work/src/terraform-provisioner-ansible/example
$ curl -o example.tf https://gist.github.com/cocoy/2a0c41597c47c64fd4f9b3c73b49b86f

The example.tf was downloaded. But should be editted for access and secret keys, and other AWS related inputs.

Then finally:

$ terraform plan
$ terraform apply 

In summary, the beauty of this is we can re-use our existing playbooks/roles together with AWS and other service providers. Hopefully this provisioner will soon be added as a core part to Terraform.

Packer for building AMI on EC2 and beyond.

Using Hashicorp’s packer to build AMI on EC2 is a breeze. This tool can be used also with other platform to build images. In this example, Ansible provisioner is used to setup the instance before packer finalize the EC2 instance to be built as image.

This will build a simple Ubuntu Trusty Amazon Linux Image(AMI) with Nginx installed.
The idea is to get it to work before doing a complex playbook.

In order to run packer’s Ansible provisioner, Ansible must properly be setup.
See docs on how to setup Ansible here.

Requirements for this setup.
* Ansible installed
* Packer installed

 
$ ansible --version  | head -n1
ansible 2.1.2.0 (stable-2.1 3808a00118) last updated 2016/09/13 15:17:18 (GMT +800)

Now define the needed Environment variables for your Packer and AWS.

$ export PATH=$PATH:~/packer
$ export AWS_ACCESS_KEY_ID=YOUR_ACCESS_KEY_HERE
$ export AWS_SECRET_ACCESS_KEY=YOUR_SECRET_KEY_HERE
$ export AWS_DEFAULT_REGION=us-east-1
$ packer --version
0.10.1

The codes are here:
https://github.com/cocoy/packer-sample

$ git clone https://github.com/cocoy/packer-sample.git
$ cd packer-sample

Checking the packer_ansible.json

$ cat packer_ansible.json
{
  "builders": [{
   "type": "amazon-ebs",
   "region": "us-east-1",
   "source_ami": "ami-e902508c",
   "instance_type": "t1.micro",
   "ssh_username": "ubuntu",
   "ami_name": "Ubuntu 14.04 Packer - {{timestamp}}"
  }],

  "provisioners": [{
   "type": "ansible",
   "playbook_file": "./playbook.yml"
  }]
}

And the simple playbook:

$ cat playbook.yml
---
- hosts: all
remote_user: ubuntu
become: yes
become_method: sudo

# More roles can be added here too.
#roles:
# - { role: pcextreme.nginx }
#
pre_tasks:
- name: update apt
apt: update_cache=yes

- name: install add_apt command
apt: name=python-software-properties state=installed

- name: install nginx
apt: name=nginx

Then start building the AMI.

$ packer build packer_ansible.json

Watch how it is being launch, provisioned, and build the AMI!