Edgar Latorre

I'm Edgar and this is my blog where I write about software development.

Creating a virtual machine at DigitalOcean and installing the dependencies using Vagrant and Ansible

17 Aug 2014

I’ve been using DigitalOcean in my personal projects and for me it has been a good cost benefit.

This weekend I was learning how to create a droplet using Vagrant with the plugin vagrant-digitalocean and how to use Ansible as provisioner.

So let’s see what I did to get all these things working!

You have to install Vagrant and Ansible on your local machine.

With the Vagrant installed you can install the vagrant-digitalocean plugin

vagrant plugin install vagrant-digitalocean

Now you have to add a box with the provider digital_ocean. For this I used the box that is on the DigitalOcean tutorial:

vagrant box add digital_ocean \
https://github.com/smdahlen/vagrant-digitalocean/raw/master/box/digital_ocean.box \
--provider digital_ocean

If you don’t have a ssh key, you have to generate one.

ssh-keygen -t rsa

The next step is to create the file Vagrantfile

vagrant init digitalocean

Edit the Vagrantfile to look like this

# -*- mode: ruby -*-
# vi: set ft=ruby :


Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "digital_ocean"
  config.vm.synced_folder ".", "/Projects"
  config.vm.provision "ansible" do |ansible|
    ansible.playbook = "demo.yml"
    ansible.limit = 'all'

  config.ssh.private_key_path = "~/.ssh/id_rsa"
  config.vm.provider :digital_ocean do |provider|
    provider.token = DIGITAL_OCEAN_TOKEN
    provider.image = "Ubuntu 14.04 x64"

In this tutorial I’m using a very basic Ansible play-book that installs nodejs, mongodb and nginx.

Create a demo.yml that contains the provision information

- hosts: all
  user: root
  sudo: yes
    - name: install nodejs
      apt: name=nodejs
    - name: install mongodb
      apt: name=mongodb
    - name: install nginx
      apt: name=nginx

Now we’re ready to create our virtual machine at DigitalOcean just running a vagrant up

vagrant up --provider digital_ocean

If everthing is ok, your instance on DigitalOcean is ready.

To wrap up I’ll show you some basic commands

vagrant ssh #connects to the instance via ssh
vagrant destroy #Removes the instance
vagrant halt #Stops the instance
vagrant reload #Restarts the instance

And that’s it, I hope this tutorial could help you somehow.