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.
First, 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.
With that you’re ready with ansible on your local machine. Now let’s get Digital Ocean ready.
Set up Digital Ocean
Take 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.
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 https://raw.github.com/ansible/ansible/devel/plugins/inventory/digital_ocean.py > 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
- name: Create new DO Droplet
Almost done! Now all you have to do is run this command to create that Droplet.
And just like that, you deployed a Droplet on Digital Ocean using Ansible. You can test it’s existence by doing the following command.
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!).