marcusianlevine.django-container

ansible-django-container

This tool adds a Django service powered by Gunicorn to your Ansible Container project. To install the service, run these commands:

# Change to your Ansible Container project directory
$ cd myproject

# Install the service
$ ansible-container install marcusianlevine.ansible-django-container

Requirements

  • You need to have Ansible Container.

  • You need an existing Ansible Container project. To create one, follow these steps:

    # Create a new project directory
    $ mkdir myproject
    
    # Change to the new directory
    $ cd myproject
    
    # Initialize the project
    $ ansible-container init
    
  • This role creates a user named "django" inside the container. The container should run as this user during deployment.

  • Your Django project must have gunicorn listed in the requirements.txt file.

  • This role not only runs collectstatic to gather your static files into the Docker image, but also supports moving your static files to another container, such as an nginx container, using Docker volumes. This is done by copying the static files to the conductor so they can be accessed by other containers created by the same Ansible Container setup.

Role Variables

Note: To differentiate between Ansible and Environment variables, Ansible variables will be lower case (this_is_ansible) while Environment variables will be capitalized (ENVIRONMENT_VARIABLE).

Ansible Role Variables

These variables can be changed in container.yml when including the role, or you can specify them in a separate file and include that in container.yml under the var_files section, with paths relative to /src (this is where your project's source code is mounted in Ansible Container).

  • project_name: The name of your Django project folder and app (following the standard Django folder structure).
  • django_environment: A set of environment variable definitions that are added to the virtualenv postactivate script (see defaults/main.yml for an example).
  • requirements_file: If your Django app's requirements.txt is not at the repo root, specify its relative path here.
  • core_source_files: The default includes requirements_file and the main directory that matches project_name. You can add more source files to be included in the image here.
  • django_static_root: The directory where Django will store static files. This should match the STATIC_ROOT setting in your Django configuration or be set via an environment variable in django_environment.
  • django_media_root: Same as django_static_root, but for the MEDIA_ROOT setting in Django.
  • manage_path: The location of your project's manage.py file, defaults to the main directory that matches project_name.
  • django_rpm_deps and django_apt_deps: Lists of packages for either apt or yum, depending on your Linux distribution (note that the same package might have different names in each package manager).
  • script_templates: A list of relative paths to extra script files (Jinja2 or plain text) you want in your image at /usr/bin/. Keep in mind that the file extension will be removed.
  • bundle_build_cmd: If you need to build something like a Webpack bundle, provide the command here.
  • bundle_build_dir: The directory from which to run the bundle_build_cmd, defaults to manage_path.
  • virtualenv_python_version: The Python version to use, defaults to python2.7.
  • pip_command: The command to execute pip, defaults to pip, it can also be pip3.
  • pip_path: The absolute path to the pip executable, defaults to /usr/bin/{{ pip_command }}.

Environment Variables

You must define the following environment variables in your container.yml for the Django service:

  • DJANGO_PORT: The port number for serving the Django app.
  • DJANGO_VENV: The absolute path to where the Django app's Python virtual environment will be located (default: /venv).
  • DJANGO_USER: The user who will own all application files and run the application (default: django).

Reasonable defaults are provided as lower-case Ansible role variables for these values, which you can add to your container.yml under the environment tag (e.g., DJANGO_VENV: '{{ django_venv }}').

Injecting Vault Secrets

The current version of Ansible Container does not support Vault encrypted files directly in container.yml, but you can include secrets in your images using Ansible playbook executed from the conductor.

In this role, the django_environment variable holds a set of environment variables that will be placed in a script that runs before every call to manage.py.

IMPORTANT: This means any secrets stored in the image will be visible unless they are flattened during deployment and can also be viewed by inspecting a running instance of your image.

This is the best way currently to programmatically include secrets using Ansible Container; if you're not sharing your built images publicly, this should not be a major concern.

License

BSD

Author Information

This was adapted from the official Ansible Container Django example project.

Written by Marcus Levine for CKM Advisors.

Informazioni sul progetto

Container-enabled role for configuring a Django app behind Gunicorn

Installa
ansible-galaxy install marcusianlevine.django-container
Licenza
bsd-3-clause
Download
657
Proprietario
Data Science + DevOps = DataEng