---
- name: Kubernetes global vars | Fetch Token
  shell: kubectl describe secret $(kubectl get secrets | grep default | cut -f1 -d ' ') | grep -E '^token' | cut -f2 -d':' | tr -d '\t'
  register: fetch_token
  changed_when: false

- name: Kubernetes global vars | Register Token
  set_fact:
    TOKEN: "{{fetch_token.stdout | trim}}"

- name: Kubernetes global vars | Checking Token
  fail:
    msg: Could not fetch Token, make sure kubectl can run without any errors on the node {{ansible_hostname}}
  when: TOKEN == ""

- name: Kubernetes global vars | Fetch global variables
  blockinfile:
    path: "/tmp/generated_global_vars.yml"
    create: yes
    block: |
      ---
      MASTER_IP: {{host_public_ip}}
      MASTER_INTERNAL_IP_OVN: {{MASTER_INTERNAL_IP}}
      MASTER_INTERNAL_IP: {{host_internal_ip}}
      TOKEN: {{TOKEN}}
      CLUSTER_SUBNET: {{CLUSTER_SUBNET}}
      SERVICE_CLUSTER_IP_RANGE: {{SERVICE_CLUSTER_IP_RANGE}}
      K8S_DNS_DOMAIN: {{K8S_DNS_DOMAIN}}
      K8S_DNS_SERVICE_IP: {{K8S_DNS_SERVICE_IP}}
      K8S_API_SERVICE_IP: {{K8S_API_SERVICE_IP}}
      certs_generated: {{certs_generated}}
  changed_when: false

- name: Kubernetes global vars | Fetch vars
  synchronize:
    mode: pull
    src: /tmp/generated_global_vars.yml
    dest: "{{ansible_tmp_dir}}/generated_global_vars.yml"
    use_ssh_args: yes
