blob: aa74444755c22f2ed88566c7371b3d6ca8a5a489 [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
Mohammed Naserad999a72023-03-02 16:58:19 +010078- name: Disable swap on the host
Mohammed Nasera8950ec2023-03-05 19:28:27 +000079 when: not kubernetes_allow_unsafe_swap | bool
Mohammed Naserad999a72023-03-02 16:58:19 +010080 block:
81 - name: Check swap status
82 ansible.builtin.command: /sbin/swapon -s
83 changed_when: false
84 register: _swapon
Mohammed Naser336caf42022-03-11 17:56:45 -050085
Mohammed Naserad999a72023-03-02 16:58:19 +010086 - name: Disable swap
87 ansible.builtin.command: /sbin/swapoff -a
88 changed_when: true
89 ignore_errors: "{{ ansible_check_mode }}"
90 when:
91 - _swapon.stdout
Mohammed Naser336caf42022-03-11 17:56:45 -050092
Mohammed Naserad999a72023-03-02 16:58:19 +010093 - name: Remove swapfile from /etc/fstab
94 ansible.posix.mount:
95 name: "{{ item }}"
96 fstype: swap
97 state: absent
98 with_items:
99 - swap
100 - none
Mohammed Naser336caf42022-03-11 17:56:45 -0500101
Mohammed Naserad999a72023-03-02 16:58:19 +0100102 - name: Create noswap systemd service config file
103 ansible.builtin.copy:
104 src: noswap.service
105 dest: /etc/systemd/system/noswap.service
106 owner: root
107 group: root
108 mode: "0644"
109 notify: Enable noswap service
Tadas Sutkaitis4770f552023-02-20 17:41:37 +0200110
Mohammed Naser336caf42022-03-11 17:56:45 -0500111- name: Configure short hostname
112 ansible.builtin.hostname:
113 name: "{{ inventory_hostname_short }}"
114
115- name: Ensure hostname inside hosts file
116 ansible.builtin.lineinfile:
117 path: /etc/hosts
118 regexp: '^127\.0\.1\.1'
119 line: 127.0.1.1 {{ inventory_hostname }} {{ inventory_hostname_short }}
120
121- name: Setup control plane
122 when: inventory_hostname in groups[kubernetes_control_plane_group]
123 ansible.builtin.include_tasks: control-plane.yml
124
125- name: Setup nodes
126 when: inventory_hostname not in groups[kubernetes_control_plane_group]
127 ansible.builtin.include_tasks: nodes.yml
128
129- name: Add labels to control plane nodes
130 delegate_to: "{{ groups[kubernetes_control_plane_group][0] }}"
131 kubernetes.core.k8s:
132 state: patched
133 kind: Node
134 name: "{{ inventory_hostname_short }}"
135 definition:
136 metadata:
Mohammed Nasera98799e2022-05-19 21:54:20 -0400137 labels: "{{ kubernetes_control_plane_labels }}"
Mohammed Naser336caf42022-03-11 17:56:45 -0500138 when:
139 - inventory_hostname in groups['controllers']
140
141- name: Add labels to compute nodes
142 delegate_to: "{{ groups[kubernetes_control_plane_group][0] }}"
143 kubernetes.core.k8s:
144 state: patched
145 kind: Node
146 name: "{{ inventory_hostname_short }}"
147 definition:
148 metadata:
Mohammed Nasera98799e2022-05-19 21:54:20 -0400149 labels: "{{ kubernetes_compute_node_labels }}"
Mohammed Naser336caf42022-03-11 17:56:45 -0500150 when:
151 - inventory_hostname in groups['computes']