cdriehuys.django-app
cdriehuys.django-app
This Ansible role helps you run a Django application using Gunicorn.
Note: If you find something missing in this role, feel free to open an issue so we can discuss it.
Table of Contents
Requirements
To use the settings provided by this role, you need to include the local settings file in your production settings file, like this:
# production_settings.py
DEBUG = False
ALLOWED_HOSTS = ['...']
try:
from app_package.local_settings import *
except ImportError:
pass
We also assume a certain project structure. You can change this by setting the right variables, but it’s easiest if your project looks like this:
app-name
├── app_package
│ ├── app_package
│ │ └── settings.py
│ ├── other_apps
│ └── are_here
├── LICENSE
└── requirements.txt
Role Variables
Here are the variables used by this role. All variables except the ones in the Required Variables have their default values listed.
Required
These variables must be set for the role to work.
# Link to your code repository
app_repo: https://your-repo-host/user/repo-name
# Secret key for Django
django_secret_key: your secret key here
Commonly Used
These settings control project-specific details and are often changed.
# The name of your application
app_name: app
# The name of your Python module
app_package: app
# Version of your application to deploy
app_repo_version: master
# List of required system packages
app_required_packages: []
# Django settings file to use
django_settings_module: "{{ app_package }}.settings"
Shared
These variables are common across multiple roles.
# Duration the apt cache is valid in seconds
apt_cache_time: 3600
Django
These variables configure Django's settings.
# Database configuration
django_databases: {}
# Django management commands to run during deployment
django_manage_commands: []
# Additional Django settings
django_project_settings: {}
# Root path of the Django project
django_project: /opt/{{ app_name }}
# Directory of the Django application (contains 'manage.py')
django_app_dir: "{{ django_project }}/{{ app_package }}"
# Path for the local settings file
django_local_settings: "{{ django_app_dir }}/{{ app_package }}/local_settings.py"
# Path to requirements.txt
django_requirements: "{{ django_project }}/requirements.txt"
# Path for Django log file
django_log_file: /var/log/{{ app_name }}/django.log
# Django logging configuration
django_logging: {}
# Paths for static files
django_media_root: /var/www/{{ inventory_hostname }}/media
django_static_root: /var/www/{{ inventory_hostname }}/static
Gunicorn
These variables control the configuration of the Gunicorn server that serves the Django application.
# Version of Gunicorn to install
gunicorn_version: gunicorn
# User for running Gunicorn
gunicorn_user: gunicorn
# Path for the Gunicorn systemd service unit
gunicorn_service_conf: /etc/systemd/system/gunicorn.service
# Path for the Gunicorn socket configuration
gunicorn_socket_conf: /etc/systemd/system/gunicorn.socket
# Path for the Gunicorn temporary file configuration
gunicorn_tempfile_conf: /etc/tmpfiles.d/gunicorn.conf
# Directory for runtime information
gunicorn_runtime_directory: gunicorn
# Path for Gunicorn's PID file
gunicorn_pid: /run/{{ gunicorn_runtime_directory }}/pid
# Path for the socket to communicate with Gunicorn
gunicorn_socket: /run/{{ gunicorn_runtime_directory }}/socket
# Unix path for the Gunicorn socket
gunicorn_socket_unix: "unix:{{ gunicorn_socket }}"
# Path for the Gunicorn executable
gunicorn_bin: "{{ venv }}/bin/gunicorn"
# Working directory for Gunicorn
gunicorn_working_directory: "{{ django_app_dir }}"
# Path for the WSGI application to run
gunicorn_wsgi_app: "{{ app_package }}.wsgi:application"
# Additional environment variables for Gunicorn
gunicorn_environment: {}
Dependencies
This role depends on the following:
- cdriehuys.virtualenv
Example Playbook
To run the role, include it like this:
- hosts: all
roles:
- cdriehuys.django-app
License
MIT
Author Information
Chathan Driehuys (cdriehuys@gmail.com)
ansible-galaxy install cdriehuys.django-app