blob: e6d4356eecd30c96a7842dcf794589c2b172a565 [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
50- name: Enable kernel modules on-boot
51 ansible.builtin.template:
52 src: modules-load.conf.j2
53 dest: /etc/modules-load.d/k8s.conf
Mohammed Naser511c3fa2022-03-17 17:54:10 -040054 owner: root
55 group: root
Tadas Sutkaitis4ace4182023-02-27 04:31:52 +020056 mode: "0644"
Mohammed Naser336caf42022-03-11 17:56:45 -050057
58- name: Enable kernel modules in runtime
59 community.general.modprobe:
60 name: "{{ item }}"
61 state: present
62 loop: "{{ kubernetes_kernel_modules }}"
63
64- name: Configure sysctl values
65 ansible.posix.sysctl:
66 name: "{{ item.name }}"
67 value: "{{ item.value }}"
68 state: present
69 loop: "{{ kubernetes_sysctls }}"
70
Mohammed Naserad999a72023-03-02 16:58:19 +010071- name: Disable swap on the host
Mohammed Nasera8950ec2023-03-05 19:28:27 +000072 when: not kubernetes_allow_unsafe_swap | bool
Mohammed Naserad999a72023-03-02 16:58:19 +010073 block:
74 - name: Check swap status
75 ansible.builtin.command: /sbin/swapon -s
76 changed_when: false
77 register: _swapon
Mohammed Naser336caf42022-03-11 17:56:45 -050078
Mohammed Naserad999a72023-03-02 16:58:19 +010079 - name: Disable swap
80 ansible.builtin.command: /sbin/swapoff -a
81 changed_when: true
82 ignore_errors: "{{ ansible_check_mode }}"
83 when:
84 - _swapon.stdout
Mohammed Naser336caf42022-03-11 17:56:45 -050085
Mohammed Naserad999a72023-03-02 16:58:19 +010086 - name: Remove swapfile from /etc/fstab
87 ansible.posix.mount:
88 name: "{{ item }}"
89 fstype: swap
90 state: absent
91 with_items:
92 - swap
93 - none
Mohammed Naser336caf42022-03-11 17:56:45 -050094
Mohammed Naserad999a72023-03-02 16:58:19 +010095 - name: Create noswap systemd service config file
96 ansible.builtin.copy:
97 src: noswap.service
98 dest: /etc/systemd/system/noswap.service
99 owner: root
100 group: root
101 mode: "0644"
102 notify: Enable noswap service
Tadas Sutkaitis4770f552023-02-20 17:41:37 +0200103
Mohammed Naser336caf42022-03-11 17:56:45 -0500104- name: Configure short hostname
105 ansible.builtin.hostname:
106 name: "{{ inventory_hostname_short }}"
107
108- name: Ensure hostname inside hosts file
109 ansible.builtin.lineinfile:
110 path: /etc/hosts
111 regexp: '^127\.0\.1\.1'
112 line: 127.0.1.1 {{ inventory_hostname }} {{ inventory_hostname_short }}
113
114- name: Setup control plane
115 when: inventory_hostname in groups[kubernetes_control_plane_group]
116 ansible.builtin.include_tasks: control-plane.yml
117
118- name: Setup nodes
119 when: inventory_hostname not in groups[kubernetes_control_plane_group]
120 ansible.builtin.include_tasks: nodes.yml
121
122- name: Add labels to control plane nodes
123 delegate_to: "{{ groups[kubernetes_control_plane_group][0] }}"
124 kubernetes.core.k8s:
125 state: patched
126 kind: Node
127 name: "{{ inventory_hostname_short }}"
128 definition:
129 metadata:
Mohammed Nasera98799e2022-05-19 21:54:20 -0400130 labels: "{{ kubernetes_control_plane_labels }}"
Mohammed Naser336caf42022-03-11 17:56:45 -0500131 when:
132 - inventory_hostname in groups['controllers']
133
134- name: Add labels to compute nodes
135 delegate_to: "{{ groups[kubernetes_control_plane_group][0] }}"
136 kubernetes.core.k8s:
137 state: patched
138 kind: Node
139 name: "{{ inventory_hostname_short }}"
140 definition:
141 metadata:
Mohammed Nasera98799e2022-05-19 21:54:20 -0400142 labels: "{{ kubernetes_compute_node_labels }}"
Mohammed Naser336caf42022-03-11 17:56:45 -0500143 when:
144 - inventory_hostname in groups['computes']