blog.dbrgn.ch

Opbeat Release Tracking with Ansible

written on Monday, March 30, 2015 by

I recently discovered Opbeat. It seems to be a simple (and free/cheap) alternative to Newrelic, at least if you're using Django.

Opbeat Screenshot

Opbeat supports release tracking (like Newrelic does, too). That means that your deployment setup can log all deployments together with the git revision number via the Opbeat API. That helps you to identify issues that occur when deploying a buggy version (or performance improvements when deploying optimizations).

Ansible Configuration

If you use Ansible for deployment, you can implement the release tracking with a handler for the git checkout module.

First of all, define some useful Ansible variables:

git_target: 'master'
opbeat_org_id: '...'
opbeat_app_id: '...'
opbeat_secret_token: '...'

Then, change your git checkout command to look similar to this:

- name: Pull sources from the repository.
  git: repo=https://github.com/user/repo dest=/path/to/directory version={{ git_target }}
  register: git_status
  notify: register opbeat release

There are three important things to note:

  1. The git version is specified as a variable. That variable can then be used in the handler as well. Usually this is the master branch.
  2. The return value by this command is registered in the git_status variable.
  3. The register opbeat release handler is called when changes are pulled from your git repository server.

The handler looks like this:

- name: register opbeat release
  uri:
    url: 'https://intake.opbeat.com/api/v1/organizations/{{ opbeat_org_id }}/apps/{{ opbeat_app_id }}/releases/'
    method: POST
    status_code: 202
    HEADER_Authorization: 'Bearer {{ opbeat_secret_token }}'
    HEADER_Content-Type: 'application/x-www-form-urlencoded'
    body: 'rev={{ git_status.after }}&branch={{ git_target }}&status=completed'

That's it, all deployments should now be registered as a new release.

This entry was tagged ansible, django and python