-
Notifications
You must be signed in to change notification settings - Fork 34
/
deploy.yml
80 lines (76 loc) · 2.45 KB
/
deploy.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# Ansible playbook for deploying a Flask app
---
# Install system apt packages
- hosts: webservers
become: yes
become_method: sudo
tasks:
- name: update cache
apt: name=python-software-properties state=present update_cache=yes cache_valid_time=43200
- name: disable lighttpd
systemd: name=lighttpd state=stopped enabled=no # To avoid conflicts with nginx
- name: install packages
apt: name={{item}} state=installed
with_items:
- tree # Optional
- python-pip # Optional
- python-dev # Optional
- python3-pip
- python3-dev
- nginx
# Install the app, note: don't do these tasks with become sudo
- hosts: webservers
tasks:
- name: clone repo
git:
repo: 'https://github.com/{{ github_user }}/{{ app_name }}.git'
dest: /home/{{ ansible_ssh_user }}/{{ app_name }}
update: yes # Does a git pull if the repo already exists
- name: install modules in a virtualenv
pip:
requirements: /home/{{ ansible_ssh_user }}/{{ app_name }}/requirements.txt
virtualenv: /home/{{ ansible_ssh_user }}/{{ app_name }}/env
virtualenv_python: python3.5
# Configure app systemd service and nginx
- hosts: webservers
become: yes
become_method: sudo
tasks:
- name: template systemd service config
copy:
src: .service
dest: /etc/systemd/system/{{ app_name }}.service
- name: start systemd app service
systemd: name={{ app_name }}.service state=restarted enabled=yes
- name: template nginx site config
template:
src: .nginx
dest: /etc/nginx/sites-available/{{ app_name }}
- name: remove default nginx site config
file: path=/etc/nginx/sites-enabled/default state=absent
# - command: mv /etc/nginx/sites-enabled/default /tmp/nginx.sites-enabled.default
- name: enable nginx site
file:
src: /etc/nginx/sites-available/{{ app_name }}
dest: /etc/nginx/sites-enabled/default
state: link
force: yes
- name: restart nginx
systemd: name=nginx state=restarted enabled=yes
- name: open firewall for nginx
ufw:
rule: allow
name: Nginx Full
# Run a quick test to verify the site is working
- hosts: webservers
tasks:
- name: get url
get_url:
url: http://{{inventory_hostname}}
dest: /tmp/index.html
- name: read html
shell: cat /tmp/index.html
register: html_contents
- name: check for string in html
when: html_contents.stdout.find('hello') != -1
debug: msg="success!"