diff --git a/README.md b/README.md index 7fa0128..8da0ae4 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [![CI](https://github.com/DudeCalledBro/jellyfin/actions/workflows/ci.yml/badge.svg)](https://github.com/DudeCalledBro/jellyfin/actions/workflows/ci.yml) -This repository contains my ansible and docker deployment for Jellyfin - an open-source media server. +This repository contains the Ansible code for deploying Jellyfin using Docker. ## Prerequisites @@ -23,6 +23,20 @@ This repository contains my ansible and docker deployment for Jellyfin - an open > You may have to enter a password to SSH into the target system, so you need to add `-k` after the `ansible-playbook` command. +## Watchtower + +Watchtower is a Docker container application that automates the process of updating other Docker containers. It monitors running containers and checks for changes to their base images. When Watchtower detects that a new version of an image has been pushed to a Docker registry, it performs the following actions: + +- Pulls the updated image +- Gracefully shuts down the existing container +- Restarts the container using the new image with the same runtime options + +> **Side Note!** I use Watchtower for effortless updates on my systems, as I often overlook Jellyfin updates. The installation is optional. + +```bash +ansible-playbook play-watchtower.yml +``` + ## License Copyright © 2024 Niclas Spreng diff --git a/example.config.yml b/example.config.yml index de2d4bb..4d367d3 100644 --- a/example.config.yml +++ b/example.config.yml @@ -10,3 +10,6 @@ jellyfin_docker_volumes: # jellyfin proxy tls certificates jellyfin_proxy_docker_crt: "" jellyfin_proxy_docker_key: "" + +# watchtower configuration +watchtower_docker_command: "--debug --cleanup --schedule '0 30 4 * * *'" diff --git a/play-watchtower.yml b/play-watchtower.yml new file mode 100644 index 0000000..af578a6 --- /dev/null +++ b/play-watchtower.yml @@ -0,0 +1,8 @@ +- name: Setup Watchtower + hosts: all + pre_tasks: + - name: Include Variables + ansible.builtin.include_vars: config.yml + + roles: + - watchtower diff --git a/roles/watchtower/defaults/main.yml b/roles/watchtower/defaults/main.yml new file mode 100644 index 0000000..539a6cc --- /dev/null +++ b/roles/watchtower/defaults/main.yml @@ -0,0 +1,10 @@ +# Specifies the watchtower docker image to be used. +watchtower_docker_image: "containrrr/watchtower:latest" + +# Specifies the watchtower path, owner and group. +watchtower_docker_path: /opt/watchtower +watchtower_docker_owner: root +watchtower_docker_group: root + +# Specifies the watchtower command +watchtower_docker_command: "--debug" diff --git a/roles/watchtower/handlers/main.yml b/roles/watchtower/handlers/main.yml new file mode 100644 index 0000000..ac3c507 --- /dev/null +++ b/roles/watchtower/handlers/main.yml @@ -0,0 +1,7 @@ +- name: Restart watchtower container + ansible.builtin.command: >- + docker compose --ansi never --progress plain restart + args: + chdir: "{{ watchtower_docker_path }}" + changed_when: true + listen: restart watchtower diff --git a/roles/watchtower/tasks/main.yml b/roles/watchtower/tasks/main.yml new file mode 100644 index 0000000..bd5ad99 --- /dev/null +++ b/roles/watchtower/tasks/main.yml @@ -0,0 +1,31 @@ +- name: Create watchtower directory + ansible.builtin.file: + path: "{{ watchtower_docker_path }}" + owner: "{{ watchtower_docker_owner }}" + group: "{{ watchtower_docker_group }}" + state: directory + mode: 0755 + +- name: Create watchtower docker compose deployment + ansible.builtin.template: + src: docker-compose.yml.j2 + dest: "{{ watchtower_docker_path }}/docker-compose.yml" + owner: "{{ watchtower_docker_owner }}" + group: "{{ watchtower_docker_group }}" + trim_blocks: false + mode: 0644 + notify: restart watchtower + +- name: Validate watchtower docker compose deployment + ansible.builtin.command: docker compose config --quiet + args: + chdir: "{{ watchtower_docker_path }}" + changed_when: false + +- name: Startup watchtower docker compose deployment + ansible.builtin.command: >- + docker compose --ansi never --progress plain up --detach + args: + chdir: "{{ watchtower_docker_path }}" + register: watchtower_container_startup + changed_when: "'Started' in watchtower_container_startup.stdout" diff --git a/roles/watchtower/templates/docker-compose.yml.j2 b/roles/watchtower/templates/docker-compose.yml.j2 new file mode 100644 index 0000000..26daadd --- /dev/null +++ b/roles/watchtower/templates/docker-compose.yml.j2 @@ -0,0 +1,16 @@ +{{ ansible_managed | comment }} +--- +services: + watchtower: + image: "{{ watchtower_docker_image }}" + container_name: watchtower + restart: unless-stopped + logging: + driver: json-file + options: + max-size: "100m" + max-file: "10" + command: "{{ watchtower_docker_command }}" + volumes: + - "/etc/localtime:/etc/localtime:ro" + - "/var/run/docker.sock:/var/run/docker.sock:ro"