forked from LemmyNet/lemmy-ansible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lemmy.yml
263 lines (235 loc) · 9.5 KB
/
lemmy.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
---
- name: Install Lemmy
hosts: all
# Install python if required
# https://www.josharcher.uk/code/ansible-python-connection-failure-ubuntu-server-1604/
gather_facts: false
vars_files:
- "inventory/host_vars/{{ domain }}/vars.yml"
pre_tasks:
- name: Assert that Ansible version is >= 2.11.0
delegate_to: localhost
ansible.builtin.assert:
that:
- "ansible_version.full is version('2.11.0', '>=')"
fail_msg: "This playbook requires Ansible 2.11.0 or higher"
become: false
- name: Check lemmy_base_dir
ansible.builtin.fail:
msg: "`lemmy_base_dir` is unset. if you are upgrading from an older version, add `lemmy_base_dir=/lemmy` to your inventory file."
when: lemmy_base_dir is not defined
- name: Check for legacy passwords/postgres file
delegate_to: localhost
ansible.builtin.stat:
path: "inventory/host_vars/{{ domain }}/passwords/postgres"
register: postgres_password_file
become: false
- name: Legacy use of passwords/postgres file
delegate_to: localhost
ansible.builtin.fail:
msg: >-
In current versions of the Lemmy Ansible playbooks, the passwords/postgres file must be renamed to passwords/postgres.psk.
See https://github.com/LemmyNet/lemmy-ansible#upgrading
when: postgres_password_file.stat.exists
become: false
- name: Check for vars.yml file
delegate_to: localhost
ansible.builtin.stat:
path: "inventory/host_vars/{{ domain }}/vars.yml"
register: vars_file
become: false
- name: Missing vars.yml file
delegate_to: localhost
ansible.builtin.fail:
msg: >-
Missing vars.yml file, please refer to the installations instructions. See https://github.com/LemmyNet/lemmy-ansible#install
and https://github.com/LemmyNet/lemmy-ansible#upgrading
when: not vars_file.stat.exists
become: false
- name: Install python for Ansible
# python2-minimal instead of python-minimal for ubuntu 20.04 and up
ansible.builtin.raw: test -e /usr/bin/python || test -e /usr/bin/python3 || (apt -y update && apt install -y python3-minimal python3-setuptools)
args:
executable: /bin/bash
register: output
changed_when: output.stdout != ''
- name: Gather facts
ansible.builtin.setup:
handlers:
- name: Reload nginx
ansible.builtin.systemd:
name: nginx
state: reloaded
tasks:
- name: Install aptitude
ansible.builtin.apt:
name: aptitude
state: present
update_cache: true
- name: Install dependencies
ansible.builtin.apt:
state: present
update_cache: true
pkg:
- "nginx"
- "certbot"
- "python3-certbot-nginx"
- "apt-transport-https"
- "ca-certificates"
- "curl"
- "gnupg"
- "software-properties-common"
- "python3-pip"
- "virtualenv"
- "python3-setuptools"
- name: Configure Docker apt repo for Ubuntu < 22.04
when: ansible_distribution == 'Ubuntu' and ansible_distribution_version < '22.04'
block:
- name: Add Docker GPG apt Key
ansible.builtin.apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
state: present
- name: Add Docker Repository
ansible.builtin.apt_repository:
repo: deb https://download.docker.com/linux/ubuntu focal stable
state: present
- name: Get architecture using dpkg
when: (ansible_distribution == 'Debian') or
(ansible_distribution == 'Ubuntu' and ansible_distribution_version >= '22.04')
ansible.builtin.command: dpkg --print-architecture
register: dpkg_output
changed_when: false
# based on https://docs.docker.com/engine/install/debian/
# and https://docs.docker.com/engine/install/ubuntu/
# note that Debian and Ubuntu use the same key
- name: Configure Docker apt repo for Debian or Ubuntu >= 22.04
when: (ansible_distribution == 'Debian') or
(ansible_distribution == 'Ubuntu' and ansible_distribution_version >= '22.04')
block:
- name: Download Docker GPG Key
ansible.builtin.get_url:
url: https://download.docker.com/linux/{{ ansible_distribution | lower }}/gpg
dest: /etc/apt/trusted.gpg.d/docker.asc
checksum: sha256:1500c1f56fa9e26b9b8f42452a553675796ade0807cdce11975eb98170b3a570
mode: "0644"
- name: Add Docker apt repo
ansible.builtin.apt_repository:
repo: >-
deb [arch={{ dpkg_output.stdout }} signed-by=/etc/apt/trusted.gpg.d/docker.asc]
https://download.docker.com/linux/{{ ansible_distribution | lower }} {{ ansible_distribution_release }} stable
state: present
- name: Install docker-ce and docker-compose
ansible.builtin.apt:
name:
- docker-ce
- docker-compose
state: present
update_cache: true
- name: Copy docker config
ansible.builtin.copy:
src: files/docker-daemon.json
dest: /etc/docker/daemon.json
mode: "0644"
- name: Request initial letsencrypt certificate
ansible.builtin.command: certbot certonly --nginx --agree-tos --cert-name '{{ domain }}' -d '{{ domain }}' -m '{{ letsencrypt_contact_email }}'
args:
creates: "/etc/letsencrypt/live/{{ domain }}/privkey.pem"
- name: Create lemmy folder
ansible.builtin.file:
path: "{{ item.path }}"
owner: "{{ item.owner }}"
state: directory
mode: "0755"
loop:
- path: "{{ lemmy_base_dir }}/{{ domain }}/"
owner: "root"
- path: "{{ lemmy_base_dir }}/{{ domain }}/volumes/"
owner: "root"
- path: "{{ lemmy_base_dir }}/{{ domain }}/volumes/pictrs/"
owner: "991"
- name: Deploy configuration files
block:
- name: Generate random port for lemmy service
ansible.builtin.set_fact:
lemmy_port: "{{ 32767 | random(start=1024) }}"
- name: Distribute nginx proxy_params configuration
ansible.builtin.copy:
src: files/proxy_params
dest: "{{ lemmy_base_dir }}/{{ domain }}/proxy_params"
owner: root
group: root
mode: "0644"
notify: Reload nginx
- name: Add template files
ansible.builtin.template:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
mode: "{{ item.mode }}"
loop:
- src: "templates/docker-compose.yml"
dest: "{{ lemmy_base_dir }}/{{ domain }}/docker-compose.yml"
mode: "0600"
- src: "templates/nginx_internal.conf"
dest: "{{ lemmy_base_dir }}/{{ domain }}/nginx_internal.conf"
mode: "0644"
- src: "templates/nginx.conf"
dest: "/etc/nginx/sites-available/{{ domain }}.conf"
mode: "0644"
notify: Reload nginx
vars:
lemmy_docker_image: "dessalines/lemmy:{{ lemmy_version | default(lookup('file', 'VERSION')) }}"
lemmy_docker_ui_image: "dessalines/lemmy-ui:{{ lemmy_ui_version | default(lemmy_version | default(lookup('file', 'VERSION'))) }}"
- name: Set up nginx sites-enabled symlink
notify: Reload nginx
block:
- name: Gather stats on site enabled config
ansible.builtin.stat:
path: "/etc/nginx/sites-enabled/{{ domain }}.conf"
register: reg_enabled
- name: Remove if regular file (legacy) instead of symlink
ansible.builtin.file:
path: "/etc/nginx/sites-enabled/{{ domain }}.conf"
state: absent
when: reg_enabled.stat.exists and reg_enabled.stat.isreg
- name: Enable nginx site
ansible.builtin.file:
src: "../sites-available/{{ domain }}.conf"
dest: "/etc/nginx/sites-enabled/{{ domain }}.conf"
state: link
- name: Add the config.hjson
ansible.builtin.template:
src: "inventory/host_vars/{{ domain }}/config.hjson"
dest: "{{ lemmy_base_dir }}/{{ domain }}/lemmy.hjson"
mode: "0600"
owner: "1000"
group: "1000"
- name: Add the customPostgresql.conf
ansible.builtin.template:
src: "inventory/host_vars/{{ domain }}/customPostgresql.conf"
dest: "{{ lemmy_base_dir }}/{{ domain }}/customPostgresql.conf"
mode: "0644"
owner: root
group: root
- name: Enable and start docker service
ansible.builtin.systemd:
name: docker
enabled: true
state: started
# - name: Change the working directory to /opt
# ansible.builtin.shell:
# cmd: find . # To list files under /opt directory
# chdir: /opt # changes to /opt directory
# register: shell_output
# - debug: var=shell_output
- name: Start docker-compose
community.docker.docker_compose:
project_src: "{{ lemmy_base_dir }}/{{ domain }}"
state: present
pull: true
remove_orphans: true
- name: Certbot renewal cronjob
ansible.builtin.cron:
special_time: daily
name: certbot-renew-lemmy
user: root
job: "certbot certonly --nginx --cert-name '{{ domain }}' -d '{{ domain }}' --deploy-hook 'nginx -s reload'"