Skip to content

Instantly share code, notes, and snippets.

@Ivaylo-Bachvarov
Created May 2, 2017 19:16
Show Gist options
  • Save Ivaylo-Bachvarov/fd7c369f2b44fb309e827d8ed962c272 to your computer and use it in GitHub Desktop.
Save Ivaylo-Bachvarov/fd7c369f2b44fb309e827d8ed962c272 to your computer and use it in GitHub Desktop.
article.md

Cookiecutter Django Ansible

More automation, more time for beer! 🍺

What is Ansible?

Ansible is an open source automation platform. It is the simplest solution out there but it is very powerful. You can use Ansible to manage your infrastructure, deploy your application, and automate tasks. It is so simple that their motto is: "Automation for everyone!"

So if you are tired of setting up VPSs manually you should definitely consider an automation framework like Ansible, Puppet or Chef.

Why are we using it?

Django is a great framework when it comes to productivity. But what happens when it comes to deployment on a VPS? Who is going to setup the infrastructure and how? Do we really have to deal with all the postgresql configuration, the NGINX files, the upstart jobs that keeps our gunicorns alive? :(

Of course we have! We are developers and we have to know all of those details but we don`t really have to make them manually for every django project that we maintain.

So here are the Ansible powers:

  • It gives you the ability to keep your configuration in code and push it to a repository.
  • That means configuration history and configuration state.
  • An easy way to provision a couple of identical machines for a horizontal scaling.
  • You don`t have to describe the setup in a documentation any more. Your code is your documentation.

Look how simple it is:

- name: Install Nginx
  apt: name=nginx state=installed
  tags: packages

- name: Ensure that the default site is disabled
  file: path=/etc/nginx/sites-enabled/default state=absent
  notify: reload nginx

- name: Ensure Nginx service is started
  service: name=nginx state=started enabled=yes

Yes, we all understand it and yes, you can keep your infrastructure described in a readable human file. That is called an Ansible role.

So no more bash scripting or manual work for the infrastructure!

Of course, you can host your django app on a PaaS like Heroku, but it is not always worth it and it is a topic for another blog post.

What is Cookiecutter Django Ansible?

This is an open source project inspired by the guys from cookiecutter-django. We all know that there are so many ways that you can setup your django structure, but a very smart couple of people have decided to make a code generation project that will show you the best. That really saves time and pain.

Cookiecutter Django Ansible project is a code generator that generates production ready Ansible code. Asking you a series of questions for the most common django cases, it generates Ansible code that you can run afterwards and setup your infrastructure.

TODO: Replace with a feature list

Here are the questions for now:

  • ansible_project_name
  • ansible_project_slug
  • application_name
  • application_slug
  • application_user
  • application_root - where on the server are you going to keep the app code
  • staging_server_domain
  • production_server_domain
  • add_your_pulic_key
  • add_celery_support
  • add_letsencrypt_certificate
  • letsencrypt_email
  • project_git_remote - in order to deploy your project from the repo

The first time when you write an Ansible code is fun but when you have to support more than one Ansible project it is more of a copy pasting then writing so this cookiecutter project comes in place.

Why did we opensource it?

I made it because I wanted to optimize my time. I found myself writing the same code for a second time so I decided to make a cookiecutter project to save some time in the future.

While I was setuping our infrastructure I asked myself "Is this the best way to set up all the services that we are using?" Is it secure enough?" I wanted to collect some feedback from sys admins and people who are more experienced. Just like the cookiecutter django project.

The next steps

  • Redis support
  • Celery beats scheduler support
  • Ansible Tests

The long term idea is to collect feedback. So if you feel like you can contribute to the project, please raise a discussion in GitHub 🍺

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment