Django, PyCharm, Vagrant and Ansible

To provide maximum isolation between customers, I prefer to set each one up on its own VPS. One down-side to this approach is it is a pain to setup a new server. In the past, I used Fabric. But recently I read High Performance Django. In that book, they go out of their way to advise against using Fabric. They like Salt. After a little googling and a few coin tosses, I decided to give Ansible a try.

At first the going was a little rough, then I discovered this gem of a repo on Github: ansible-django-stack. It even uses Vagrant, another package I have wanted to try.


This repo is insanely helpful. It even comes with a fully functional Django project. This was incredibly helpful for figuring out how to get Pycharm working with the virtual machine created by Vagrant. Just follow the docs in repo and in a few minutes you will have a fully functioning Django site, running the components any serious Django project needs. This will change your life.

Installing the Exact Same Configuration on Digital Ocean

Getting it running on a local virtual machine is pretty cool. Its even more amazing how easy it is to get it running in a Digital Ocean droplet. In a previous post, I tried and failed to setup a droplet using Ansible. So I skipped that step and use the Ansible web interface to do that. Here are the steps for creating the droplet and installing the complete stack:

  1. Setup a Droplet using Ubuntu 12.04.5×64 because that is what the Vagrantfile specifies. If you want to setup a different OS, change your Vagrantfile.
  2. Get the IP address for your new droplet
  3. cd into the root dir of ansible-django-stack and type:

ansible-playbook -i ip_address, -v development.yml --ask-sudo-pass

Make sure to include the comma after the IP address. This tells Ansible to not look for an inventory file and to just run the playbook on that server. That’s it! Go to and you will see a fully functioning Django site!

Bring the Charm

One powerful feature of Vagrant is it’s “Synced Folders” function. Actually this is a misnomer. A synced folder is a folder that is accessible on your host machine and the virtual machine. Thus there is no synching involved. They are the same physical location.

You can use this feature to edit project files “on” the virtual machine from the PyCharm installation on the host machine. To activate folder synching, do the following:

  1. git clone   /home/me/glucose-tracker
  2. Add this line to the Vagrantfile in ansible-django-stack:
config.vm.synced_folder "/home/me/glucose-tracker", "/webapps/glucosetracker/glucose-tracker"

Now open this repo with PyCharm just like you would with any other project. If you change some of the code and restart the server on the virtual machine, you will see the changes when you reload the page.

Yeah But Can I Use the PyCharm Debugger?

The best feature of PyCharm is the debugger. But will it work with Vagrant? Of course it will. Those folks at Jet Brains have got you covered.

PyCharm (v4) fully supports Vagrant and can run the python interpreter on the virtual machine. To get started, just go to settings>tools>vagrant and do what comes naturally. Then go to Tools on the menu bar and select vagrant up.

Next, go to settings>Project>Project Interpreter. Click the gear icon and select Add Remote, then select Vagrant and set the instance folder to the folder that contains Vagrantfile. Next set the python interpreter to the virtualenv for this project (note how awesome having a working project is):


Next go to Run>Edit Configurations to setup the PyCharm server. Here is what it looks like:


You will need to setup these environment vars:


Finally, you need to setup the Path Mappings like this:


And ta da! You can now run the project in PyCharm complete with debugging.

Now you can add the power of Ansible and Vagrant without sacrificing any of the power of PyCharm!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s