Has anyone gotten Ansible 2.0 to work w/ the DigitalOcean v2 API?

July 15, 2015 4.7k views
API DigitalOcean Ansible Configuration Management Python

There seems to be a lack of well-documented examples of using the new Ansible 2.0 digital_ocean module to create DO droplets, using the v2 API. If anyone has done it, could you share a working code example please? Thanks!

Also posted more about this here:

  • Hello
    I started a similar challenge last weekend, and so far only managed to realize the same as you: documentation shows Ansible v2 but only v1.9 is available, so there is a lack of coherence with the doc.
    This is how I was trying to launch a droplet before quitting to do other stuff. See my playbook in case it can help:

    • hosts: localhost connection: local gather_facts: False

    # Create a new Droplet
    # Will return the droplet details including the droplet id (used for idempotence)

    - name: Provision a set of instances
      digital_ocean: >
      register: my_droplet
  • Just for clarity: there are 2 problems

    • Digital ocean having v1 and v2 API
    • Ansible having online docs for v2 when the code that you deploy is v1.9

    Good luck :) I'll try again this weekend

  • Hi marcosgm: I'm using Ansible from git checkout, since they're supposed to announce/release Ansible 2 this month or Real-Soon-Now. Also, DO is obsoleting the v1 API completely by this Fall, so one should avoid it.

    That being said, things still don't work from Ansible to create DO droplets. I'll try again a bit later. But someone from DO should look into this, it's in their direct financial interest to make it easier for people to spin-up more droplets. :-)

  • This thread is kind of old, but I have Ansible 2.0 (built from source see here: running against v2 api.
    Don't forget to: sudo pip install dopy (found this out by tracing errors :(

    can create droplets no problem. the ansible example fails due to misnamed slug.

  • Hi there,

    I'm new to Ansible, but am making some progress and managed to get this working with version 2.0.2 on a ubuntu machine (at least to the place where I can create machines).. other testing still in progress........

    This should help you out...... (please experiment on an image or do a droplet snapshot first just in case!). I know VERY little about Python at this time and it could mess up your machine. Use at your own risk.

    The module was not properly finding something called DoError.

    I did some research and the following as pre-requisites to recompiling using pip --upgrade. The first issue appears to be that IF you need to upgrade modules the apt package for ansible (I use ubuntu) was missing some dependencies to recompile (or upgrade). I did not previously have python installed so everything necessary isn't there to --upgrade using pip.

    To allow me to compile Phython modules, I did this...

    sudo apt-get install python-dev libxml2-dev libxslt-dev

    Now, you can compile (upgrade the modules that were missing from the ansible package.

    pip install paramiko PyYAML Jinja2 httplib2 six --upgrade

    You might need to run this with (I haven't figured out the python pip way of thinking yet.. Do what makes sense for you.

    sudo pip install paramiko PyYAML Jinja2 httplib2 six --upgrade
    pip install paramiko PyYAML Jinja2 httplib2 six --upgrade

    Now, you should be able to run the digitalocean: role in v2 (at least I can).

    Here's a code snippet for you that I have actually USED to create a DO droplet..

    My ansible version is...

    Here's a code snippet that seems to actually work... (the whole ">" thing is totally new to me.. Nothing in the ansible world seems to work that way.. I converted it to what the documentation generally says for parameters).

    This worked (key adjusted of course)...

    # digital_ocean_create_droplet_v2.yml
    # Create a new Droplet (version 2 of ansible, version 2 Digital Ocean API)
    # Will return the droplet details including the droplet id (used for idempotence)
    - hosts: localhost
      connection: local
      gather_facts: false
        - name: Create Test Host at Digital Ocean
            state: present
            command: droplet
            name: testdroplet
            api_token: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 
            size_id: 512mb
            region_id: tor1
            image_id: ubuntu-14-04-x64
            wait_timeout: 500
          register: my_droplet
        - debug: msg="ID is {{ }}"
        - debug: msg="IP is {{ my_droplet.droplet.ip_address }}"

    If you want a gist, you can grab it here.


    Good luck.

2 Answers

I know this question is a bit old, but I just wanted to follow up and point out the we recently published a tutorial covering how to use Ansible 2.0's DigitalOcean module to provision Droplets. It walks you through an example step by step.

Check it out here:

Ansible 2.0 has recently been released, and with it comes support for version 2 of the DigitalOcean API. This means that you can use Ansible to not only provision your web applications, but also to provision and manage your Droplets automatically. This tutorial will show by example how to use Ansible's support of the DigitalOcean API v2.

After much trial and error on using Ansible modules with Digital Ocean, I thought I would post an update so that others don't go down the same path and get as frustrated as I am.

Ansible/digital_ocean module
As some instructions point out, you should use dopy 0.3.5. However, both 'private_networking' and 'unique_name' do not work. I didn't thoroughly investigate, but it appears that a change in the API led to the breakage. I have found that if you use dopy 0.3.6, 'private_networking' was fixed, although I'm not sure if anything else was lost.

If you are creating a brand new domain record, this works. If the domain already exists and you use 'state=present', you will get an error. Looking at Github, it appears that a fix was submitted back in January, yet it sits unreviewed, going nowhere.

I'm pretty frustrated. I don't know Python, but considering learning it now. And trying to think of some other solution to using Ansible with Digital Ocean. :(

Have another answer? Share your knowledge.