Using Ansible with Terrafoam

Terraform is a great provisioning tool. Ansible is great to set up machines. What happens when we add both to the mix?

What is Ansible?

Ansible is a modern automation tool which makes your life easier by managing your servers for you. You just need to define the configuration in which you are interested and ansible will go ahead and do it for you, be it installing a package or configuring a server application or even restarting a service. Ansible is always ready to manage your servers.
Read more about Ansible and its installation.

What is Terrafoam?

Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. Terraform can manage existing and popular service providers as well as custom in-house solutions. In true sense, Terraform enables infrastructure as code. It works well with providers like Amazon Web Services, Google Cloud, Digital Ocean, Microsoft Azure and a lot more. Since Terraform is an independent tool, it can manage cross-cloud dependencies.

How to setup Terraform?

Terraform and its plugins are distributed as binaries. It is downloadable, in zip format, from https://www.terraform.io/downloads.html. Once downloaded, we have to extract it and make sure that we add the extracted location in $PATH variable. Typically, doing this in ~/.bashrc is the best way to manage $PATH.
We recommend extracting it in /opt/terraform and then adding the line below in ~/.bashrc

export PATH=$PATH:/opt/terraform

Important Terraform Components

Terraform consist of several components. Three of the important ones are listed below.

Resource: It is a component that has to exist in the infrastructure like a compute instance or a DNS entry.

Provider: Provider is the infrastructure provider like AWS, Google Cloud or Digital Ocean.

Provisioner: Provisioners come in action after the resource is created successfully at the provider's end. Provisioner can execute a local command, invoke chef or execute a remote commend. We can use this to call Ansible.

Setting up ownCloud using Terraform and Ansible

We will run a virtual machine on Digital Ocean (they call it a droplet) using Terraform. For this, we need to generate an API token from Digital Ocean's website. So here, our provider is Digital Ocean and resource is the droplet. We will call Ansible locally using local-exec provisioner to set up ownCloud on the Droplet. Let us create a directory where we will keep all the files.

$ mkdir OConDO
$ cd OConCO

Terrafoam loads all the files in the current directory which ends with .tf. We will create two files, credentials.tf and DO.tf. We don't really need two files but it is a good idea to keep credentials in a different file.
So, credentials.tf would look like this:

We have declared the resource to use the Fedora 22 image. We want a 512 mb droplet in Singapore region. We will name it as web-1. An additional parameter to let add my ssh key to the droplet has been added.

sleep for 30 seconds because sometimes it takes time for the droplet to allow ssh immediately.

add the IP address of the droplet with additional parameters to inventory file.

call Ansible with the inventory file.

We have a playbook ready to do the installation. The playbook, named oc-playbook.yml should look like this:

We can verify that Terraform is going to create only one Digital Ocean droplet. There are no changes to the existing droplets and there is nothing to be destroyed. Once we have verified the Terraform output, we can simply call terraform apply and once the execution is finished, we can open the browser and checkout http://<droplet_ip>/owncloud.