hypebeast.flaskapp
ansible-flaskapp
Ansible role for setting up and deploying Flask applications.
Features
- Tasks for setting up and deploying a Flask application.
- Prepares a server with all necessary applications and requirements.
- Simple deployment of your Flask application.
- Uses git to download the application.
- Nginx is used as a reverse proxy.
- Supervisor manages application processes.
- Gunicorn runs the Flask application (can be replaced with a custom start script).
- Sets up Postgresql with a required database and user (optional).
- Runs custom scripts before and after installation.
- Installs npm and bower if needed.
- Runs
npm install
andbower install
during deployment (optional).
Installation
ansible-galaxy
ansible-flaskapp is available through Ansible Galaxy. To install the ansible-flaskapp role, use this command:
$ ansible-galaxy install hypebeast.flaskapp
Manually
Or you can clone the repository directly into your roles folder:
$ git clone https://github.com/hypebeast/ansible-flaskapp-stack flaskapp
Examples
Install with ansible-galaxy
First, install the role with:
ansible-galaxy install hypebeast.flaskapp
Add the role to your playbook
Include the ansible-flaskapp role in your playbook:
- hosts: all
roles:
- {role: hypebeast.flaskapp,
tags: [flaskapp-setup,flaskapp-deploy]}
This will run the setup and deployment tasks.
If you want to run only the deployment task:
- hosts: all
roles:
- {role: hypebeast.flaskapp,
tags: [flaskapp-deploy]}
Set up role variables
Customize the role variables as needed.
Role Variables
Check defaults/main.yml for the complete list of available variables.
# Name of the application, used to create directory.
flaskapp_app_name: flaskapp
# Application port
flaskapp_port: 8000
# User to run the application
flaskapp_app_user: www-data
# Directory for application source code
flaskapp_app_directory: "/opt/{{ flaskapp_app_name }}"
# Directory for application logs
flaskapp_app_log_directory: "/var/log/{{ flaskapp_app_name }}"
# Path to application requirements
flaskapp_app_requirements: "{{ flaskapp_app_directory }}/requirements.txt"
# Remote git repository for code
flaskapp_app_repository: [email protected]:hypebeast/flaskapp.git
# Version of the repository to check out
flaskapp_app_version: master
# Environment variables for commands
flaskapp_app_environment:
PATH: "{{ flaskapp_app_directory }}/bin:{{ ansible_env.PATH }}"
# Custom commands to run before and after deployment
flaskapp_app_pre_hooks: []
flaskapp_app_post_hooks: []
# Path to application config file
flaskapp_app_config:
# Python version to install
flaskapp_python_version: 2.7
# Required system packages for the application
flaskapp_app_packages:
- git
- python{{ flaskapp_python_version }}
- build-essential
- python-dev
- python-pip
- python-virtualenv
- libpq-dev
# Additional system packages for your application
flaskapp_additional_app_packages:
# Install `nodejs` and its dependencies via `npm`?
flaskapp_app_requires_npm: false
# Install frontend dependencies via `bower`?
flaskapp_app_requires_bower: false
# Install and set up Postgresql
flaskapp_use_postgresql: true
# Postgresql database user
flaskapp_postgresql_user: "{{ flaskapp_app_name }}"
# Postgresql database password
flaskapp_postgresql_password: xxxxx
# Postgresql database table name
flaskapp_postgresql_table: "{{ flaskapp_app_name }}"
# Run migrations during deployment?
flaskapp_run_migrations: false
# Directory with migration scripts
flaskapp_migrations_dir: ../migrations
# Migration command
flaskapp_migration_command: python manage.py migrate
# Nginx server name
flaskapp_nginx_server_name: "{{ flaskapp_app_name }}.domain.com"
# Directory for static files served by Nginx
flaskapp_nginx_static_dir: "{{ flaskapp_app_directory }}/static/"
# Use custom nginx config?
flaskapp_use_custom_nginx_conf: false
# Use custom supervisor config?
flaskapp_use_custom_supervisor_conf: false
# Use custom startup script for supervisor?
flaskapp_use_custom_start_script: false
Testing
To easily test the role, the project includes a Vagrantfile.
Run vagrant provision
or vagrant up
, or use ansible-playbook tests/test.yml -i vagrant-inventory
.
Author
Sebastian Ruml, sebastian@sebastianruml.name
License
See the LICENSE file for details.
Credits
Thanks to the authors of these projects:
Provisioning and deploy Flask applications
ansible-galaxy install hypebeast.flaskapp