blob: b9d55f56a92e2a83c8f9afac11304240b6bb1cde [file] [log] [blame]
Mohammed Naser336caf42022-03-11 17:56:45 -05001# Copyright (c) 2022 VEXXHOST, Inc.
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14
15- name: Add repository keys
16 ansible.builtin.copy:
17 src: apt-key.gpg
18 dest: /usr/share/keyrings/kubernetes-archive-keyring.gpg
Mohammed Naser511c3fa2022-03-17 17:54:10 -040019 owner: root
20 group: root
Tadas Sutkaitis4ace4182023-02-27 04:31:52 +020021 mode: "0644"
Mohammed Naser336caf42022-03-11 17:56:45 -050022 when:
23 - kubernetes_repo_url == _kubernetes_upstream_apt_repository
24
25- name: Add repository
26 ansible.builtin.apt_repository:
Mohammed Naser511c3fa2022-03-17 17:54:10 -040027 repo:
28 deb
29 {% if kubernetes_repo_url == _kubernetes_upstream_apt_repository %}[signed-by=/usr/share/keyrings/kubernetes-archive-keyring.gpg]{% endif %}
30 {{ kubernetes_repo_url }}
31 kubernetes-xenial
32 main
Mohammed Naser336caf42022-03-11 17:56:45 -050033 state: present
34
35- name: Setup version pins
36 ansible.builtin.template:
37 src: apt-preferences.j2
38 dest: /etc/apt/preferences.d/kubernetes
Tadas Sutkaitis4ace4182023-02-27 04:31:52 +020039 mode: "0644"
Mohammed Naser336caf42022-03-11 17:56:45 -050040
41- name: Install packages
42 ansible.builtin.apt:
43 name:
Mohammed Naserb19a6312023-01-19 02:43:40 +000044 - "cri-tools={{ kubernetes_cri_tools_version }}-00"
Mohammed Naser336caf42022-03-11 17:56:45 -050045 - "kubeadm={{ kubernetes_version }}-00"
46 - "kubectl={{ kubernetes_version }}-00"
47 - "kubelet={{ kubernetes_version }}-00"
48 state: present
49
Tadas Sutkaitis06159252023-02-20 17:15:42 +020050- name: Create crictl config
51 ansible.builtin.template:
52 src: crictl.yaml.j2
53 dest: /etc/crictl.yaml
54 owner: root
Tadas Sutkaitis4ace4182023-02-27 04:31:52 +020055 mode: "0644"
Tadas Sutkaitis06159252023-02-20 17:15:42 +020056
Mohammed Naser336caf42022-03-11 17:56:45 -050057- name: Enable kernel modules on-boot
58 ansible.builtin.template:
59 src: modules-load.conf.j2
60 dest: /etc/modules-load.d/k8s.conf
Mohammed Naser511c3fa2022-03-17 17:54:10 -040061 owner: root
62 group: root
Tadas Sutkaitis4ace4182023-02-27 04:31:52 +020063 mode: "0644"
Mohammed Naser336caf42022-03-11 17:56:45 -050064
65- name: Enable kernel modules in runtime
66 community.general.modprobe:
67 name: "{{ item }}"
68 state: present
69 loop: "{{ kubernetes_kernel_modules }}"
70
71- name: Configure sysctl values
72 ansible.posix.sysctl:
73 name: "{{ item.name }}"
74 value: "{{ item.value }}"
75 state: present
76 loop: "{{ kubernetes_sysctls }}"
77
78- name: Check swap status
79 ansible.builtin.command: /sbin/swapon -s
80 changed_when: false
81 register: _swapon
82
83- name: Disable swap
84 ansible.builtin.command: /sbin/swapoff -a
Tadas Sutkaitis4ace4182023-02-27 04:31:52 +020085 changed_when: true
Mohammed Naser336caf42022-03-11 17:56:45 -050086 ignore_errors: "{{ ansible_check_mode }}"
87 when:
88 - _swapon.stdout
89
90- name: Remove swapfile from /etc/fstab
91 ansible.posix.mount:
92 name: "{{ item }}"
93 fstype: swap
94 state: absent
95 with_items:
96 - swap
97 - none
98
Tadas Sutkaitis4770f552023-02-20 17:41:37 +020099- name: Create noswap systemd service config file
100 ansible.builtin.copy:
101 src: noswap.service
102 dest: /etc/systemd/system/noswap.service
103 owner: root
104 group: root
Tadas Sutkaitis4ace4182023-02-27 04:31:52 +0200105 mode: "0644"
Tadas Sutkaitis4770f552023-02-20 17:41:37 +0200106 notify: Enable noswap service
107
Mohammed Naser336caf42022-03-11 17:56:45 -0500108- name: Configure short hostname
109 ansible.builtin.hostname:
110 name: "{{ inventory_hostname_short }}"
111
112- name: Ensure hostname inside hosts file
113 ansible.builtin.lineinfile:
114 path: /etc/hosts
115 regexp: '^127\.0\.1\.1'
116 line: 127.0.1.1 {{ inventory_hostname }} {{ inventory_hostname_short }}
117
118- name: Setup control plane
119 when: inventory_hostname in groups[kubernetes_control_plane_group]
120 ansible.builtin.include_tasks: control-plane.yml
121
122- name: Setup nodes
123 when: inventory_hostname not in groups[kubernetes_control_plane_group]
124 ansible.builtin.include_tasks: nodes.yml
125
126- name: Add labels to control plane nodes
127 delegate_to: "{{ groups[kubernetes_control_plane_group][0] }}"
128 kubernetes.core.k8s:
129 state: patched
130 kind: Node
131 name: "{{ inventory_hostname_short }}"
132 definition:
133 metadata:
Mohammed Nasera98799e2022-05-19 21:54:20 -0400134 labels: "{{ kubernetes_control_plane_labels }}"
Mohammed Naser336caf42022-03-11 17:56:45 -0500135 when:
136 - inventory_hostname in groups['controllers']
137
138- name: Add labels to compute nodes
139 delegate_to: "{{ groups[kubernetes_control_plane_group][0] }}"
140 kubernetes.core.k8s:
141 state: patched
142 kind: Node
143 name: "{{ inventory_hostname_short }}"
144 definition:
145 metadata:
Mohammed Nasera98799e2022-05-19 21:54:20 -0400146 labels: "{{ kubernetes_compute_node_labels }}"
Mohammed Naser336caf42022-03-11 17:56:45 -0500147 when:
148 - inventory_hostname in groups['computes']