---

- name: Disable swap permanently
  lineinfile:
    path: /etc/fstab
    regexp: '^\s*UUID=\S+\s+none\s+swap'
    state: absent
  become: true

- name: Disable swap on current session
  become: true
  command:
    cmd: swapoff -a
  when: ansible_swaptotal_mb > 0

- name: Install containerd dependencies
  become: true
  apt:
    name: "{{ k8s_dependencies }}"
    state: present
    update_cache: yes

- name: Ensure the keyring exist
  become: true 
  file:
     mode: '0755'  
     path: "{{ keyring_path }}"
     state: directory
  
- name: Get the  GPG keys
  get_url:
      url: "{{ item.url }}"
      dest: "{{ item.dest }}"
  loop: "{{ gpg_keys }}"

- name: Add  the  GPG keys
  become: true
  apt_key:
     file: "{{ item.dest }}"
     keyring: "{{ item.keyring_files }}"
  loop: "{{ gpg_keys }}"

- name: Set the permissions for key
  file:
     path: "{{ keyring_path }}/docker.gpg"
     state: file
     mode: '0644'


- name: Set up Docker repository
  become: true 
  shell: |
    echo "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" > /etc/apt/sources.list.d/docker.list
    echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.28/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list

- name: Install packages
  become: true
  apt: 
    name: "{{ k8s_packages }}"
    state: present
    update_cache: yes

- name: Configure kernel modules to load on boot for Kubernetes
  become: true
  copy:
    dest: "{{ item }}"
    content: |
      overlay
      br_netfilter
  loop:
    -  /etc/modules-load.d/containerd.conf
    -  /etc/modules-load.d/k8s.conf

- name: Load necessary kernel modules for Kubernetes
  become: true
  modprobe:
    name: "{{ item }}"
    state: present
  loop:
    - overlay
    - br_netfilter

- name: Set sysctl parameters for Kubernetes
  become: true 
  copy:
    dest: /etc/sysctl.d/k8s.conf
    content: |
      net.bridge.bridge-nf-call-iptables = 1
      net.bridge.bridge-nf-call-ip6tables = 1
      net.ipv4.ip_forward = 1
  
- name: Apply sysctl parameters without reboot
  become: true
  command:
    cmd: sysctl --system

- name: Set containerd default config
  become: true
  shell: |
    containerd config default | tee /etc/containerd/config.toml >/dev/null 2>&1

- name: Change systemd to true
  become: true 
  lineinfile:
      path:  /etc/containerd/config.toml
      regexp: '^(\s*)SystemdCgroup'
      line: '\1SystemdCgroup = true'
      backrefs: yes

- name: Restart containerd
  become: true
  ansible.builtin.systemd:
    name: containerd
    state: restarted

- include_tasks: k8s-master.yaml
  when: "'k8smaster' in group_names"

- include_tasks: k8s-nodes.yaml
  when: "'k8snodes' in group_names"

- name: Add worker label on nodes
  command:
      cmd: "kubectl label node {{ item }} node-role.kubernetes.io/worker=worker"
  loop: "{{ groups['k8snodes']  }}"
  when: "'k8smaster' in group_names"

- name: Add the correct IP for the nodes
  become: true
  template:
    dest: /var/lib/kubelet/kubeadm-flags.env
    src:  kubeadm-flags.env.j2
    force: yes
  notify: Restart kubelet