Spinning up a DigitalOcean Droplet with Ansible

Jan 19, 2016 00:00 · 390 words · 2 minutes read ansible digitalocean configuration management

Recently I faced the annoying task of switching VPS providers. I am of course a good little SysAdmin / DevOps engineer so of course I had a lot of things automated, but when it came to the actual task of spinning up a new virtual server, I found that over the years a few tweaks here and there had led me to a position in that I had manual work to do.

Unacceptable.

I decided to start working on completing my Ansible configuration management setup, which wasn’t actually a lot of work, but then I realized as I was perusing the Module Index that there is a DigitalOcean module baked into the core of Ansible! This moves me into fully automated territory including provisioning. I had to give it a shot.

Unfortunately due to some minor bugs with documentation, a missing Python module, a broken Python module, and a nuance of my Mac OS X environment, the examples listed wouldn’t work as presented. This blog post will serve as a reference to my future self as to how to get this working.

Pre-requisites:

  • Brew installed
  • Python installed via Brew
  • Ansible installed via Brew

Steps:

  • Install dopy 0.3.5. The latest version (0.3.7 at time of writing) is broken, so use 0.3.5 explicitly:

pip install -I dopy==0.3.5

  • Install six:

pip install six

  • Enter a localhost entry in your inventory file. This is used by Ansible which must talk over SSH as a method to get a Python interpreter:
[localhost]
localhost ansible_python_interpreter=/usr/local/bin/python
  • Enable SSH; you can either do this through the GUI or alternatively:

sudo launchctl load -w /System/Library/LaunchDaemons/ssh.plist

  • Don’t forget to add your SSH key’s public component to your authorized_keys:

cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

With all of that duct tape out of the way, we’re onto the actual fun part. Add the following into droplet-playbook.yml

---
- hosts: localhost

  tasks:
  - name: deploy $5 droplet
    digital_ocean: >
        state=present
        command=droplet
        name=ubuntudroplet
        api_token=YOUR_API_TOKEN_HERE
        size_id=512mb
        region_id=tor1
        image_id=ubuntu-14-04-x64
        wait_timeout=500
    register: ubuntu_droplet
  - debug: msg="ID is {{ ubuntu_droplet.droplet.id }}"
  - debug: msg="IP is {{ ubuntu_droplet.droplet.ip_address }}"

With all of that in place, you can now provision a new droplet simply by:

ansible-playbook droplet-playbook.yml

If you found this post useful, and you’re thinking out about signing up for DigitalOcean, please consider doing so through my referral link; we’ll both get $ credits to our accounts.

DigitalOcean Referral Link