---
- name: Update and upgrade apt packages
  apt:
    upgrade: yes
    update_cache: yes
    force_apt_get: yes
    cache_valid_time: 86400

- name: Install apt packages
  apt:
    name: ['cloud-utils', 'apt-transport-https', 'ca-certificates', 'curl', 'socat', 'conntrack', 'gnupg', 'lsb-release', 'bash-completion', 'chrony']
    state: present

- name: Disable ufw 
  command: 'ufw disable'
  when: ansible_distribution_version == '20.04'

- name: Disable SWAP since kubernetes can't work with swap enabled (1/2)
  command: 'swapoff -a'

- name: Disable SWAP in fstab since kubernetes can't work with swap enabled (2/2)
  replace:
    path: /etc/fstab
    regexp: '^([^#].*?\sswap\s+sw\s+.*)$'
    replace: '# \1'

- name: Add br_netfilter to module autoload
  lineinfile:
    path: /etc/modules-load.d/k8s.conf
    line: "{{ item }}"
    create: true
  with_items:
    - 'overlay'
    - 'br_netfilter'

- name: Add br_netfilter to module autoload
  modprobe:
    name: "{{ item }}"
    state: present
  become: true
  with_items:
    - 'overlay'
    - 'br_netfilter'

- name: Add br_netfilter to module autoload
  lineinfile:
    path: /etc/sysctl.d/k8s.conf
    line: "{{ item }}"
    create: true
  with_items:
    - 'net.bridge.bridge-nf-call-iptables  = 1'
    - 'net.bridge.bridge-nf-call-ip6tables = 1'
    - 'net.ipv4.ip_forward                 = 1'

- name: Disable net.bridge.bridge-nf-call-iptables
  sysctl:
    name: "{{ item }}"
    value: 1
  with_items:
    - 'net.bridge.bridge-nf-call-iptables'
    - 'net.bridge.bridge-nf-call-ip6tables'

- name: Disable net.ipv4.ip_forward
  sysctl:
    name: net.ipv4.ip_forward
    value: "1"

- name: Setting hosts file
  template:
    src: hosts.j2
    dest: /etc/hosts
