Awaken Your Inner DevOps with Ansible and Digital Ocean

December 10, 2013
Written by
Opinions expressed by Twilio contributors are their own

Awaken Your Inner DevOps with Ansible and Digital Ocean

For some reason I've recently been attracted to DevOps. I've done some work on Puppet and others, but I have found that I'm more productive (and enjoy) working with Ansible, recommended to me by fellow evangelist Scott Motte. With this blog series I hope to get you up and running in Ansible by deploying and provisioning a Digital Ocean Droplet.

AnsibleFirst, I would like to provide you with a simple gist of what Ansible and Digital Ocean are. Ansible is an easy-to-use tool that can execute pre-defined actions on servers such as installing dependencies, watching for changes, deploying applications, or even deploying other servers. Digital Ocean is a cloud hosting provider that is super simple and cheap.

Installing Ansible

Ansible is dead simple to install using Python's package manager, pip. Just run: sudo pip install ansible

With that you're ready with ansible on your local machine. Now let's get Digital Ocean ready.

Set up Digital Ocean

logo-digitaloceanTake the time to create an account in Digital Ocean. There are coupons out there. Tweet me @elbuo8 if you need help finding them.

After your account is up and running, let's add your SSH key to it. Doing so will save you the hassle of managing passwords for your VMs. If you don't know how to create your SSH keys, Digital Ocean has a tutorial. When you're ready to go, add your keys to Digital Ocean.

Set up the Workspace

You will be adding your Digital Ocean credentials to your environment in order to use an Ansible plugin to interface with the Digital Ocean API. Run the following commands to bootstrap your environment.

mkidr ansible-tutorial cd ansible-tutorial export DO_API_KEY=YOUR DIGITAL OCEAN API KEY export DO_CLIENT_ID=YOUR DIGITAL OCEAN CLIENT ID sudo pip install dopy echo "[localhost]nlocalhost" > localhosts curl > dohosts chmod +x dohosts

The cURL line grabs this Python script, an Ansible plugin for Digital Ocean.

Create a Droplet

In order to create a Droplet, you need to fetch some things from the Digital Ocean API:
  • ID of the SSH Key previously stored
  • ID of Image to be used
  • ID of Region
  • ID of Size
Since this might be a little bit of a hassle, I wrote a shell script that will provide you with all the needed information.

The localhosts file is one of your inventories. You can have multiple inventories. Your other inventory is a dynamic inventory which populates with your Digital Ocean Droplets. Ansible runs its Playbooks (pre-defined commands) over the servers which pattern is matched in the inventories.

Now, on with the Playbook. Save this file as newdroplet.yml. Replace the values that you obtained before.

--- - hosts: localhost tasks: - name: Create new DO Droplet digital_ocean: > state=present command=droplet name=ansible-tutorial size_id=SIZEID region_id=REGIONID image_id=IMAGEID ssh_key_ids=SSHKEYID

Almost done! Now all you have to do is run this command to create that Droplet. ansible-playbook newdroplet.yml -c local -i localhosts

And just like that, you deployed a Droplet on Digital Ocean using Ansible. You can test it's existence by doing the following command. ansible -m ping -u root -i dohosts all

I hope you found this tutorial useful! I will write a follow-up on how to use Ansible to configure a Droplet to mine some crypto currencies. Tweet at me if you have any issues on this tutorial (or improvements!).

Recommended For You

Most Popular

Send With Confidence

Partner with the email service trusted by developers and marketers for time-savings, scalability, and delivery expertise.