-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsite.yml
More file actions
210 lines (178 loc) · 8.31 KB
/
Copy pathsite.yml
File metadata and controls
210 lines (178 loc) · 8.31 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
---
- hosts: all
vars:
ansible_python_interpreter: "/usr/bin/env python"
gather_facts: False
connection: local
tasks:
# If the variable 'server_provider' is specified in the host variables, we
# will create a new server as first step in the deploy process. But any SSH
# keys will have to be added before that can happen, which is why we
# use a local connection for these tasks; there is no host yet.
- name: Make sure all public SSH keys are on DigitalOcean.
digital_ocean: command=ssh
ssh_pub_key="{{ lookup('file', item) }}"
name={{ item | basename }}
state=present
client_id={{ do_client_id }}
api_key={{ do_api_key }}
with_fileglob:
- authorized_keys/*
register: result
when: server_provider is defined and server_provider == 'digital_ocean'
# Get or create a droplet for this hostname, which will have all available
# SSH keys added to it by default.
- name: Create a DigitalOcean droplet for this hostname.
digital_ocean: command=droplet
state=present
ssh_key_ids={{ result['results'] | map(attribute='ssh_key') | map(attribute='id') | join(',') }}
name={{ inventory_hostname }}
region_id={{ do_region_id }}
image_id={{ do_image_id }}
size_id={{ do_size_id }}
state=active
unique_name=yes
client_id={{ do_client_id }}
api_key={{ do_api_key }}
register: result
when: server_provider is defined and server_provider == 'digital_ocean'
# If a new server was created, it will have a new SSH signature. Since ip
# addresses are often reused on DigitalOcean, you will probably get one that
# you have used before. This will cause alarms to go of in SSH, and prevent
# any commands to run. To remedy, remove the relevant line from known_hosts
# before continuing.
# TODO: Do not hardcode the path to the .ssh directory.
- name: Make sure the new server's ip address is not already in known_hosts.
lineinfile: dest=~/.ssh/known_hosts
regexp="^{{ result['droplet']['ip_address'] }}"
state=absent
when: server_provider is defined and server_provider == 'digital_ocean' and result|changed
- name: Make sure the new server's host name is not already in known_hosts.
lineinfile: dest=~/.ssh/known_hosts
regexp="^{{ inventory_hostname }}"
state=absent
when: server_provider is defined and server_provider == 'digital_ocean' and result|changed
- name: Add the new server address to a group for later reference.
add_host: name={{ result['droplet']['ip_address'] }}
groups=dokku
when: server_provider is defined and server_provider == 'digital_ocean'
# This will set up some "fake" variables by exposing those available to the
# name based host to the ip based host. They will later be references using
# a kind of weird hack.
# TODO: Reorganize the whole project to avoid such folly.
- set_fact: ip_address={{ result['droplet']['ip_address'] }}
when: server_provider is defined and server_provider == 'digital_ocean'
- set_fact: dokku_git_repo={{ dokku_git_repo }}
when: server_provider is defined and server_provider == 'digital_ocean'
- set_fact: dokku_version={{ dokku_version }}
when: server_provider is defined and server_provider == 'digital_ocean'
# If server_provider is not defined for this host, we will assume that the
# hostname itself is set up and reachable through SSH.
- name: Add hostname to group for later reference.
add_host: name={{ inventory_hostname }}
groups=dokku
when: server_provider is not defined
# If the variable 'dns_provider' is specified in the host variables, we
# will create DNS entries for the current hostname, if not already present.
- name: Check if DNS records already exist.
route53: command=get
zone={{ inventory_hostname }}
record={{ inventory_hostname }}
type=A
aws_access_key={{ aws_access_key }}
aws_secret_key={{ aws_secret_key }}
register: result
when: dns_provider is defined and dns_provider == 'route53'
- name: Add A record to DNS for this hostname.
route53: command=create
zone={{ inventory_hostname }}
record={{ inventory_hostname }}
type=A
value={{ ip_address }}
ttl=300
overwrite=yes
aws_access_key={{ aws_access_key }}
aws_secret_key={{ aws_secret_key }}
when: dns_provider is defined and dns_provider == 'route53' and result.set | length == 0
- name: Add A wildcard DNS record for this hostname.
route53: command=create
zone={{ inventory_hostname }}
record="*.{{ inventory_hostname }}"
type=A
value={{ ip_address }}
ttl=300
overwrite=yes
aws_access_key={{ aws_access_key }}
aws_secret_key={{ aws_secret_key }}
when: dns_provider is defined and dns_provider == 'route53' and result.set | length == 0
- hosts: dokku
# TODO: Do not hardcode the remote username.
remote_user: root
tasks:
- set_fact: dokku_git_repo={{ item.value.dokku_git_repo }}
with_dict: hostvars
when: item.value.ip_address is defined and inventory_hostname == item.value.ip_address
- set_fact: dokku_version={{ item.value.dokku_version }}
with_dict: hostvars
when: item.value.ip_address is defined and inventory_hostname == item.value.ip_address
- name: Ensure LC_ALL is set to fix annoying local errors on pristine server
lineinfile: dest=/etc/environment
line="LC_ALL=en_US.UTF-8"
state=present
- name: Ensure LANG is set to fix annoying local errors on pristine server
lineinfile: dest=/etc/environment
line="LANG=en_US.UTF-8"
state=present
- name: Set up authorized_keys for root user
authorized_key: user=root
key="{{ lookup('file', item) }}"
state=present
with_fileglob:
- authorized_keys/*
- name: Install packages
apt: pkg={{ item }}
state=latest
with_items:
- aufs-tools
- build-essential
- git
- software-properties-common
- name: Update all package definitions and upgrade all installed packages
apt: upgrade=dist
update_cache=yes
- name: Refresh dokku repository, cloning it if non-existent
git: repo={{ dokku_git_repo }}
dest=/root/dokku
version={{ dokku_version }}
- name: Install dokku from the latest repo version
command: sudo /usr/bin/make install
chdir=/root/dokku
- name: Set up authorized_keys for dokku user
# TODO: The last touch is a hack, 'creates' should do it for us... but that doesn't work for some reason.
shell: echo {{ lookup('file', item) }} | sudo sshcommand acl-add dokku progrium && touch /home/dokku/.acl-add-done
creates=/home/dokku/.acl-add-done
with_fileglob:
- authorized_keys/*
- name: Make sure the VHOST file is set up correctly.
copy: content={{ ansible_fqdn }}
dest=/home/dokku/VHOST
force=yes
- name: Make sure the HOSTNAME file is set up correctly.
copy: content={{ ansible_fqdn }}
dest=/home/dokku/HOSTNAME
force=yes
- name: Copy custom default nginx configuration to sites
template: src=nginx.conf.j2
dest=/etc/nginx/sites-available/{{ ansible_fqdn }}
force=yes
- name: Disable the existing default nginx site
file: path=/etc/nginx/sites-enabled/default
state=absent
- name: Enable the custom default nginx site
file: src=/etc/nginx/sites-available/{{ ansible_fqdn }}
dest=/etc/nginx/sites-enabled/{{ ansible_fqdn }}
state=link
force=yes
- name: Reload nginx configuration
service: name=nginx
state=reloaded