Skip to content

Instantly share code, notes, and snippets.

@stevepereira
Created August 26, 2013 13:47
Show Gist options
  • Save stevepereira/6341582 to your computer and use it in GitHub Desktop.
Save stevepereira/6341582 to your computer and use it in GitHub Desktop.
Ansible deployment script
vars.yml:
---
project_name: myproject
project_root: /var/projects/myproject
project_repo: git@bitbucket.org:username/myproject.git
system_packages:
- build-essential
- git
- libevent-dev
- nginx
- postgresql
- postgresql-server-dev-all
- python-dev
- python-setuptools
- redis-server
- postfix
- yui-compressor
python_packages:
- pip
- virtualenv
initfiles:
- backend
- gunicorn
deploy.yml:
---
- hosts: servers
vars_files:
- vars.yml
gather_facts: false
sudo: true
sudo_user: myproject
tasks:
- name: Pull sources from the repository.
git: repo=${project_repo} dest=${project_root}/code/ version=${branch}
only_if: "$vm == 0"
notify:
- restart backend
- name: Upload configuration.
copy: src=webapp_settings/local_settings.${nickname}.py dest=${project_root}/code/webapp/local_settings.py
only_if: "$vm == 0"
- name: Upgrade the virtualenv.
pip: requirements=${project_root}/code/requirements.txt virtualenv=${project_root}/env/
- name: Sync Django database.
shell: ${project_root}/env/bin/python ${project_root}/code/webapp/manage.py syncdb --migrate --noinput
- name: Generate Django media.
shell: ${project_root}/env/bin/python ${project_root}/code/webapp/manage.py generatemedia
handlers:
- include: handlers.yml
handlers.yml:
---
- name: restart nginx
service: name=nginx state=restarted
sudo_user: root
- name: reload nginx
service: name=nginx state=reloaded
sudo_user: root
- name: restart backend
action: service name=${project_name}_backend state=restarted
sudo_user: root
provision.yml:
---
- hosts: servers
vars_files:
- vars.yml
gather_facts: false
sudo: true
tasks:
- name: Create the project directory.
file: state=directory path=${project_root}
- name: Create user.
user: home=${project_root}/home/ name=${project_name} state=present
- name: Update the project directory.
file: group=${project_name} owner=${project_name} mode=755 state=directory path=${project_root}
- name: Create the code directory.
file: group=${project_name} owner=${project_name} mode=755 state=directory path=${project_root}/code/
- name: Install required system packages.
apt: pkg=${item} state=installed update-cache=yes
with_items: ${system_packages}
- name: Install required Python packages.
easy_install: name=${item}
with_items: ${python_packages}
- name: Mount code folder.
mount: fstype=vboxsf opts=uid=${project_name},gid=${project_name} name=${project_root}/code/ src=${project_name} state=mounted
only_if: "$vm == 1"
- name: Create the SSH directory.
file: state=directory path=${project_root}/home/.ssh/
only_if: "$vm == 0"
- name: Upload SSH known hosts.
copy: src=known_hosts dest=${project_root}/home/.ssh/known_hosts mode=0600
only_if: "$vm == 0"
- name: Upload SSH key.
copy: src=key dest=${project_root}/home/.ssh/id_rsa mode=0600
only_if: "$vm == 0"
- name: Create the SSL directory.
file: state=directory path=${project_root}/home/ssl/
- name: Upload SSL private key.
copy: src=files/ssl/myproject.pem dest=${project_root}/home/ssl/myproject.pem
- name: Upload SSH public key.
copy: src=files/ssl/myproject.key.encrypted dest=${project_root}/home/ssl/myproject.key
- name: Change permissions.
shell: chown -R ${project_name}:${project_name} ${project_root}
- name: Install nginx configuration file.
copy: src=files/conf/nginx.conf dest=/etc/nginx/sites-enabled/myproject
notify: restart nginx
- name: Install init scripts.
copy: src=files/init/${item}.conf dest=/etc/init/myproject_${item}.conf
with_items: ${initfiles}
- name: Create database.
shell: ${project_root}/env/bin/python ${project_root}/code/webapp/manage.py sqlcreate --router=default | sudo -u postgres psql
handlers:
- include: handlers.yml
- include: deploy.yml
- hosts: servers
vars_files:
- vars.yml
gather_facts: false
sudo: true
tasks:
- name: Restart services.
service: name=myproject_${item} state=restarted
with_items: ${initfiles}
hosts:
[remote:children]
production
staging
[servers:children]
production
staging
local
[production]
www.myproject.com: nickname=production vm=0 branch=master
[staging]
staging.myproject.com nickname=staging vm=0 branch=develop
[local]
local.myproject.com nickname=local vm=1 branch=develop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment