- hosts: odroid_master
  name: Install k3s
  become: true
  gather_facts: no
  tasks:
    - name: Install K3S
      ansible.builtin.shell: 'curl -sfL https://get.k3s.io | K3S_KUBECONFIG_MODE="644" INSTALL_K3S_VERSION="v1.24.1+k3s1" sh -s -'
      #changed_when: false
    - name: Get token
      ansible.builtin.command: 'cat /var/lib/rancher/k3s/server/node-token'
      register: k3s_token
      changed_when: false
    - name: Print token
      ansible.builtin.debug: var=k3s_token.stdout
    - name: "Add K8S Token and Hash to dummy host"
      ansible.builtin.add_host:
        name: "K3S_TOKEN_HOLDER"
        token: "{{ k3s_token.stdout }}"
    - name: Print token
      ansible.builtin.debug:
        msg: "[Master] K3S_TOKEN_HOLDER K8S token is {{ hostvars['K3S_TOKEN_HOLDER']['token'] }}"
    - name: Grant cluster role
      ansible.builtin.command: kubectl create clusterrolebinding cluster-admin-binding --clusterrole cluster-admin --user root
      changed_when: false
      ignore_errors: yes

    - name: Reboot the server
      tags: reboot
      become: yes
      become_user: root
      shell: "sleep 5 && reboot"
      async: 1
      poll: 0
  
    - name: (reboot) Wait for server to restart
      wait_for_connection:
        delay: 75
    - name: Wait for service 'k3s.service' to be running
      service_facts:
      register: result
      until: result.ansible_facts.services['k3s.service'].state == 'running'
      retries: 10
      delay: 5
#create cluster:
#curl -u "token-wg66j:p26nwqs9ckqbth4h8gtfd2g7858g8smbtpgmxq9qr52w4795l5jvv9" -X POST -H 'Accept: application/json' -H 'Content-Type: application/json' -d '{"name":"test-cluster"}' 'https://192.168.77.106:8543/v3/clusters'  --insecure
#get registration token
# curl -u "token-wg66j:p26nwqs9ckqbth4h8gtfd2g7858g8smbtpgmxq9qr52w4795l5jvv9" -X GET -H 'Accept: application/json' -H 'Content-Type: application/json' https://192.168.77.106:8543/v3/clusters/c-xq5pv/clusterRegistrationTokens  --insecure|jq
    - name: Create new cluster in rancher
      ansible.builtin.shell: "curl -u '{{ rancher_api_key }}' -X POST -H 'Accept: application/json' -H 'Content-Type: application/json' -d '{\"name\":\"odroid\"}' 'https://192.168.77.106:8543/v3/clusters'  --insecure| jq .id"
      changed_when: false
      ignore_errors: true
      register: cluster_id

    - name: Just the Names
      ansible.builtin.debug:
        msg: "{{ cluster_id.stdout }}"
    - name: Create new cluster in rancher
      ansible.builtin.shell: "curl -u '{{ rancher_api_key }}' -X GET -H 'Accept: application/json' -H 'Content-Type: application/json' https://192.168.77.106:8543/v3/clusters/{{ cluster_id.stdout }}/clusterRegistrationTokens  --insecure| jq .data[0].manifestUrl"
      changed_when: false
      ignore_errors: true
      register: register_id
    - name: Register Token
      ansible.builtin.debug:
        msg: "{{ register_id.stdout }}"
    - name: "Register in Rancher"
      ansible.builtin.shell: "curl --insecure -sfL {{ register_id.stdout }} |sed 's/rancher-agent:v2.6.7/rancher-agent:v2.6.7-linux-arm64/g'| sed 's%beta.kubernetes.io/os%kubernetes.io/os%g'| kubectl apply -f -"
      changed_when: false
      ignore_errors: true
    # - name: "Register in Rancher"
    #   ansible.builtin.shell: curl --insecure -sfL https://192.168.77.106:8543/v3/import/f5679ncdtx68489nqj2rntk8bwv6lcsq4j5qdtnxswkbfbtttn5s5q_c-lq4j4.yaml |sed 's/rancher-agent:v2.6.7/rancher-agent:v2.6.7-linux-arm64/g'| sed 's%beta.kubernetes.io/os%kubernetes.io/os%g'| kubectl apply -f -
    #   changed_when: false
    #   ignore_errors: true
    - name: wait for pods to come up
      shell: kubectl get pods -o json -n cattle-system
      register: kubectl_get_pods
      until: kubectl_get_pods.stdout|from_json|json_query('items[*].status.phase')|unique == ["Running"]
      retries: 20
      delay: 30