Ansible Playbook designed for environments running a Django app. It can install and configure these applications that are commonly used in production Django deployments:
- Nginx
- Gunicorn
- PostgreSQL
- Supervisor
- Virtualenv
- Memcached
- Celery
- RabbitMQ
Default settings are stored in roles/role_name/vars/main.yml
. Environment-specific settings are in the env_vars
directory.
Tested with OS: Ubuntu 12.04.4 LTS x64
Tested with Cloud Providers: Amazon, Rackspace, Digital Ocean
A quick way to get started is with Vagrant and VirtualBox.
The main settings to change here is in the env_vars/base file, where you can configure the location of your Git project, the project name, and application name which will be used throughout the Ansible configuration.
I set some default values here using my open-source app, GlucoseTracker, so all you really have to do is type in this one command in the project root:
vagrant up
Wait a few minutes for the magic to happen. Access the app by going to this URL: http://192.168.33.15
Yup, exactly, you just provisioned a completely new server and deployed an entire Django stack in 5 minutes with two words :).
SSH to the box
vagrant ssh
Re-provision the box to apply the changes you made to the Ansible configuration
vagrant provision
Reboot the box
vagrant reload
Shutdown the box
vagrant halt
Create a Playbook for that environment and specify the user accounts to use. See development.yml for an example.
If you only have 1 server, you can skip the other steps below and simply run the Playbook with this command:
ansible-playbook -i server_ip_address_or_hostname, -v playbook.yml --ask-sudo-pass
If you have multiple servers, create an inventory file in the root folder for your environment and add your servers' hostnames or IP addresses there.
For example:
# development
[webservers]
webserver1.example.com
webserver2.example.com
[dbservers]
dbserver1.example.com
Run the Playbook:
ansible-playbook -i development -v development.yml --ask-sudo-pass
If you're testing with vagrant, you can use this command:
ansible-playbook -i vagrant_ansible_inventory_default --private-key=~/.vagrant.d/insecure_private_key -v vagrant.yml
- Ansible - Getting Started
- Ansible - Best Practices
- Setting up Django with Nginx, Gunicorn, virtualenv, supervisor and PostgreSQL
- How to deploy encrypted copies of your SSL keys and other files with Ansible and OpenSSL
I created an environments folder in my repo to keep my settings private.
$ mkdir environments $ cd environments $ mkdir env_vars $ cd env_vars copy files from env_vars to the local env_vars folder
Where I store my local environement files: MyGitRepo/ansible/environments/env_vars/
$ cd env_vars $ ln -s ../../environments/env_vars/base.yml base.yml $ ln -s ../../environments/env_vars/dev.yml dev.yml $ ln -s ../../environments/env_vars/local.yml local.yml
All files in ansible-django-stack/env_vars should point $ ls base.yml -> ../../environments/env_vars/base.yml dev.yml -> ../../environments/env_vars/dev.yml local.yml -> ../../environments/env_vars/local.yml