Last Updated: February 25, 2016
·
7.733K
· pix-art

How to setup a Digital Ocean Droplet with Ansible

A while ago I finally started my own droplet via Digital Ocean. To automise this setup I used an Ansible playbook with a basic Nginx setup. Doing it this way not only provides you with an easier setup but also easy provisioning in the future.

Since I'm new to the scene of server setup I ran into some issues so here is a step by step setup on how to get started.

Assumptions
You're running Mac OS X and Python installed. These instructions are specific to Digital Ocean but should work with any remote servers offered by any vendor.

STEP 1: Installing Xcode

To run the all upcomming commands you'll need Xcode

STEP 2: Installing Ansible

sudo easy_install pip
sudo pip install ansible --quiet

Then, if you would like to update Ansible later, just do:

sudo pip install ansible --upgrade

Ansible also uses the following Python modules that need to be installed:

sudo pip install paramiko PyYAML jinja2 httplib2

Ansible can also be installed via Homebrew if you have that installed:

brew update
brew install ansible

STEP 3: Configuring Ansible to Communicate with Servers

Let's assume that you have a servers with ip 1.2.3.4. Let's add our SSH key to the server.

3.1 SSH Keys

if you don't have an SSH key, you can generate one really easily:

ssh-keygen -t rsa -C "johndoe@example.com"

if you're running Linux, you can use ssh-copy-id to copy the key the remote servers:

ssh-copy-id -i ~/.ssh/id_rsa.pub root@1.2.3.4

if you're running on OS X, you won't have ssh-copy-id, here is an alternative:

cat ~/.ssh/id_rsa.pub | ssh root@1.2.3.4 "mkdir ~/.ssh; cat >> ~/.ssh/authorized_keys"

if you get an error about the .ssh directory existing, modify the previous to this:

cat ~/.ssh/id_rsa.pub | ssh root@1.2.3.4 "cat >> ~/.ssh/authorized_keys"

3.2 Ansible Hosts File

You can set a general hosts file but I prefer to use a hosts file in my Ansible playbook.

[servers]
1.2.3.4 

By using this you'll most likely run into some issues about authentication or permission failure. it's because you didn't specify that we want to connect with the root user. Generally, it's considered a bad idea to connect to SSH with the root user, but that's how Digital Ocean servers are configured out of the box.

You can modify your hosts file:

[servers]
1.2.3.4 ansible_connection=ssh  ansible_ssh_user=root

3.3 Testing your hosts file

ansible all -m ping -i <location of your hosts file>

This should output the following:

1.2.3.4 | success >> {
    "changed": false, 
    "ping": "pong"
}

More info can be found at: http://docs.ansible.com/ or you can check my other protip on How to create a LAMP stack with Ansible

All my tips have been moved to my blog www.pix-art.be so come check it out!

2 Responses
Add your response

Just so you know, ansible can also be installed via homebrew.

over 1 year ago ·

Added that the install commands in the list if people prefer homebrew

over 1 year ago ·