infra_django_apache

Django Logo

Ansible Role - Python3 Django

Ansible Role to deploy one or multiple Django applications on a linux server using Apache2 as webserver.

Buy me a coffee

Molecule Test Status YamlLint Test Status PyLint Test Status Ansible-Lint Test Status Ansible Galaxy

Molecule Logs: Short, Full

Tested:

  • Debian 11

Install

# latest
ansible-galaxy role install git+https://github.com/ansibleguy/infra_django_apache

# from galaxy
ansible-galaxy install ansibleguy.infra_django_apache

# or to custom role-path
ansible-galaxy install ansibleguy.infra_django_apache --roles-path ./roles

# install dependencies
ansible-galaxy install -r requirements.yml

Usage

You want a simple Ansible GUI? Check-out my Ansible WebUI

Config

You need to define your instances by configuring the 'mariadb' dictionary!

django:
  sites:
    niceApp:
      domain: 'django.ansibleguy.net'
      project: 'super'  # the directory containing the 'settings.py' is named like this 
      
      sync_code:  # sync's local code to the remote server
        enabled: true
        src: '/home/ansibleguy/code/niceApp'
        static_src: '/home/ansibleguy/code/niceApp_static'
      
      venv: '/var/lib/niceApp'

      python_modules:
        present: ['netaddr', 'pycryptodome']
      
      env_pythonpath: ['/var/lib/myOtherApp']  # will get added to django's PYTHONPATH environmental variable

      ssl:
        mode: 'letsencrypt'

      letsencrypt:
        email: '[email protected]'

You might want to use 'ansible-vault' to encrypt your passwords:

ansible-vault encrypt_string

Execution

Run the playbook/role:

ansible-playbook -K -D -i inventory/hosts.yml django.yml --ask-vault-pass

There are also some useful tags available:

  • base => only configure basics; instances will not be touched
  • sites
  • config
  • db
  • sync => only sync local code to remote host (if enabled by user)
  • django => don't start sub-roles for apache and/or mariadb

To debug errors - you can set the 'debug' variable at runtime:

ansible-playbook -K -D -i inventory/hosts.yml playbook.yml -e debug=yes

Functionality

  • Package installation

    • Ansible dependencies (minimal)
  • Configuration

    • Apache using THIS role

    • Support for MySQL or PostgreSQL

    • Default opt-in:

      • MariaDB database using THIS role
    • Default opt-outs:

      • Database backup service
      • Special apache config => can be passed using the 'django' dictionary
      • Running 'collectstatic'
    • Default config:

      • A Self-Signed certificate will be used
      • Database type => MariaDB
        • Database will be installed automatically
      • Using a python virtual environment
      • A database migration script will be created in the venv directory

Info

  • Note: this role currently only supports debian-based systems

  • Note: Most of the role's functionality can be opted in or out.

    For all available options - see the default-config located in the main/site defaults-file!

  • Warning: Not every setting/variable you provide will be checked for validity. Bad config might break the role!

About

Role to deploy one or multiple django sites using apache2 to host them

Install
ansible-galaxy install ansibleguy/infra_django_apache
GitHub repository
License
other
Downloads
1396
Owner
[email protected] | GPG: https://badges.ansibleguy.net/public.gpg