From 82cccc01d9514237dd18a6953eb197ec1805b0c0 Mon Sep 17 00:00:00 2001 From: Cian Hatton Date: Thu, 8 Sep 2022 20:38:46 +0100 Subject: [PATCH] only create files on hosts that need them --- group_vars/servers.yml | 14 +---- host_vars/qnap.yml | 22 +++++++- roles/deploy_portainer_stack/tasks/main.yml | 52 +++++++++---------- roles/setup_hosted_services/tasks/main.yml | 40 ++++++-------- .../templates/olivetin.j2 | 3 +- 5 files changed, 66 insertions(+), 65 deletions(-) diff --git a/group_vars/servers.yml b/group_vars/servers.yml index c6f8119..87c3b6e 100644 --- a/group_vars/servers.yml +++ b/group_vars/servers.yml @@ -3,21 +3,8 @@ backups: monthly: monthly nightly: nightly -# dashy related config -dashy: - source_file: dashboards/dashy-config.yml - config_file: dashy-config.yml - config_directory: /etc/config/dashy - -# olivetin related config -olivetin: - source_file: olivetin/config.yml - config_file: config.yml - config_directory: /etc/config/OliveTin - homelab_user: cianhatton - configure_mergerfs: false configure_samba: false samba_group: smbgroup @@ -56,3 +43,4 @@ portainer_endpoint: -1 portainer_host: false portainer_base_url: "http://qnap:9000" external_docker_networks: [] +portainer_required_files: [] diff --git a/host_vars/qnap.yml b/host_vars/qnap.yml index 51bd448..0fd7994 100644 --- a/host_vars/qnap.yml +++ b/host_vars/qnap.yml @@ -34,8 +34,28 @@ external_docker_networks: ansible_pull_path: /home/{{ homelab_user }}/.local/bin/ansible-pull -portainer_endpoint: 2 +dashy: + source_file: dashboards/dashy-config.yml + config_file: dashy-config.yml + config_directory: /etc/config/dashy + +# olivetin related config +olivetin: + source_file: olivetin/config.yml + config_file: config.yml + config_directory: /etc/config/OliveTin +portainer_required_files: + - source_file: dashboards/dashy-config.yml + dest_file_name: dashy-config.yml + dest_directory: /etc/config/dashy + handler: restart-dashy + - source_file: olivetin/config.yml + dest_file_name: config.yml + dest_directory: /etc/config/OliveTin + handler: restart-olivetin + +portainer_endpoint: 2 services: - name: gitea template_vars: diff --git a/roles/deploy_portainer_stack/tasks/main.yml b/roles/deploy_portainer_stack/tasks/main.yml index ae805f3..03ad358 100644 --- a/roles/deploy_portainer_stack/tasks/main.yml +++ b/roles/deploy_portainer_stack/tasks/main.yml @@ -22,35 +22,35 @@ mode: '0755' with_items: "{{ portainer_stack_directories }}" -#- name: "Stack {{ portainer_stack_name }} | Find docker volumes" -# ansible.builtin.shell: docker volume ls -f name={{ portainer_stack_name }} --format '{{ '{{' }} .Name {{ '}}' }}' -# register: find_volumes -# changed_when: false +- name: "Stack {{ portainer_stack_name }} | Find docker volumes" + ansible.builtin.shell: docker volume ls -f name={{ portainer_stack_name }} --format '{{ '{{' }} .Name {{ '}}' }}' + register: find_volumes + changed_when: false -#- name: "Stack {{ portainer_stack_name }} | Find docker volumes that need to be restored" -# ansible.builtin.script: scripts/find-volumes-to-restore.py -# environment: -# EXISTING_VOLUMES: "{{ find_volumes.stdout_lines }}" -# STACK_NAME: '{{ portainer_stack_name }}' -# DOCKER_COMPOSE_DIR: '{{ directories.docker_compose_directory }}' -# args: -# executable: python3 -# register: python_output -# changed_when: false +- name: "Stack {{ portainer_stack_name }} | Find docker volumes that need to be restored" + ansible.builtin.script: scripts/find-volumes-to-restore.py + environment: + EXISTING_VOLUMES: "{{ find_volumes.stdout_lines }}" + STACK_NAME: '{{ portainer_stack_name }}' + DOCKER_COMPOSE_DIR: '{{ directories.docker_compose_directory }}' + args: + executable: python3 + register: python_output + changed_when: false -#- name: "Stack {{ portainer_stack_name }} | Build list of volumes to restore." -# ansible.builtin.set_fact: -# restore_volumes: "{{ restore_volumes | default([]) + [{'volume_name':item}] }}" -# with_items: "{{ python_output.stdout_lines | list }}" +- name: "Stack {{ portainer_stack_name }} | Build list of volumes to restore." + ansible.builtin.set_fact: + restore_volumes: "{{ restore_volumes | default([]) + [{'volume_name':item}] }}" + with_items: "{{ python_output.stdout_lines | list }}" -#- name: "Stack {{ portainer_stack_name }} | Restore any missing volumes from S3" -# ansible.builtin.include_role: -# name: chatton.docker_backup.docker_s3_volume_restore -# when: restore_volumes is defined -# vars: -# docker_backup_restore_force: false -# docker_backup_restore_latest_s3_key: true -# docker_backup_s3_restores: "{{ restore_volumes }}" +- name: "Stack {{ portainer_stack_name }} | Restore any missing volumes from S3" + ansible.builtin.include_role: + name: chatton.docker_backup.docker_s3_volume_restore + when: restore_volumes is defined + vars: + docker_backup_restore_force: false + docker_backup_restore_latest_s3_key: true + docker_backup_s3_restores: "{{ restore_volumes }}" - name: "Stack {{ portainer_stack_name }} | Update Portainer." chatton.portainer.portainer_stack: diff --git a/roles/setup_hosted_services/tasks/main.yml b/roles/setup_hosted_services/tasks/main.yml index 546a818..e763069 100644 --- a/roles/setup_hosted_services/tasks/main.yml +++ b/roles/setup_hosted_services/tasks/main.yml @@ -12,36 +12,26 @@ - ubuntu - busybox -- name: Directories | Ensure required directories +- name: Create required directories. ansible.builtin.file: - path: '{{ item }}' + path: '{{ file_item.dest_directory }}' state: directory mode: '0755' - with_items: - - '{{ dashy.config_directory }}' - - '{{ olivetin.config_directory }}' - - '{{ directories.backups_dir }}/.s3tmp' - -- name: Dashy | Config Files - ansible.builtin.copy: - src: '{{ dashy.source_file }}' - dest: '{{ dashy.config_directory }}/{{ dashy.config_file }}' - owner: root - group: root - mode: 0440 - ignore_errors: true - notify: - - restart-dashy + with_items: "{{ portainer_required_files }}" + loop_control: + loop_var: file_item -- name: Olivetin | Config Files +- name: Create required files. ansible.builtin.copy: - src: '{{ olivetin.source_file }}' - dest: '{{ olivetin.config_directory }}/{{ olivetin.config_file }}' + src: "{{ file_item.source_file }}" + dest: '{{ file_item.dest_directory }}/{{ file_item.dest_file_name }}' owner: root group: root mode: 0440 - notify: - - restart-olivetin + with_items: "{{ portainer_required_files }}" + loop_control: + loop_var: file_item + notify: "{{ file_item.handler }}" - name: Create external docker networks. docker_network: @@ -52,10 +42,12 @@ ansible.builtin.include_role: name: deploy_portainer_stack vars: - portainer_stack_name: "{{ item.name }}" + portainer_stack_name: "{{ portainer_stack.name }}" portainer_stack_endpoint_id: "{{ portainer_endpoint }}" - portainer_stack_template_vars: "{{ item.template_vars | default({}) }}" + portainer_stack_template_vars: "{{ portainer_stack.template_vars | default({}) }}" with_items: "{{ services }}" + loop_control: + loop_var: portainer_stack - name: Install Ansible pull tags: ["cron"] diff --git a/roles/setup_hosted_services/templates/olivetin.j2 b/roles/setup_hosted_services/templates/olivetin.j2 index 414c310..ee9cc29 100644 --- a/roles/setup_hosted_services/templates/olivetin.j2 +++ b/roles/setup_hosted_services/templates/olivetin.j2 @@ -1,4 +1,5 @@ --- + version: "3.8" services: olivetin: @@ -6,7 +7,7 @@ services: image: jamesread/olivetin user: root volumes: - - {{olivetin.config_directory}}/{{olivetin.config_file}}:/config/config.yaml + - /etc/config/OliveTin/config.yml:/config/config.yaml - /var/run/docker.sock:/var/run/docker.sock ports: - "1337:1337"