- hosts: containers
  name: Stop mailu
  ignore_unreachable: false
  vars:
      arch_name: docker_ha_data
  tasks:
    - name: Stop HA main
      uri:
        url: "http://192.168.77.106:9000/api/stacks/2/stop?endpointId=2"
        method: POST
        body_format: form-urlencoded
        return_content: yes
        headers:
          Content-Type: "application/json"
          X-API-Key: "ptr_DfS2M6Fj2P3fVvYpkhE0KJh2UGCzY47ePaFaLqadsjg="
        timeout: 60
      register: result
      ignore_errors: yes
      when: inventory_hostname in groups['nas']
    - name: Stop HA rasp
      uri:
        url: "http://192.168.77.106:9000/api/stacks/132/stop?endpointId=13"
        method: POST
        body_format: form-urlencoded
        return_content: yes
        headers:
          Content-Type: "application/json"
          X-API-Key: "ptr_DfS2M6Fj2P3fVvYpkhE0KJh2UGCzY47ePaFaLqadsjg="
        timeout: 60
      register: result
      ignore_errors: yes


      when: inventory_hostname in groups['nas']
    - name: Create archive
      #ansible.builtin.shell: 'tar -czf /share/Data/dockers.tar.gz /share/docker_data/{mailu2,webhub,ha} --exclude="/ha/backups/*" --exclude="home-assistant.log*"'
      ansible.builtin.shell: 'tar -czf /share/Data/{{ arch_name }}-{{ ansible_date_time.date }}.tar.gz /share/docker_data/ha --exclude="/ha/backups/*" --exclude="home-assistant.log*"'
      ignore_errors: yes
      args:
        warn: false
      when: inventory_hostname in groups['nas']

    - name: Start HA main
      uri:
        url: "http://192.168.77.106:9000/api/stacks/2/start?endpointId=2"
        method: POST
        body_format: form-urlencoded
        return_content: yes
        headers:
          Content-Type: "application/json"
          X-API-Key: "ptr_DfS2M6Fj2P3fVvYpkhE0KJh2UGCzY47ePaFaLqadsjg="
        timeout: 60
      register: result
      ignore_errors: yes
      when: inventory_hostname in groups['nas']

    - name: Copy archive
      ansible.builtin.shell: 'scp /share/Data/{{ arch_name }}-{{ ansible_date_time.date }}.tar.gz admin@192.168.77.246:/tmp/'
      when: inventory_hostname in groups['nas']

    #- name: Unpack archive
    #  ansible.builtin.shell: 'tar -xzvf /tmp/dockers-{{ ansible_date_time.date }}.tar.gz -C /'
    #  become: true
    #  when: inventory_hostname in groups['raspberry']
    
    - name: Extract 
      become: yes
      ansible.builtin.unarchive:
        src: /tmp/{{ arch_name }}-{{ ansible_date_time.date }}.tar.gz
        dest: /
        remote_src: yes
      when: inventory_hostname in groups['raspberry']