The /bin/bash Theory


Using Ansible With Digitalocean

I started using Ansible about two months ago, because it appeared back then as an easy way to the automation world, it turns out, i was right.

Ansible is a powerful configuration management and IT automation tool created by Michael DeHaan at 2012. Ansible can be used starting from gathering information about you local machine, rolling updates, deploying small software, all the way to orchestrating a highly scalable infrastructure and managing a fleet of servers.

Ansible got one of the best documentations I’ve ever seen, it covers all the information and options needed to get you started with Ansible.

As you will discover when you start exploring this awesome tool, is that it can be used to orchestrate building and provisioning your infrastructure from scratch. In this post i will use Ansible to create, delete, and manage DigitalOcean’s droplets.

DigitalOcean API

DigitalOcean’s API allows you to control and manage the droplets using simple HTTP requests. Ansible uses dopy, a Digital Ocean python wrapper, currently Ansible integrated with dopy to communicate with the first version of the API, which uses IDs instead of names.

Let’s see some examples of using the v1 API, first you need to generate an API key from Digital Ocean admin panel:

Digital Ocean API

To list all the images used by Digital Ocean:

$ curl "https://api.digitalocean.com/images/?client_id=xxxxx&api_key=xxxxx&filter=global"|python -m json.tool

The output of the previous command will be something like that:

{
    "images": [
        {
            "distribution": "CoreOS",
            "id": 10324279,
            "name": "522.6.0 (stable)",
            "public": true,
            "region_slugs": [
                "nyc1",
                "ams1",
                ....

It is important to know the ids of the images, sizes, and regions of the droplets to be used later in the Ansible playbook, refer to here for the full guide of using the DigitalOcean API.

digital_ocean Module

Ansible provides digital_ocean module which can create, delete, and deploy ssh keys to a Digital Ocean droplet. This module requires dopy library to be installed.

To start using the digital_ocean module, you should provide Ansible with the client id and the API key. you can include the id and the key inside the playbook but it makes more sense to export them as environment variables:

$ export DO_CLIENT_ID=xxxxxx
$ export DO_API_KEY= xxxxxx

As an example for creating a 512M droplet:

create_droplet.yml:

---
- name: Digital Ocean Example
  hosts: localhost
  connection: local
  vars:
      # 512MB size
    - droplet_size: 66
      # Ubuntu 14.04 x64
    - droplet_image: 9801950
      # nyc2
    - droplet_region: 4
      # my ssh key
    - droplet_ssh_keys: 327025
  tasks:
    - name: Create New Droplet
      digital_ocean: >
        state=present
        command=droplet
        size_id={{ droplet_size }}
        region_id={{ droplet_region }}
        image_id={{ droplet_image }}
        ssh_key_ids={{ droplet_ssh_keys }}
        name=droplet-example
        unique_name=yes
      register: do_droplet

Make sure to get all the information right like the droplet size, region, and image before rush into creating a new droplet, also i set unique_names to yes to ensure idempotency as it makes sure that no other droplet exists with the same name.

After running this playbook you should see after 4 or 5 minutes that the new droplet has been created, if you ran the same playbook again you should see no change happened :

do_ok

Using Digital Ocean’s Inventory Script

Ansible provides a dynamic inventory script for Digital Ocean and most of the popular cloud service providers like AWS and Linode.

The script provides several groups to be used later in the playbook: group of ones like the droplet id and name, other groups contain droplets that belong to the same region and size, this script offers limited functionality compared to the AWS inventory script which you can use tags to group the ec2 instances. To list all the groups using the inventory script:

$ ./digital_ocean.py --list | python -m json.tool
{
 "4223694": [
"x.x.x.x"
 ],
 "distro_ubuntu-14-04-x64": [
"x.x.x.x"
 ],
 "droplet-example": [
"x.x.x.x"
 ],
 "image_6918990": [
"x.x.x.x"
.....

The dynamic inventory can be used with Ansible like any other inventory file, the following example will gather information about the newly created droplet using the setup module:

$ ansible droplet-example -u root -i digital_ocean.py -m setup

x.x.x.x | success >> {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "x.x.x.x"
        ],  
        "ansible_architecture": "x86_64",
        "ansible_bios_date": "01/01/2007",
        "ansible_bios_version": "Bochs",
        "ansible_cmdline": {
            "ro": true,
            "root": "LABEL=DOROOT"
        },
        "ansible_date_time": {
            "date": "2015-02-21",
            "day": "21",
            "epoch": "1424548725",
            "hour": "14",
            "iso8601": "2015-02-21T19:58:45Z",
            "iso8601_micro": "2015-02-21T19:58:45.958509Z",
            "minute": "58",
            "month": "02",
            ..........