Skip to content

Instantly share code, notes, and snippets.

@mikecharles
Created March 3, 2016 14:52
Show Gist options
  • Save mikecharles/d507d224d62485f1c0fb to your computer and use it in GitHub Desktop.
Save mikecharles/d507d224d62485f1c0fb to your computer and use it in GitHub Desktop.
Ansible playbook and vars file for deploying a Python application using Anaconda
# 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
# ----------------------------------------------------------------------------------------------
# Tasks
#
tasks:
- name: Check that conda is installed
command: which conda
register: conda_installed
- fail: msg="conda not installed/defined on target machine..."
when: "conda_installed.rc != 0"
- name: Clone/pull from app repo
git: >
repo={{ repo }}
dest={{ app_dir }}
force=yes
accept_hostkey=yes
key_file=~/.ssh/id_rsa
- name: Clean any untracked files in the repo
command: git clean -fd chdir={{ app_dir }}
register: git_clean
changed_when: "git_clean.stdout != ''"
- name: Create a conda virtual environment (including dependencies in conda-requirements.txt)
command: "conda 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 global variables
---
# --------------------------------------------------------------------------------------------------
# Set these variables for your app
#
# Name of your app (only use letters, numbers, dashes, or underscores)
app_name: python-skeleton
# Parent directory to contain your app(s)
app_parent_dir: "{{ HOME }}/apps"
# Git repository containing your app
repo: git@github.com:noaa-nws-cpc/python-skeleton.git
# 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
#
# $HOME environment variable on the receiving machine
HOME: "{{ ansible_env.HOME }}"
# Directory where your app will be installed
app_dir: "{{ app_parent_dir }}/{{ app_name }}"
# Directory where the Python virtual environment will be created
virtualenv_dir: "{{ app_dir }}/venv"
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment