Files
piKube/roles/common/tasks/main.yml

175 lines
4.1 KiB
YAML

---
- name: Wait for APT lock
shell: |
while fuser /var/lib/dpkg/lock-frontend >/dev/null 2>&1; do
echo "Waiting for other software managers to finish..."
sleep 5
done
register: apt_lock_wait
changed_when: false
- name: Update apt cache
apt:
update_cache: yes
cache_valid_time: 3600
- name: Upgrade all packages
apt:
upgrade: dist
- name: Check if cloud-init exists
stat:
path: /etc/cloud/cloud.cfg
register: cloud_init_cfg
- name: Prevent cloud-init from changing hostname
copy:
dest: /etc/cloud/cloud.cfg.d/99-preserve-hostname.cfg
content: "preserve_hostname: true"
when: cloud_init_cfg.stat.exists
- name: Set hostname
command: "hostnamectl set-hostname {{ inventory_hostname }}"
- name: Update /etc/hosts with inventory hostname
lineinfile:
path: /etc/hosts
regexp: '^127\.0\.1\.1'
line: "127.0.1.1 {{ inventory_hostname }}"
- name: Install required system packages
apt:
name:
- apt-transport-https
- ca-certificates
- curl
- software-properties-common
- gnupg
state: present
- name: Disable swap
command: swapoff -a
when: ansible_swaptotal_mb > 0
- name: Disable swap in /etc/fstab
replace:
path: /etc/fstab
regexp: '^([^#].*?\sswap\s+.*)$'
replace: '# \1'
- name: Stop and disable dphys-swapfile service (if exists)
service:
name: dphys-swapfile
state: stopped
enabled: no
failed_when: false
when: ansible_distribution == 'Raspbian'
- name: Determine cmdline.txt location
stat:
path: /boot/firmware/cmdline.txt
register: cmdline_firmware
- name: Set cmdline path
set_fact:
cmdline_path: "{{ '/boot/firmware/cmdline.txt' if cmdline_firmware.stat.exists else '/boot/cmdline.txt' }}"
- name: Enable cgroup features in cmdline.txt
replace:
path: "{{ cmdline_path }}"
regexp: '^((?!.*\bcgroup_enable=cpuset cgroup_enable=memory cgroup_memory=1\b).*)$'
replace: '\1 cgroup_enable=cpuset cgroup_enable=memory cgroup_memory=1'
register: cgroup_update
- name: Reboot if cgroup features updated
reboot:
when: cgroup_update.changed
- name: Load kernel modules for containerd
copy:
dest: /etc/modules-load.d/containerd.conf
content: |
overlay
br_netfilter
- name: Load overlay module
modprobe:
name: overlay
state: present
- name: Load br_netfilter module
modprobe:
name: br_netfilter
state: present
- name: Configure sysctl params for Kubernetes
copy:
dest: /etc/sysctl.d/99-kubernetes-cri.conf
content: |
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
register: sysctl_config
- name: Apply sysctl params
command: sysctl --system
when: sysctl_config.changed
- name: Install containerd
apt:
name: containerd
state: present
- name: Create containerd config directory
file:
path: /etc/containerd
state: directory
- name: Generate default containerd config
shell: containerd config default > /etc/containerd/config.toml
args:
creates: /etc/containerd/config.toml
- name: Configure SystemdCgroup in containerd config
replace:
path: /etc/containerd/config.toml
regexp: 'SystemdCgroup = false'
replace: 'SystemdCgroup = true'
notify: restart containerd
- name: Create keyrings directory
file:
path: /etc/apt/keyrings
state: directory
mode: '0755'
- name: Download Kubernetes GPG key
get_url:
url: "https://pkgs.k8s.io/core:/stable:/v{{ k8s_version | regex_search('^[0-9]+\\.[0-9]+') }}/deb/Release.key"
dest: /etc/apt/keyrings/kubernetes-apt-keyring.asc
mode: '0644'
- name: Add Kubernetes apt repository
apt_repository:
repo: "deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.asc] https://pkgs.k8s.io/core:/stable:/v{{ k8s_version | regex_search('^[0-9]+\\.[0-9]+') }}/deb/ /"
state: present
filename: kubernetes
- name: Install Kubernetes binaries
apt:
name:
- kubelet
- kubeadm
- kubectl
state: present
update_cache: yes
- name: Hold Kubernetes packages
dpkg_selections:
name: "{{ item }}"
selection: hold
loop:
- kubelet
- kubeadm
- kubectl