---
- name: import a task
  hosts: containers
  gather_facts: false
  # vars:
  #   selected_containers: selected_containers|split(",")
  tasks:
    # - debug:
    #     msg: "{{ inventory_hostname }}"
    # - debug:
    #     msg: "{{ destination_server }}"
    - name: Install sqlite3
      ansible.builtin.apt:
        name: 
          - sqlite3
        state: present
        update_cache: yes
      become: true
      
      when: inventory_hostname == destination_server

    - include_tasks: stop_containers.yaml
      name: Stop Containers
      when: inventory_hostname == destination_server or inventory_hostname == source_server
      
    - name: Pause for 60 seconds
      ansible.builtin.pause:
        seconds: 60
        
    - include_tasks: sync_container_data.yaml
      name: Sync Container Data
      when: inventory_hostname == source_server

    - include_tasks: reconfigure_nginx.yaml
      name: Reconfigure nginx proxy manager
      loop:
        - sectorq.eu
        - gitlab.sectorq.eu
        - ha.sectorq.eu
        - mail.sectorq.eu
        - pw.sectorq.eu
        - semaphore.sectorq.eu
        - kestra.sectorq.eu
        - auth.sectorq.eu
      when: inventory_hostname == destination_server

    - name: Get relevant configs
      ansible.builtin.shell: 'egrep -l  "# kestra.sectorq.eu|# auth.sectorq.eu|# ha.sectorq.eu|# pw.sectorq.eu|# semaphore.sectorq.eu|# sectorq.eu|# gitlab.sectorq.eu|# ha.sectorq.eu" /share/docker_data/nginx/data/nginx/proxy_host/*'
      ignore_errors: true
      become: true
      register: result
      when: inventory_hostname == destination_server

    - debug:
        msg: "{{ result.stdout_lines  }}"
      when: inventory_hostname == destination_server
    - name: Replace ip
      ansible.builtin.lineinfile:
        path: "{{ item }}"
        regexp: '^\s+set \$server\s+\"\w+.\w+.\w+.\w+\";'
        line: "  set $server         \"192.168.77.47\";"
      become: true
      with_items:
        - "{{ result.stdout_lines }}"
      when: inventory_hostname == destination_server

    - include_tasks: reconfigure_heimdall.yaml
      name: Reconfigure heimdall
      loop:
        - Home Assistant
        - Nginx Proxy Manager
        - Portainer
        - Roundcube
        - Authentik
        - Kestra
      when: inventory_hostname == destination_server
    - name: Changing heimdall background
      ansible.builtin.shell: sqlite3 /share/docker_data/heimdall/config/www/app.sqlite  "UPDATE setting_user SET uservalue = 'backgrounds/TRN2Ydr5dyVAkWvCq4xqR5bQ6iyv5XaKvM1r84sJ.jpg' WHERE user_id = (SELECT id FROM users WHERE username = 'jaydee')"
      become: true
      when: inventory_hostname == destination_server
    - include_tasks: start_containers.yaml
      name: Start Containers
      when: inventory_hostname == destination_server or inventory_hostname == source_server
    - name: Pause for 60 seconds
      ansible.builtin.pause:
        seconds: 60
    - name: Update gitlab perms
      ansible.builtin.shell: 'docker exec -t gitlab update-permissions'
      ignore_errors: true
      become: true
      register: result
      when: inventory_hostname == destination_server 
    # - name: Get relevant configs
    #   ansible.builtin.shell: 'docker restart gitlab'
    #   ignore_errors: yes
    #   become: yes
    #   register: result
    #   when: inventory_hostname in groups['raspberrypi5']