- hosts: docker_servers
  name: Switch server
  ignore_unreachable: false

  tasks:
    - name: Reconfigure swap size
      ansible.builtin.lineinfile:
        path: /etc/sysctl.conf
        regexp: "^net.ipv4.igmp_max_memberships =.*"
        line: "net.ipv4.igmp_max_memberships = 1024"
        create: true
      become: "{{ false if inventory_hostname == 'nas.home.lan' else true }}"
      when: inventory_hostname != "router.home.lan" 

    - name: Start containers
      shell: docker start `docker ps -a |awk '{ print $NF }'|grep -v NAME |xargs`
      become: "{{ false if inventory_hostname == 'nas.home.lan' else true }}"
      ignore_errors: true
      when: inventory_hostname == destination and inventory_hostname != "nas.home.lan" 
    - name: Start containers
      shell: docker exec -it gitlab update-permissions
      become: "{{ false if inventory_hostname == 'nas.home.lan' else true }}"
      ignore_errors: true
      when: inventory_hostname == destination and inventory_hostname != "nas.home.lan and inventory_hostname != "rpi5.home.lan" 


    - name: Start containers
      shell: /share/ZFS530_DATA/.qpkg/container-station/bin/docker exec -it gitlab update-permissions
      become: "{{ false if inventory_hostname == 'nas.home.lan' else true }}"
      ignore_errors: true
      when: inventory_hostname == destination and inventory_hostname == "nas.home.lan" 

      
    - name: Start containers
      shell: /share/ZFS530_DATA/.qpkg/container-station/bin/docker start `/share/ZFS530_DATA/.qpkg/container-station/bin/docker ps -a |awk '{ print $NF }'|grep -v NAME |xargs`
      become: "{{ false if inventory_hostname == 'nas.home.lan' else true }}"
      ignore_errors: true
      when: inventory_hostname == destination and inventory_hostname == "nas.home.lan" 

    - name: Get Authentification token
      ansible.builtin.uri:
        url: http://localhost:9380/api/auth
        method: POST
        body_format: json
        body: {"password":"l4c1j4yd33Du5lo"}
      register: login
      when: inventory_hostname != "router.home.lan"
    # - debug:
    #     msg: "{{ login.json.session }}"

    - name: Get Config 
      ansible.builtin.uri:
        url: http://localhost:9380/api/config
        method: GET
        headers:
          X-FTL-SID: "{{ login.json.session.sid  }}"
      register: old_config
      when: inventory_hostname != "router.home.lan"

    # - debug:
    #     msg: "{{ old_config.json.config.dns.cnameRecords  }}"

    - name: Parse config
      ansible.builtin.set_fact:
        jsondata: "{{ old_config }}"   

    - name: New records for nas
      ansible.builtin.set_fact:
        new_data: ["mqtt.home.lan,nas.home.lan","media.home.lan,nas.home.lan","ldap.home.lan,nas.home.lan","webhub.home.lan,nas.home.lan","semaphore.home.lan,nas.home.lan","active.home.lan,nas.home.lan"]
      when: destination  == 'nas.home.lan'

    - name: New records for m-server
      ansible.builtin.set_fact:
        new_data: ["mqtt.home.lan,m-server.home.lan","media.home.lan,m-server.home.lan","ldap.home.lan,m-server.home.lan","webhub.home.lan,m-server.home.lan","semaphore.home.lan,m-server.home.lan","active.home.lan,m-server.home.lan"]
      when: destination  == 'm-server.home.lan'

    - name: New records for rpi5
      ansible.builtin.set_fact:
        new_data: ["mqtt.home.lan,rpi5.home.lan","media.home.lan,rpi5.home.lan","ldap.home.lan,rpi5.home.lan","webhub.home.lan,rpi5.home.lan","semaphore.home.lan,rpi5.home.lan","active.home.lan,rpi5.home.lan"]
      when: destination  == 'rpi5.home.lan'


    # - debug:
    #     msg: "{{ new_data }}"
      
    - name: Set new values
      ansible.utils.update_fact:
        updates:
          - path: jsondata.json.config.dns.cnameRecords 
            value:  "{{ new_data }}"
      register: new_config            
      when: inventory_hostname != "router.home.lan"

    - name: Patch config
      ansible.builtin.uri:
        url: http://localhost:9380/api/config
        method: PATCH
        body: "{{ new_config.jsondata.json |to_json}}" 
        headers:
          X-FTL-SID: "{{ login.json.session.sid  }}"
          Content-Type: application/json
      register: _result
      until: _result.status == 200
      retries: 3 # 720 * 5 seconds = 1hour (60*60/5)
      delay: 5 # Every 5 seconds
      register: _result
      until: _result.status == 200
      retries: 3 # 720 * 5 seconds = 1hour (60*60/5)
      delay: 5 # Every 5 seconds
      when: inventory_hostname != "router.home.lan"



    - name: Sleep for 30 seconds and continue with play
      ansible.builtin.wait_for:
        timeout: 10
    - name: Logout
      ansible.builtin.uri:
        url: http://localhost:9380/api/auth
        method: DELETE
        status_code: 204
        headers:
          X-FTL-SID: "{{ login.json.session.sid  }}"
      when: inventory_hostname != "router.home.lan"
      ignore_errors: true
    - name: Setting up resolv.conf
      ansible.builtin.copy:
        dest: "/etc/resolv.conf"
        content: |
          nameserver 192.168.77.101
          nameserver 192.168.77.106
          nameserver 192.168.77.238
          options rotate
          options timeout:1
      become: "{{ false if inventory_hostname == 'nas.home.lan' else true }}"


      # until: _result.status == 204 
      # retries: 3 # 720 * 5 seconds = 1hour (60*60/5)
      # delay: 5 # Every 5 seconds
    - name: Sleep for 60 seconds and continue with play
      ansible.builtin.wait_for:
        timeout: 60

    - name: Reconfigurte router containers
      shell: python3 /root/unifi-api/unifi.py -s -d "{{ destination.split('.')[0] }}"
      when: inventory_hostname == "router.home.lan"

    - name: Stop containers
      shell: docker stop `docker ps -a |awk '{ print $NF }'|egrep -v "NAME|^pihole$|watchtower|portainer" |xargs`
      become: "{{ false if inventory_hostname == 'nas.home.lan' else true }}"
      ignore_errors: true
      when: inventory_hostname != destination and inventory_hostname != "nas.home.lan" and  inventory_hostname != "router.home.lan"

    - name: Restart containers
      shell: docker restart nginx-app-1
      become: "{{ false if inventory_hostname == 'nas.home.lan' else true }}"
      when: inventory_hostname == destination      

    - name: Stop containers
      shell: /share/ZFS530_DATA/.qpkg/container-station/bin/docker stop `/share/ZFS530_DATA/.qpkg/container-station/bin/docker ps -a |awk '{ print $NF }'|egrep -v "NAME|pihole|watchtower" |xargs`
      become: "{{ false if inventory_hostname == 'nas.home.lan' else true }}"
      ignore_errors: true
      when: inventory_hostname != destination and inventory_hostname == "nas.home.lan" and  inventory_hostname != "router.home.lan"
      
    - name: Sleep for 120 seconds and continue with play
      ansible.builtin.wait_for:
        timeout: 120
    # - name: Restart containers
    #   shell: docker restart nginx-app-1
    #   become: "{{ false if inventory_hostname == 'nas.home.lan' else true }}"
    #   when: inventory_hostname == destination

