From e68f2f2cec9199b03a1bf376026a6997122a3e82 Mon Sep 17 00:00:00 2001 From: Tobias Reisinger Date: Sun, 13 Oct 2024 23:20:11 +0200 Subject: [PATCH] Add task to stop unused services --- playbooks/serguzim.net.yml | 11 +++++++++++ playbooks/tasks/stop-unused.yml | 17 +++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 playbooks/tasks/stop-unused.yml diff --git a/playbooks/serguzim.net.yml b/playbooks/serguzim.net.yml index c75d9c2..79927c7 100644 --- a/playbooks/serguzim.net.yml +++ b/playbooks/serguzim.net.yml @@ -30,3 +30,14 @@ tags: - caddy - never + + - name: Stop unused services + ansible.builtin.include_tasks: + file: tasks/stop-unused.yml + apply: + tags: + - cleanup + - always + tags: + - cleanup + - always diff --git a/playbooks/tasks/stop-unused.yml b/playbooks/tasks/stop-unused.yml new file mode 100644 index 0000000..0cd8b11 --- /dev/null +++ b/playbooks/tasks/stop-unused.yml @@ -0,0 +1,17 @@ +- name: Get running compose projects + ansible.builtin.shell: + cmd: docker container ls --format json | jq .Labels | grep -oe 'com.docker.compose.project=[a-zA-Z0-9_-]*' | sed -e 's/com.docker.compose.project=//' | sort | uniq + changed_when: false + register: docker_compose_projects_result + +- name: Get unused services + ansible.builtin.set_fact: + unused_services: "{{ docker_compose_projects_result.stdout_lines | difference(host_services) }}" + +- name: Stop unused services + ansible.builtin.command: + cmd: docker compose down + chdir: "{{ (services_path, item) | path_join }}" + changed_when: true + loop: "{{ unused_services }}" +