- name: Install Kubernetes hosts: kubernetes become: true tasks: - name: Disable swap command: swapoff -a - name: Comment out swap entries in /etc/fstab replace: path: /etc/fstab regexp: '^([^#].*\sswap\s.*)$' replace: '#\1' - name: Remove conflicting packages apt: name: - docker.io - docker-compose - docker-compose-v2 - docker-doc - podman-docker - containerd - runc state: absent purge: true autoremove: true - name: Update and upgrade apt packages apt: update_cache: true upgrade: dist - name: Install prerequisites apt: name: - ca-certificates - curl - apt-transport-https - gpg state: present - name: Create /etc/apt/keyrings directory file: path: /etc/apt/keyrings state: directory mode: '0755' - name: Download Docker GPG key get_url: url: https://download.docker.com/linux/ubuntu/gpg dest: /etc/apt/keyrings/docker.asc mode: '0644' - name: Get Ubuntu codename shell: ". /etc/os-release && echo \"${UBUNTU_CODENAME:-$VERSION_CODENAME}\"" register: ubuntu_codename changed_when: false - name: Add Docker apt repository copy: dest: /etc/apt/sources.list.d/docker.sources content: | Types: deb URIs: https://download.docker.com/linux/ubuntu Suites: {{ ubuntu_codename.stdout }} Components: stable Signed-By: /etc/apt/keyrings/docker.asc - name: Download Kubernetes GPG key shell: > curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.35/deb/Release.key | gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg args: creates: /etc/apt/keyrings/kubernetes-apt-keyring.gpg - name: Add Kubernetes apt repository copy: dest: /etc/apt/sources.list.d/kubernetes.list content: "deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.35/deb/ /\n" - name: Update apt cache apt: update_cache: true - name: Install Docker and Kubernetes packages apt: name: - docker-ce - docker-ce-cli - containerd.io - docker-buildx-plugin - docker-compose-plugin - kubelet - kubeadm - kubectl state: present - name: Hold kubelet, kubeadm, kubectl at current version dpkg_selections: name: "{{ item }}" selection: hold loop: - kubelet - kubeadm - kubectl - name: Generate default containerd config shell: containerd config default > /etc/containerd/config.toml args: creates: /etc/containerd/config.toml - name: Restart containerd systemd: name: containerd state: restarted - name: Enable and start Docker systemd: name: docker enabled: true state: started - name: Enable and start kubelet systemd: name: kubelet enabled: true state: started - name: Run hello-world Docker test command: docker run hello-world changed_when: false