From d8e70c71bb0c19e661f1caee153257d163081123 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Claes=20Hallstr=C3=B6m?= Date: Thu, 26 Oct 2023 09:39:06 +0200 Subject: [PATCH] Extend docker compose service role to handle extra files/templates --- .../docker_compose_service/defaults/main.yaml | 2 + roles/docker_compose_service/tasks/main.yaml | 18 +++++++ roles/gatus/defaults/main.yaml | 2 - roles/gatus/handlers/main.yaml | 6 --- roles/gatus/tasks/main.yaml | 29 +++-------- roles/homepage/defaults/main.yaml | 3 -- roles/homepage/handlers/main.yaml | 6 --- roles/homepage/tasks/main.yaml | 50 ++++++------------- roles/mosquitto/defaults/main.yaml | 3 -- roles/mosquitto/handlers/main.yaml | 6 --- roles/mosquitto/tasks/main.yaml | 36 +++++-------- roles/pihole/defaults/main.yaml | 3 -- roles/pihole/handlers/main.yaml | 6 --- roles/pihole/tasks/main.yaml | 47 +++++------------ roles/zigbee2mqtt/defaults/main.yaml | 3 -- roles/zigbee2mqtt/handlers/main.yaml | 6 --- roles/zigbee2mqtt/tasks/main.yaml | 45 +++++------------ 17 files changed, 82 insertions(+), 189 deletions(-) delete mode 100644 roles/gatus/handlers/main.yaml delete mode 100644 roles/homepage/defaults/main.yaml delete mode 100644 roles/homepage/handlers/main.yaml delete mode 100644 roles/mosquitto/defaults/main.yaml delete mode 100644 roles/mosquitto/handlers/main.yaml delete mode 100644 roles/pihole/defaults/main.yaml delete mode 100644 roles/zigbee2mqtt/defaults/main.yaml delete mode 100644 roles/zigbee2mqtt/handlers/main.yaml diff --git a/roles/docker_compose_service/defaults/main.yaml b/roles/docker_compose_service/defaults/main.yaml index 9d5ec668..bd5a24f1 100644 --- a/roles/docker_compose_service/defaults/main.yaml +++ b/roles/docker_compose_service/defaults/main.yaml @@ -2,3 +2,5 @@ docker_compose_service_file: false docker_compose_service_template: false docker_compose_service_sub_directories: [] +docker_compose_service_extra_files: [] +docker_compose_service_extra_templates: [] diff --git a/roles/docker_compose_service/tasks/main.yaml b/roles/docker_compose_service/tasks/main.yaml index 4cda2023..12a96963 100644 --- a/roles/docker_compose_service/tasks/main.yaml +++ b/roles/docker_compose_service/tasks/main.yaml @@ -29,5 +29,23 @@ notify: "Recreate {{ docker_compose_service_name }}" when: docker_compose_service_file +- name: "Copy extra files for {{ docker_compose_service_name }}" + ansible.builtin.copy: + src: "files/{{ item | ansible.builtin.basename }}" + dest: "{{ services_path }}/{{ docker_compose_service_name }}/{{ item }}" + mode: "0644" + loop: "{{ docker_compose_service_extra_files }}" + notify: "Recreate {{ docker_compose_service_name }}" + when: docker_compose_service_extra_files | length > 0 + +- name: "Template extra files for {{ docker_compose_service_name }}" + ansible.builtin.template: + src: "templates/{{ item | ansible.builtin.basename }}.j2" + dest: "{{ services_path }}/{{ docker_compose_service_name }}/{{ item }}" + mode: "0644" + loop: "{{ docker_compose_service_extra_templates }}" + notify: "Recreate {{ docker_compose_service_name }}" + when: docker_compose_service_extra_templates | length > 0 + - name: "Flush handlers" ansible.builtin.meta: flush_handlers diff --git a/roles/gatus/defaults/main.yaml b/roles/gatus/defaults/main.yaml index d818c617..4fad97d6 100644 --- a/roles/gatus/defaults/main.yaml +++ b/roles/gatus/defaults/main.yaml @@ -1,6 +1,4 @@ --- -service_name: "{{ ansible_role_name }}" -service_path: "{{ services_path }}/{{ service_name }}" gatus_icmp_endpoints: [] gatus_dns_endpoints: [] gatus_healthcheck_endpoints: [] diff --git a/roles/gatus/handlers/main.yaml b/roles/gatus/handlers/main.yaml deleted file mode 100644 index 2972fc4c..00000000 --- a/roles/gatus/handlers/main.yaml +++ /dev/null @@ -1,6 +0,0 @@ ---- -- name: Recreate {{ service_name }} - ansible.builtin.command: - chdir: "{{ service_path }}" - cmd: docker compose up -d --force-recreate - changed_when: true diff --git a/roles/gatus/tasks/main.yaml b/roles/gatus/tasks/main.yaml index bfe8874c..1f972bb5 100644 --- a/roles/gatus/tasks/main.yaml +++ b/roles/gatus/tasks/main.yaml @@ -1,22 +1,9 @@ --- -- name: Create directories - ansible.builtin.file: - path: "{{ item }}" - state: directory - mode: "0775" - loop: - - "{{ service_path }}" - -- name: Create compose file - ansible.builtin.template: - src: "templates/compose.yaml.j2" - dest: "{{ service_path }}/compose.yaml" - mode: "0640" - notify: "Recreate {{ service_name }}" - -- name: Create config - ansible.builtin.template: - src: "templates/config.yaml.j2" - dest: "{{ service_path }}/config.yaml" - mode: "0640" - notify: "Recreate {{ service_name }}" +- name: Create docker compose service + ansible.builtin.include_role: + name: docker_compose_service + vars: + docker_compose_service_name: gatus + docker_compose_service_template: true + docker_compose_service_extra_templates: + - config.yaml diff --git a/roles/homepage/defaults/main.yaml b/roles/homepage/defaults/main.yaml deleted file mode 100644 index 9b61aee1..00000000 --- a/roles/homepage/defaults/main.yaml +++ /dev/null @@ -1,3 +0,0 @@ ---- -service_name: "{{ ansible_role_name }}" -service_path: "{{ services_path }}/{{ service_name }}" diff --git a/roles/homepage/handlers/main.yaml b/roles/homepage/handlers/main.yaml deleted file mode 100644 index 2972fc4c..00000000 --- a/roles/homepage/handlers/main.yaml +++ /dev/null @@ -1,6 +0,0 @@ ---- -- name: Recreate {{ service_name }} - ansible.builtin.command: - chdir: "{{ service_path }}" - cmd: docker compose up -d --force-recreate - changed_when: true diff --git a/roles/homepage/tasks/main.yaml b/roles/homepage/tasks/main.yaml index eadfb447..f7a3260b 100644 --- a/roles/homepage/tasks/main.yaml +++ b/roles/homepage/tasks/main.yaml @@ -1,35 +1,17 @@ --- -- name: Create directories - ansible.builtin.file: - path: "{{ item }}" - state: directory - mode: "0775" - loop: - - "{{ service_path }}" - - "{{ service_path }}/config" - -- name: Create compose file - ansible.builtin.template: - src: "templates/compose.yaml.j2" - dest: "{{ service_path }}/compose.yaml" - mode: "0640" - notify: "Recreate {{ service_name }}" - -- name: Create config - ansible.builtin.copy: - src: "files/{{ item }}" - dest: "{{ service_path }}/config/{{ item }}" - mode: "0640" - loop: - - bookmarks.yaml - - docker.yaml - - kubernetes.yaml - - settings.yaml - - widgets.yaml - notify: "Recreate {{ service_name }}" - -- name: Create config - ansible.builtin.template: - src: "templates/services.yaml.j2" - dest: "{{ service_path }}/config/services.yaml" - mode: "0640" +- name: Create docker compose service + ansible.builtin.include_role: + name: docker_compose_service + vars: + docker_compose_service_name: homepage + docker_compose_service_sub_directories: + - config + docker_compose_service_template: true + docker_compose_service_extra_files: + - config/bookmarks.yaml + - config/docker.yaml + - config/kubernetes.yaml + - config/settings.yaml + - config/widgets.yaml + docker_compose_service_extra_templates: + - config/services.yaml diff --git a/roles/mosquitto/defaults/main.yaml b/roles/mosquitto/defaults/main.yaml deleted file mode 100644 index 9b61aee1..00000000 --- a/roles/mosquitto/defaults/main.yaml +++ /dev/null @@ -1,3 +0,0 @@ ---- -service_name: "{{ ansible_role_name }}" -service_path: "{{ services_path }}/{{ service_name }}" diff --git a/roles/mosquitto/handlers/main.yaml b/roles/mosquitto/handlers/main.yaml deleted file mode 100644 index 2972fc4c..00000000 --- a/roles/mosquitto/handlers/main.yaml +++ /dev/null @@ -1,6 +0,0 @@ ---- -- name: Recreate {{ service_name }} - ansible.builtin.command: - chdir: "{{ service_path }}" - cmd: docker compose up -d --force-recreate - changed_when: true diff --git a/roles/mosquitto/tasks/main.yaml b/roles/mosquitto/tasks/main.yaml index 804e428d..3f3c81a3 100644 --- a/roles/mosquitto/tasks/main.yaml +++ b/roles/mosquitto/tasks/main.yaml @@ -1,25 +1,13 @@ --- -- name: Create directories - ansible.builtin.file: - path: "{{ item }}" - state: directory - mode: "0775" - loop: - - "{{ service_path }}" - - "{{ service_path }}/config" - - "{{ service_path }}/data" - - "{{ service_path }}/log" - -- name: Create compose file - ansible.builtin.copy: - src: "files/compose.yaml" - dest: "{{ service_path }}/compose.yaml" - mode: "0640" - notify: "Recreate {{ service_name }}" - -- name: Create config file - ansible.builtin.copy: - src: "files/mosquitto.conf" - dest: "{{ service_path }}/config/mosquitto.conf" - mode: "0640" - notify: "Recreate {{ service_name }}" +- name: Create docker compose service + ansible.builtin.include_role: + name: docker_compose_service + vars: + docker_compose_service_name: mosquitto + docker_compose_service_sub_directories: + - config + - data + - log + docker_compose_service_file: true + docker_compose_service_extra_files: + - config/mosquitto.conf diff --git a/roles/pihole/defaults/main.yaml b/roles/pihole/defaults/main.yaml deleted file mode 100644 index 50be36dd..00000000 --- a/roles/pihole/defaults/main.yaml +++ /dev/null @@ -1,3 +0,0 @@ ---- -service_name: pihole -service_path: "{{ services_path }}/{{ service_name }}" diff --git a/roles/pihole/handlers/main.yaml b/roles/pihole/handlers/main.yaml index 6db24589..583c047c 100644 --- a/roles/pihole/handlers/main.yaml +++ b/roles/pihole/handlers/main.yaml @@ -3,9 +3,3 @@ ansible.builtin.command: resolvconf -u changed_when: true become: true - -- name: Recreate {{ service_name }} - ansible.builtin.command: - chdir: "{{ service_path }}" - cmd: docker compose up -d --force-recreate - changed_when: true diff --git a/roles/pihole/tasks/main.yaml b/roles/pihole/tasks/main.yaml index 91e4954e..2d40c482 100644 --- a/roles/pihole/tasks/main.yaml +++ b/roles/pihole/tasks/main.yaml @@ -15,37 +15,16 @@ notify: Update resolvconf when: pihole_which_resolvconf.rc == 0 -- name: Create directories - ansible.builtin.file: - path: "{{ item }}" - state: directory - mode: "0775" - loop: - - "{{ service_path }}" - - "{{ service_path }}/etc-pihole" - - "{{ service_path }}/etc-dnsmasq.d" - -- name: Create compose file - ansible.builtin.copy: - src: "files/{{ item }}" - dest: "{{ service_path }}/{{ item }}" - mode: "0640" - loop: - - compose.yaml - notify: "Recreate {{ service_name }}" - -- name: Create adlists - ansible.builtin.copy: - src: "files/{{ item }}" - dest: "{{ service_path }}/etc-pihole/{{ item }}" - mode: "0640" - loop: - - adlists.list - notify: "Recreate {{ service_name }}" - -- name: Create dns overrides - ansible.builtin.template: - src: "templates/03-dns-overrides.conf.j2" - dest: "{{ service_path }}/etc-dnsmasq.d/03-dns-overrides.conf" - mode: "0640" - notify: "Recreate {{ service_name }}" +- name: Create docker compose service + ansible.builtin.include_role: + name: docker_compose_service + vars: + docker_compose_service_name: pihole + docker_compose_service_sub_directories: + - etc-pihole + - etc-dnsmasq.d + docker_compose_service_file: true + docker_compose_service_extra_files: + - etc-pihole/adlists.list + docker_compose_service_extra_templates: + - etc-dnsmasq.d/03-dns-overrides.conf diff --git a/roles/zigbee2mqtt/defaults/main.yaml b/roles/zigbee2mqtt/defaults/main.yaml deleted file mode 100644 index 40f3d3a4..00000000 --- a/roles/zigbee2mqtt/defaults/main.yaml +++ /dev/null @@ -1,3 +0,0 @@ ---- -service_name: zigbee2mqtt -service_path: "{{ services_path }}/{{ service_name }}" diff --git a/roles/zigbee2mqtt/handlers/main.yaml b/roles/zigbee2mqtt/handlers/main.yaml deleted file mode 100644 index 2972fc4c..00000000 --- a/roles/zigbee2mqtt/handlers/main.yaml +++ /dev/null @@ -1,6 +0,0 @@ ---- -- name: Recreate {{ service_name }} - ansible.builtin.command: - chdir: "{{ service_path }}" - cmd: docker compose up -d --force-recreate - changed_when: true diff --git a/roles/zigbee2mqtt/tasks/main.yaml b/roles/zigbee2mqtt/tasks/main.yaml index 24f5e4f9..47efaab2 100644 --- a/roles/zigbee2mqtt/tasks/main.yaml +++ b/roles/zigbee2mqtt/tasks/main.yaml @@ -1,36 +1,17 @@ --- -- name: Create directories - ansible.builtin.file: - path: "{{ item }}" - state: directory - mode: "0775" - loop: - - "{{ service_path }}" - - "{{ service_path }}/data" - -- name: Create compose file - ansible.builtin.copy: - src: "files/compose.yaml" - dest: "{{ service_path }}/compose.yaml" - mode: "0640" - notify: "Recreate {{ service_name }}" - -- name: Create config - ansible.builtin.template: - src: "templates/configuration.yaml.j2" - dest: "{{ service_path }}/data/configuration.yaml" - mode: "0640" - notify: "Recreate {{ service_name }}" - -- name: Create devices and groups - ansible.builtin.copy: - src: "files/{{ item }}" - dest: "{{ service_path }}/data/{{ item }}" - mode: "0400" - loop: - - devices.yaml - - groups.yaml - notify: "Recreate {{ service_name }}" +- name: Create docker compose service + ansible.builtin.include_role: + name: docker_compose_service + vars: + docker_compose_service_name: zigbee2mqtt + docker_compose_service_sub_directories: + - data + docker_compose_service_file: true + docker_compose_service_extra_files: + - data/devices.yaml + - data/groups.yaml + docker_compose_service_extra_templates: + - data/configuration.yaml - name: Create backups with autorestic ansible.builtin.include_role: