This repository has been archived by the owner on Dec 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsite.yml
204 lines (188 loc) · 5.65 KB
/
site.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
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
---
- hosts: web-01,snm,localhost
become: true
handlers:
- name: Restart Apache
systemd:
name: apache2
state: restarted
tasks:
- name: Create LetsEncrypt web directory
file:
path: "/var/www/acme"
state: directory
owner: "root"
group: "root"
mode: '0755'
- name: Read Group list
getent:
database: group
split: ':'
register: ge_groups
- name: Read Users list
getent:
database: passwd
register: ge_users
- name: Create group directory
file:
path: "/home/groups/"
state: directory
owner: "root"
group: "root"
mode: '0775'
- name: Create skeleton directories
file:
path: "/home/skeleton/{{ item }}"
state: directory
owner: "root"
group: "root"
mode: '0775'
loop:
- ""
- dokuwiki
- dokuwiki/public_html
- abusemod
- abusemod/public_html
- static
- static/public_html
- name: Create redirection files in the skeleton directories for wikis
copy:
dest: "/home/skeleton/{{ item.key }}/public_html/index.html"
owner: root
group: root
mode: '0664'
content: |
<html>
<head>
<title>Find our wiki ... over there!</title>
<meta http-equiv="refresh" content="0; url={{ item.value }}" />
</head>
<body>
<p>We have a <a href="{{ item.value }}">wiki</a></p>
</body>
</html>
loop:
- key: dokuwiki
value: /wiki/
- key: abusemod
value: /wiki/wiki.pl
- name: Create missing group groups
group:
name: "{{ item.key }}"
state: present
loop: "{{ lug_groups|dict2items }}"
loop_control:
label: "{{ item.key }}"
when:
- item.value.hosting == ansible_hostname
- ge_groups.ansible_facts.getent_group[item.key] is not defined
- name: Create missing group users
user:
name: "{{ item.key }}"
comment: "{{ item.value.group_name | default(item.key + ' LUG') }},,,"
home: "/home/groups/{{ item.key }}/"
password_lock: yes
system: yes
group: "{{ item.key }}"
shell: /bin/bash
state: present
loop: "{{ lug_groups|dict2items }}"
loop_control:
label: "{{ item.key }}"
when:
- item.value.hosting == ansible_hostname
- ge_users.ansible_facts.getent_passwd[item.key] is not defined
# Based on:
# https://medium.com/@khandelwal12nidhi/automate-letsencrypt-ssl-installation-with-ansible-for-multiple-domains-8453f2c3212d
- name: Check if certificate already exists.
stat:
path: /etc/letsencrypt/live/{{ item.key }}/cert.pem
register: letsencrypt_cert
loop: "{{ lug_groups|dict2items }}"
loop_control:
label: "{{ item.key }}"
when:
- item.value.hosting == ansible_hostname
- name: Ensure apache2 is stopped if no certificates are available
systemd:
name: apache2
state: stopped
loop: "{{ letsencrypt_cert.results }}"
loop_control:
label: "{{ item.item.key }}"
when:
- item.stat.exists == false
- name: Generate new certificate if one doesn't exist.
shell: |
certbot certonly --standalone --noninteractive --agree-tos --email certificates@{{ item.item.value.preferred_domain }} -d {{ item.item.value.preferred_domain }}
{%- if item.item.value.serveralias | type_debug == 'list' -%}
{%- for alias in item.item.value.serveralias %} -d {{ alias }}{% endfor -%}
{%- else %} -d {{ item.item.value.serveralias }}
{%- endif -%}
loop: "{{ letsencrypt_cert.results }}"
loop_control:
label: "{{ item.item.key }}"
when:
- item.stat.exists == false
- name: Reconfigure certbot to use the apache installer
lineinfile:
path: "/etc/letsencrypt/renewal/{{ item.item.value.preferred_domain }}.conf"
insertafter: "[renewalparams]"
regex: "^installer\\s*=\\s*"
line: "installer = apache"
loop: "{{ letsencrypt_cert.results }}"
loop_control:
label: "{{ item.item.key }}"
when:
- item.stat.exists == false
- name: Reconfigure certbot to use the apache authenticator
lineinfile:
path: "/etc/letsencrypt/renewal/{{ item.item.value.preferred_domain }}.conf"
insertafter: "[renewalparams]"
regex: "^authenticator\\s*=\\s*"
line: "authenticator = apache"
loop: "{{ letsencrypt_cert.results }}"
loop_control:
label: "{{ item.item.key }}"
when:
- item.stat.exists == false
- name: Ensure apache2 is started
systemd:
name: apache2
state: started
- name: Create apache2 site-available
template:
dest: "/etc/apache2/sites-available/{{ item.key }}.conf"
src: templates/apache/standard.j2
owner: root
group: root
mode: 0755
notify:
- Restart Apache
loop: "{{ lug_groups|dict2items }}"
loop_control:
label: "{{ item.key }}"
when:
- item.value.web == 'static' or
item.value.web == 'php' or
item.value.web == 'redirect' or
item.value.web == 'dokuwiki' or
item.value.web == 'jekyll'
- item.value.hosting == ansible_hostname
- name: Create symlinks from site-available to site-enabled
file:
src: "../sites-available/{{ item.key }}.conf"
dest: "/etc/apache2/sites-enabled/{{ item.key }}.conf"
state: link
notify:
- Restart Apache
loop: "{{ lug_groups|dict2items }}"
loop_control:
label: "{{ item.key }} = {{ item.value.web | default('error') }}"
when:
- item.value.web == 'static' or
item.value.web == 'php' or
item.value.web == 'redirect' or
item.value.web == 'dokuwiki' or
item.value.web == 'jekyll'
- item.value.hosting == ansible_hostname