---
- hosts: k8s_master 
  become: true
  tasks:
  - name: Install packages that allow apt to be used over HTTPS
    apt:
      name: "{{ packages }}"
      state: present
      update_cache: yes
    vars:
      packages:
      - apt-transport-https
      - ca-certificates
      - curl
      - gnupg-agent
      - software-properties-common

  - name: Add an apt signing key for Docker
    apt_key:
      url: https://download.docker.com/linux/ubuntu/gpg
      state: present

  - name: Add apt repository for stable version
    apt_repository:
      repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable
      state: present

  - name: Install docker and its dependecies
    apt: 
      name: "{{ packages }}"
      state: present
      update_cache: yes
    vars:
      packages:
      - docker-ce 
      - docker-ce-cli 
      - containerd.io
    notify:
      - docker status

  - name: Add ubuntu user to docker group
    user:
      name: ubuntu 
      group: docker

  - name: Remove swapfile from /etc/fstab
    mount:
      name: "{{ item }}"
      fstype: swap
      state: absent
    with_items:
      - swap
      - none

  - name: Disable swap
    command: swapoff -a
    when: ansible_swaptotal_mb > 0

  - name: Add Kubernetes apt key.
    apt_key:
      url: https://packages.cloud.google.com/apt/doc/apt-key.gpg
      state: present
    register: add_repository_key
    #ignore_errors: "{{ kubernetes_apt_ignore_key_error }}"

  - name: Add Kubernetes repository.
    # xenial repo is used for all debian derivates at this time
    apt_repository:
      repo: "deb http://apt.kubernetes.io/ kubernetes-xenial main"
      state: present
      update_cache: true

#  - name: Add an apt signing key for Kubernetes
#    apt_key:
#      url: https://packages.cloud.google.com/apt/doc/apt-key.gpg 
#      state: present
#
#  - name: Adding apt repository for Kubernetes
#    apt_repository:
#      repo: deb https://apt.kubernetes.io/ kubernetes-focal main
#      state: present
#      filename: kubernetes.list

  - name: Install Kubernetes binaries
    apt:
      name: "{{ packages }}"
      state: present
      update_cache: yes
    vars:
      packages:
        - kubelet
        - kubeadm
        - kubectl

  - name: Configure node ip
    lineinfile:
      path: /etc/default/kubelet
      line: KUBELET_EXTRA_ARGS=--node-ip={{ node_ip }}
      create: yes

  - name: Restart kubelet
    service:
      name: kubelet
      daemon_reload: yes
      state: restarted

  - name: Initialize the Kubernetes cluster using kubeadm
    command: kubeadm init --apiserver-advertise-address={{ node_ip }} --apiserver-cert-extra-sans={{ node_ip }}  --node-name {{ vm_hostname }} --pod-network-cidr=192.168.0.0/16

  - name: Setup kubeconfig for ubuntu user
    command: "{{ item }}"
    with_items:
     - mkdir -p /home/ubuntu/.kube
     - cp -i /etc/kubernetes/admin.conf /home/ubuntu/.kube/config
     - chown ubuntu:ubuntu /home/ubuntu/.kube/config

#  - name: Install calico pod network
#    become: false
#    command: kubectl create -f https://docs.projectcalico.org/v3.4/getting-started/kubernetes/installation/hosted/calico.yaml
#
  - name: Install the Tigera Calico operator and custom resource definitions.
    become: false
    command: kubectl create -f https://docs.projectcalico.org/manifests/tigera-operator.yaml

  - name: Install Calico by creating the necessary custom resource.  
    become: false
    command: kubectl create -f https://docs.projectcalico.org/manifests/custom-resources.yaml

  - name: Remove the taints on the master so that you can schedule pods on it. 
    become: false
    command: kubectl taint nodes --all node-role.kubernetes.io/master-


  - name: Generate join command
    command: kubeadm token create --print-join-command
    register: join_command

  - name: Copy join command to local file
    local_action: copy content="{{ join_command.stdout_lines[0] }}" dest="./join-command"

  handlers:
  - name: docker status
    service: name=docker state=started


