Ansible Playbook for setting up a Django app. This playbook installs and configures a Django application with the following technologies:
- Nginx
- Gunicorn
- Supervisor
- Virtualenv
- PostgreSQL
OS: Ubuntu, Debian
Tested with Cloud Providers: Digital Ocean, Amazon
First, create an inventory file for the environment, for example:
[webservers]
django_test ansible_ssh_user=testNext, define variables
- git_repo: "https://github.com/myarik/demo_django_ansible_setup.git"
Run the playbook:
ansible-playbook main.yml -i server.ini
When doing deployments, you can simply use the --tags option to only run those tasks with these tags.
The user can specify parameters they wish to apply.
Configuration
application_name: Application name. Defaults isdjango_testapplication_path: Application path. Defaults is/opt/{{application_name}}application_static_path: A static path. Defaults is/opt/{{application_name}}/staticfilesapplication_media_path: A media path. Defaults is/opt/{{application_name}}/mediavirtualenv_path: Application virtualenv. Defaults is/opt/{{application_name}}/.envsetup_nginx: Install and configure nginx. Default is Truesetup_postgresql: Install PostgreSQL. Default is False
Common settings
main_pkg: List of main pkgssetup_zsh: Setup zsh. Defaults istrue.git_user_name: Git username. Defaults is django_testgit_email: Git user email. Defaults isgit@django_test
Application settings
add_pkgs: List of additional packeges.git_repo: Git Repositorygit_branch: Git branch. Defaults is mastergunicorn_num_workers: The number of worker processes for handling requests. Default is 2gunicorn_max_requests: The maximum number of requests a worker will process. Default is 0
Django settings
django_environment: Django Environment variablesrequirements_file: Django requirement file. Default is{{application_path}}/requirements.txtdjango_settings_file: Django settings file. Default is{{application_path}}/config/settings/productiondjango_wsgi: Django wsgi file. Default is config.wsgidjango_manage_commands: List of django manage commands. Default ismigrateandcollectstatic
PostgreSQL settings
postgresql_pgtune: Install pgtune. Default is Falsepostgresql_encoding: Default is UTF-8database_name: Database namedatabase_user: Database userdatabase_password: Database password
For creating demo project, I used a cookiecutter template for Django
- Install and configre enviroment with PostgreSQL database. (Setup enviroment in Digital Ocean)
server.ini
[webservers]
digital_ocean_test ansible_ssh_user=rootvars:
- git_repo: "https://github.com/myarik/demo_django_ansible_setup.git"
- setup_postgresql: true
- database_name: "demo_test"
- database_user: "demo"
- database_password: "demo"
- django_environment:
DEBUG: False
DATABASE_URL: "postgres://demo:demo@localhost:5432/demo_test"
DJANGO_SECRET_KEY: be&9u)d64q^@a__^1oswsezz((%li@j^5#=f!lm32n+21x!*2@
DJANGO_ALLOWED_HOSTS: "*"- Install and configre enviroment with Sqlite database. (Setup enviroment in AWS)
server.ini
[webservers]
aws_test ansible_ssh_user=ubuntuvars:
- git_repo: "https://github.com/myarik/demo_django_ansible_setup.git"
- application_name: my_project
- application_path: /home/ubuntu/{{application_name}}
- django_environment:
DEBUG: False
DATABASE_URL: sqlite:////tmp/my-tmp-sqlite.db
DJANGO_SECRET_KEY: be&9u)d64q^@a__^1oswsezz((%li@j^5#=f!lm32n+21x!*2@
DJANGO_ALLOWED_HOSTS: "*"