lemmy-ansible-ARM/lemmy.yml
Dessalines d9ce528278 Revert "V0.17.0 changes (#59)"
This reverts commit 02f30dfbdc.
2023-01-30 10:47:24 -05:00

133 lines
4.3 KiB
YAML

---
- hosts: all
# Install python if required
# https://www.josharcher.uk/code/ansible-python-connection-failure-ubuntu-server-1604/
gather_facts: False
pre_tasks:
- name: check lemmy_base_dir
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: install python for Ansible
# python2-minimal instead of python-minimal for ubuntu 20.04 and up
raw: test -e /usr/bin/python || (apt -y update && apt install -y python3-minimal python3-setuptools)
args:
executable: /bin/bash
register: output
changed_when: output.stdout != ''
- setup: # gather facts
tasks:
- name: install dependencies
apt:
update_cache: yes
pkg:
- 'nginx'
- 'docker-compose'
- 'docker.io'
- 'certbot'
- 'python3-certbot-nginx'
- name: copy docker config
copy: src='../files/docker-daemon.json' dest='/etc/docker/daemon.json' mode='0644'
- name: request initial letsencrypt certificate
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
file:
path: '{{item.path}}'
owner: '{{item.owner}}'
state: directory
with_items:
- path: '{{lemmy_base_dir}}/{{domain}}/'
owner: 'root'
- path: '{{lemmy_base_dir}}/{{domain}}/volumes/'
owner: 'root'
- path: '{{lemmy_base_dir}}/{{domain}}/volumes/pictrs/'
owner: '991'
- block:
- set_fact:
lemmy_port: "{{ 32767 |random(start=1024) }}"
lemmy_ui_port: "{{ 32767 |random(start=1024) }}"
pictrs_port: "{{ 32767 |random(start=1024) }}"
- name: add template files
template:
src: '{{item.src}}'
dest: '{{item.dest}}'
mode: '{{item.mode}}'
with_items:
- src: 'templates/docker-compose.yml'
dest: '{{lemmy_base_dir}}/{{domain}}/docker-compose.yml'
mode: '0600'
- src: 'templates/nginx.conf'
dest: '/etc/nginx/sites-available/{{domain}}.conf'
mode: '0644'
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')))}}"
- block:
- name: gather stats on site enabled config
stat:
path: "/etc/nginx/sites-enabled/{{domain}}.conf"
register: reg_enabled
- name: remove if regular file (legacy) instead of symlink
file:
path: "/etc/nginx/sites-enabled/{{domain}}.conf"
state: absent
when: reg_enabled.stat.exists and reg_enabled.stat.isreg
- name: enable nginx site
file:
src: '../sites-available/{{domain}}.conf'
dest: "/etc/nginx/sites-enabled/{{domain}}.conf"
state: link
- name: add the config.hjson
template:
src: 'inventory/host_vars/{{domain}}/config.hjson'
dest: '{{lemmy_base_dir}}/{{domain}}/lemmy.hjson'
mode: '0600'
owner: '1000'
group: '1000'
vars:
postgres_password: "{{ lookup('password', 'inventory/host_vars/{{domain}}/passwords/postgres chars=ascii_letters,digits') }}"
- name: enable and start docker service
systemd:
name: docker
enabled: yes
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
docker_compose:
project_src: '{{lemmy_base_dir}}/{{domain}}'
state: present
pull: yes
remove_orphans: yes
- name: reload nginx with new config
shell: nginx -s reload
- name: certbot renewal cronjob
cron:
special_time: daily
name: certbot-renew-lemmy
user: root
job: "certbot certonly --nginx --cert-name '{{ domain }}' -d '{{ domain }}' --deploy-hook 'nginx -s reload'"