Skip to content

Instantly share code, notes, and snippets.

@miku
Forked from mikecharles/README.md
Created March 4, 2019 19:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miku/15887f83b827801c2d09df3dfcdbbb5e to your computer and use it in GitHub Desktop.
Save miku/15887f83b827801c2d09df3dfcdbbb5e to your computer and use it in GitHub Desktop.
Deploy a Python application using Ansible and the Ansistrano role

To deploy your app, you'll need to install Ansible and the Ansistrano deploy and rollback Ansible Galaxy roles.

Then create a directory in your app (eg. named ansible) and put these files in there. You should only need to modify the top few variables in vars.yml to get the deployment working for your app.

To do the deployment, use a command like this:

ansible-playbook --limit <host-name> ansible/deploy.yml
# Ansible deployment playbook
---
# ------------------------------------------------------------------------------------------------
# Hosts to deploy to (set to all if you want to be able to just limit installation to specific
# hosts using the `--limit` arg to `ansible-playbook`.
#
- hosts: all
# ----------------------------------------------------------------------------------------------
# Variables
#
vars:
# Run tasks in this file after creating the 'current' release symbolic link
ansistrano_after_symlink_tasks_file: "{{ playbook_dir }}/install.yml"
# ----------------------------------------------------------------------------------------------
# Files containing additional variables
#
vars_files:
- vars.yml
# ----------------------------------------------------------------------------------------------
# Ansible roles
#
# One or more roles may be from Ansible Galaxy (ansible-galaxy install <user.role>)
#
roles:
# Miniconda installation role
- role: noaa-nws-cpc.miniconda
# Ansistrano deployment role
- role: carlosbuenosvinos.ansistrano-deploy
...
# Ansible install tasks
---
- name: Check that conda is installed
command: which {{ miniconda_command }}
register: conda_installed
- fail: msg="conda not installed/defined on target machine..."
when: "conda_installed.rc != 0"
- name: Create several directories
file: path={{ app_dir }}/{{ item }} state=directory
with_items:
- logs
- output
- work
- name: Create a conda virtual environment (including dependencies in conda-requirements.txt)
command: "{{ miniconda_command }} create --yes -p {{ virtualenv_dir }} --file={{ app_dir }}/conda-requirements.txt python={{ python_version }}"
failed_when: "conda_created.rc != 0"
register: conda_created
- name: Install remaining pip requirements (in pip-requirements.txt)
pip: >
virtualenv={{ virtualenv_dir }}
requirements={{ app_dir }}/pip-requirements.txt
extra_args={{ extra_pip_args }}
- name: Install application in the virtual environment
pip: >
virtualenv={{ virtualenv_dir }}
name={{ app_dir }}
extra_args='{{ extra_pip_args }} -e'
...
# Ansible deployment playbook
---
# ------------------------------------------------------------------------------------------------
# Hosts to deploy to (set to all if you want to be able to just limit installation to specific
# hosts using the `--limit` arg to `ansible-playbook`.
#
- hosts: all
# ----------------------------------------------------------------------------------------------
# Files containing additional variables
#
vars_files:
- vars.yml
# ----------------------------------------------------------------------------------------------
# Ansible roles
#
# One or more roles may be from Ansible Galaxy (ansible-galaxy install <user.role>)
#
roles:
# Ansistrano rollback role
- role: carlosbuenosvinos.ansistrano-rollback
...
# Ansible global variables
---
# --------------------------------------------------------------------------------------------------
# Set these variables for your app
#
# Name of your app (only use letters, numbers, dashes, or underscores) (eg. mpp-driver)
app_name:
# Directory to contain your app(s) (eg. "{{ HOME }}/apps")
app_root:
# Git repository containing your app (eg. gitlab@gitlab.ncep.noaa.gov:cpc/mpp-driver.git)
git_repo:
# Git branch to deploy
git_branch: master
# Version of Python to install in the virtual environment
python_version: 3.5
# Extra pip arguments to use when installing Python packages with pip - alternatively these can
# be defined in ~/.pip/pip.conf
extra_pip_args:
# --------------------------------------------------------------------------------------------------
# These can usually be left alone
#
# Ansistrano vars
ansistrano_allow_anonymous_stats: false
ansistrano_deploy_via: git
ansistrano_deploy_to: "{{ app_root }}/{{ app_name }}"
ansistrano_git_repo: "{{ git_repo }}"
ansistrano_git_branch: "{{ git_branch }}"
ansistrano_keep_releases: 10
# $HOME environment variable on the target machine
HOME: "{{ ansible_env.HOME }}"
# Directory where your app will be installed on the target machine
app_dir: "{{ ansistrano_release_path.stdout }}"
# Directory where the Python virtual environment will be created
virtualenv_dir: "{{ app_dir }}/venv"
miniconda_python: 3
miniconda_prefix: "{{ ansible_env.HOME }}/miniconda"
miniconda_command: "{{ miniconda_prefix }}/bin/conda"
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment