Playbook #1

/root/kubeinit/ci/builds/6mbKNrxD/0/kubeinit/kubeinit/kubeinit-aux/kubeinit/playbook.yml

Report Status CLI Date Duration Controller User Versions Hosts Plays Tasks Results Files Records
31 Oct 2023 08:44:29 +0000 00:45:59.97 nyctea root Ansible 2.15.2 ara 1.6.1 (client), 1.6.1 (server) Python 3.11.4 9 7 1091 1091 53 1

File: /root/.ansible/collections/ansible_collections/kubeinit/kubeinit/roles/kubeinit_k8s/tasks/main.yml

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
---
# Copyright kubeinit contributors
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

- name: Deploy the cluster nodes
  ansible.builtin.include_role:
    name: kubeinit.kubeinit.kubeinit_libvirt
    tasks_from: "deploy_{{ hostvars[kubeinit_deployment_node_name].os }}_guest.yml"
    public: yes
  loop: "{{ groups['all_cluster_nodes'] }}"
  loop_control:
    loop_var: kubeinit_deployment_node_name
  vars:
    kubeinit_deployment_delegate: "{{ hostvars[kubeinit_deployment_node_name].target }}"
  when: kubeinit_cluster_nodes_deployed is not defined or not kubeinit_cluster_nodes_deployed

- name: Setup the first controller node
  block:

    - name: Clean kubeadm and initialize Kubernetes cluster
      ansible.builtin.shell: |
        set -eo pipefail
        kubeadm reset -f || true
        kubeadm init \
            --control-plane-endpoint "api.{{ kubeinit_cluster_fqdn }}:6443" \
            --upload-certs \
            --pod-network-cidr={{ kubeinit_k8s_pod_network_cidr }}
      args:
        executable: /bin/bash
      register: _result_kubeadm_init_output
      changed_when: "_result_kubeadm_init_output.rc == 0"

    - name: Debug
      ansible.builtin.debug:
        var: _result_kubeadm_init_output

    - name: Get the controller join key
      ansible.builtin.set_fact:
        k8s_controller_join_key: "{{ item }}"
      loop: "{{ _result_kubeadm_init_output.stdout_lines }}"
      when: ('--control-plane --certificate-key' in item)

    - name: Debug
      ansible.builtin.debug:
        var: k8s_controller_join_key

    - name: Storing the controller join key to add the other controller nodes.
      ansible.builtin.copy:
        content: "{{ k8s_controller_join_key }}"
        dest: ~/k8s_controller_join_key
        mode: '0644'
      when: kubeinit_controller_count|int > 1

    - name: Create kube directory
      ansible.builtin.file:
        path: ~/.kube
        state: directory
        mode: '0644'

    - name: Copying required files
      ansible.builtin.shell: |
        cp -f /etc/kubernetes/admin.conf ~/.kube/config
        chown $(id -u):$(id -g) ~/.kube/config
      args:
        executable: /bin/bash
      register: _result
      changed_when: "_result.rc == 0"

    - name: Get the join command
      ansible.builtin.command: kubeadm token create --print-join-command
      register: _result_join_command
      changed_when: "_result_join_command.rc == 0"

    - name: Storing the join cluster command token.
      ansible.builtin.copy:
        content: "{{ _result_join_command.stdout }}"
        dest: ~/k8s_token
        mode: '0644'
      when: kubeinit_controller_count|int > 1

    - name: Render the flannel template
      ansible.builtin.template:
        src: "kube-flannel.yml.j2"
        dest: "~/kube-flannel.yml"
        mode: "0644"

    - name: Install the Flannel network Add-on
      ansible.builtin.command: kubectl apply -f ~/kube-flannel.yml
      register: _result
      changed_when: "_result.rc == 0"

  delegate_to: "{{ kubeinit_first_controller_node }}"

##
## Execute to configure all the other controller nodes
##

- name: Join additional controller nodes using the PK
  ansible.builtin.shell: |
    kubeadm reset -f || true
    echo "{{ _result_join_command.stdout }} {{ k8s_controller_join_key }}" > ~/k8s_controller_join_command.sh
    sh ~/k8s_controller_join_command.sh
  args:
    executable: /bin/bash
  register: _result
  changed_when: "_result.rc == 0"
  loop: "{{ groups['all_controller_nodes'] }}"
  loop_control:
    loop_var: controller_node
  delegate_to: "{{ controller_node }}"
  when: kubeinit_controller_count|int > 1 and controller_node not in kubeinit_first_controller_node

- name: Create kube directory
  ansible.builtin.file:
    path: ~/.kube
    state: directory
    mode: '0777'
  loop: "{{ groups['all_controller_nodes'] }}"
  loop_control:
    loop_var: controller_node
  delegate_to: "{{ controller_node }}"
  when: kubeinit_controller_count|int > 1 and controller_node not in kubeinit_first_controller_node

- name: Copying required files
  ansible.builtin.shell: |
    cp -f /etc/kubernetes/admin.conf ~/.kube/config
    chown $(id -u):$(id -g) ~/.kube/config
  args:
    executable: /bin/bash
  register: _result
  changed_when: "_result.rc == 0"
  loop: "{{ groups['all_controller_nodes'] }}"
  loop_control:
    loop_var: controller_node
  delegate_to: "{{ controller_node }}"
  when: kubeinit_controller_count|int > 1 and controller_node not in kubeinit_first_controller_node

- name: Join compute nodes with kubernetes control plane
  ansible.builtin.shell: |
    kubeadm reset -f || true
    echo "{{ _result_join_command.stdout }}" > ~/k8s_compute_join_command.sh
    sh ~/k8s_compute_join_command.sh
  args:
    executable: /bin/bash
  register: _result
  changed_when: "_result.rc == 0"
  loop: "{{ groups['all_compute_nodes'] | default([]) }}"
  loop_control:
    loop_var: compute_node
  delegate_to: "{{ compute_node }}"
  when: hostvars[compute_node].os != 'windows'

# The kubeconfig file is on the controller nodes so we run kubectl label on the first controller
- name: Label node
  ansible.builtin.command: |
    kubectl label node {{ hostvars[compute_node].fqdn }} node-role.kubernetes.io/worker=
  register: _result
  changed_when: "_result.rc == 0"
  loop: "{{ groups['all_compute_nodes'] | default([]) }}"
  loop_control:
    loop_var: compute_node
  delegate_to: "{{ kubeinit_first_controller_node }}"
  when: hostvars[compute_node].os != 'windows'

- name: Check if there is at least one compute Windows node
  ansible.builtin.set_fact:
    windows_compute_node_exists: true
  loop: "{{ groups['all_compute_nodes'] | default([]) }}"
  loop_control:
    loop_var: compute_node
  when: hostvars[compute_node].os == 'windows'

- name: Allow schedule workloads in controller nodes if there are no compute nodes
  ansible.builtin.shell: |
    set -o pipefail
    dnf install -y jq
    # Deprecated in 1.24
    for node in $(kubectl get nodes -o json | jq -r '.items[] | select(.spec.taints[]?.key=="node-role.kubernetes.io/master") | .metadata.labels."kubernetes.io/hostname"');
    do
      kubectl taint node ${node} node-role.kubernetes.io/master:NoSchedule-
    done
    # Working starting on 1.24
    for node in $(kubectl get nodes -o json | jq -r '.items[] | select(.spec.taints[]?.key=="node-role.kubernetes.io/control-plane") | .metadata.labels."kubernetes.io/hostname"');
    do
      kubectl taint node ${node} node-role.kubernetes.io/control-plane:NoSchedule-
    done
  args:
    executable: /bin/bash
  register: _result
  changed_when: "_result.rc == 0"
  when: not kubeinit_compute_count|int > 0 or (windows_compute_node_exists | default(false))
  delegate_to: "{{ kubeinit_first_controller_node }}"

# We fetch the kubeconfig from the first controller node
- name: Copying the kubeconfig to a variable
  ansible.builtin.slurp:
    src: ~/.kube/config
  register: _result_cluster_kubeconfig
  delegate_to: "{{ kubeinit_first_controller_node }}"

- name: Create kube directory
  ansible.builtin.file:
    path: ~/.kube
    state: directory
    mode: '0644'
  delegate_to: "{{ kubeinit_provision_service_node }}"

- name: Storing the master kubeconfig to the services machine.
  ansible.builtin.copy:
    content: "{{ _result_cluster_kubeconfig.content | default('Empty file') | b64decode }}"
    dest: ~/.kube/config
    mode: '0644'
  delegate_to: "{{ kubeinit_provision_service_node }}"

- name: Install kustomize
  ansible.builtin.shell: |
    curl -sL https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize%2Fv5.1.0/kustomize_v5.1.0_linux_amd64.tar.gz > kustomize.tar.gz
    tar xzf ./kustomize.tar.gz
    mv ./kustomize /bin/
  args:
    executable: /bin/bash
  register: _result
  changed_when: "_result.rc == 0"
  delegate_to: "{{ kubeinit_provision_service_node }}"

#
# Configure additional steps for including the Windows compute nodes
#

- name: Create the kubeinit folder
  ansible.windows.win_file:
    path: C:\k
    state: directory
  # TODO:FIXME: The following variables should be
  # added as group vars for those compute nodes which the
  # os is equals to 'windows'
  # TODO:FIXME: The usage of ansible_shell_type might
  # be different depending on the win_* task, the
  # supported values are [cmd | powershell]
  vars:
    ansible_shell_type: 'cmd'
    ansible_remote_tmp: 'C:\Windows\Temp'
  delegate_to: "{{ compute_node }}"
  loop: "{{ groups['all_compute_nodes'] | default([]) }}"
  loop_control:
    loop_var: compute_node
  when: hostvars[compute_node].os == 'windows'

- name: Copy the kubeconfig file
  ansible.windows.win_copy:
    content: "{{ _result_cluster_kubeconfig.content | default('Empty file') | b64decode }}"
    dest: C:\k\kube_config
  # TODO:FIXME: The following variables should be
  # added as group vars for those compute nodes which the
  # os is equals to 'windows'
  # TODO:FIXME: The usage of ansible_shell_type might
  # be different depending on the win_* task, the
  # supported values are [cmd | powershell]
  vars:
    ansible_shell_type: 'cmd'
    ansible_remote_tmp: 'C:\Windows\Temp'
  delegate_to: "{{ compute_node }}"
  loop: "{{ groups['all_compute_nodes'] | default([]) }}"
  loop_control:
    loop_var: compute_node
  when: hostvars[compute_node].os == 'windows'

- name: Copy the kubeconfig file
  ansible.windows.win_copy:
    content: "{{ _result_cluster_kubeconfig.content | default('Empty file') | b64decode }}"
    dest: C:\k\Kubeconfig
  # TODO:FIXME: The following variables should be
  # added as group vars for those compute nodes which the
  # os is equals to 'windows'
  # TODO:FIXME: The usage of ansible_shell_type might
  # be different depending on the win_* task, the
  # supported values are [cmd | powershell]
  vars:
    ansible_shell_type: 'cmd'
    ansible_remote_tmp: 'C:\Windows\Temp'
  delegate_to: "{{ compute_node }}"
  loop: "{{ groups['all_compute_nodes'] | default([]) }}"
  loop_control:
    loop_var: compute_node
  when: hostvars[compute_node].os == 'windows'

- name: Copy the kubeconfig file
  ansible.windows.win_copy:
    content: "{{ _result_cluster_kubeconfig.content | default('Empty file') | b64decode }}"
    dest: C:\k\config
  # TODO:FIXME: The following variables should be
  # added as group vars for those compute nodes which the
  # os is equals to 'windows'
  # TODO:FIXME: The usage of ansible_shell_type might
  # be different depending on the win_* task, the
  # supported values are [cmd | powershell]
  vars:
    ansible_shell_type: 'cmd'
    ansible_remote_tmp: 'C:\Windows\Temp'
  delegate_to: "{{ compute_node }}"
  loop: "{{ groups['all_compute_nodes'] | default([]) }}"
  loop_control:
    loop_var: compute_node
  when: hostvars[compute_node].os == 'windows'

- name: Write the join command
  ansible.windows.win_copy:
    content: "{{ _result_join_command.stdout | replace('kubeadm','c:\\k\\kubeadm.exe') }} --node-name {{ compute_node }}.{{ kubeinit_cluster_fqdn }} --cri-socket 'npipe:////./pipe/containerd-containerd' -v=10"
    dest: C:\k\k8s_join_command.ps1
  # TODO:FIXME: The following variables should be
  # added as group vars for those compute nodes which the
  # os is equals to 'windows'
  # TODO:FIXME: The usage of ansible_shell_type might
  # be different depending on the win_* task, the
  # supported values are [cmd | powershell]
  vars:
    ansible_shell_type: 'cmd'
    ansible_remote_tmp: 'C:\Windows\Temp'
  delegate_to: "{{ compute_node }}"
  loop: "{{ groups['all_compute_nodes'] | default([]) }}"
  loop_control:
    loop_var: compute_node
  when: hostvars[compute_node].os == 'windows'

- name: Install prereqs
  ansible.windows.win_powershell:
    script: |
      $env:Path += ";C:\k;C:\Program Files\containerd;C:\Program Files\nssm"
      powershell c:\k\PrepareRequirements.ps1
  # TODO:FIXME: The following variables should be
  # added as group vars for those compute nodes which the
  # os is equals to 'windows'
  # TODO:FIXME: The usage of ansible_shell_type might
  # be different depending on the win_* task, the
  # supported values are [cmd | powershell]
  vars:
    ansible_shell_type: 'cmd'
    ansible_remote_tmp: 'C:\Windows\Temp'
  delegate_to: "{{ compute_node }}"
  loop: "{{ groups['all_compute_nodes'] | default([]) }}"
  loop_control:
    loop_var: compute_node
  when: hostvars[compute_node].os == 'windows'

- name: Install containerd
  ansible.windows.win_powershell:
    script: |
      $env:Path += ";C:\k;C:\Program Files\containerd;C:\Program Files\nssm"
      powershell C:\k\Install-Containerd.ps1 -netAdapterName 'Ethernet' -ContainerDVersion '1.6.6'
  # TODO:FIXME: The following variables should be
  # added as group vars for those compute nodes which the
  # os is equals to 'windows'
  # TODO:FIXME: The usage of ansible_shell_type might
  # be different depending on the win_* task, the
  # supported values are [cmd | powershell]
  vars:
    ansible_shell_type: 'cmd'
    ansible_remote_tmp: 'C:\Windows\Temp'
  delegate_to: "{{ compute_node }}"
  loop: "{{ groups['all_compute_nodes'] | default([]) }}"
  loop_control:
    loop_var: compute_node
  when: hostvars[compute_node].os == 'windows'

- name: Prepare the Windows computes (register containerd and kubelet)
  ansible.windows.win_powershell:
    script: |
      $env:Path += ";C:\k;C:\Program Files\containerd;C:\Program Files\nssm"
      # The prepare node script will start kubelet as an nssm service
      # Make sure is like the following command, otherwise it will fail
      # for example: C:\k\kubelet.exe --container-runtime-endpoint=npipe:////./pipe/containerd-containerd --cert-dir=$env:SYSTEMDRIVE\var\lib\kubelet\pki --config=/var/lib/kubelet/config.yaml --kubeconfig=/k/config --hostname-override=compute-01.k8scluster.kubeinit.local --pod-infra-container-image=`"mcr.microsoft.com/oss/kubernetes/pause:3.6`" --enable-debugging-handlers --cgroups-per-qos=false --enforce-node-allocatable=`"`" --resolv-conf=`"`" --log-dir=/var/log/kubelet --logtostderr=true
      powershell C:\k\PrepareNode.ps1 -KubernetesVersion v1.24.2 -ContainerRuntime containerD
  # TODO:FIXME: The following variables should be
  # added as group vars for those compute nodes which the
  # os is equals to 'windows'
  # TODO:FIXME: The usage of ansible_shell_type might
  # be different depending on the win_* task, the
  # supported values are [cmd | powershell]
  vars:
    ansible_shell_type: 'cmd'
    ansible_remote_tmp: 'C:\Windows\Temp'
  delegate_to: "{{ compute_node }}"
  loop: "{{ groups['all_compute_nodes'] | default([]) }}"
  loop_control:
    loop_var: compute_node
  when: hostvars[compute_node].os == 'windows'

- name: Install prereqs and start script (register flannel)
  ansible.windows.win_powershell:
    script: |
      $env:Path += ";C:\k;C:\Program Files\containerd;C:\Program Files\nssm"
      # Default domain: controller-01.k8scluster.kubeinit.local
      # The management IP is the node's IP not the controller's IP.
      # for example: powershell C:\k\PrepareFlannel.ps1 -ManagementIP 10.0.0.2 -Hostname compute-01.k8scluster.kubeinit.local -NetworkMode overlay
      powershell C:\k\PrepareFlannel.ps1 -ManagementIP {{ hostvars[compute_node].ansible_host }}  -Hostname {{ compute_node }}.{{ kubeinit_cluster_fqdn }} -NetworkMode overlay
  # TODO:FIXME: The following variables should be
  # added as group vars for those compute nodes which the
  # os is equals to 'windows'
  # TODO:FIXME: The usage of ansible_shell_type might
  # be different depending on the win_* task, the
  # supported values are [cmd | powershell]
  vars:
    ansible_shell_type: 'cmd'
    ansible_remote_tmp: 'C:\Windows\Temp'
  delegate_to: "{{ compute_node }}"
  loop: "{{ groups['all_compute_nodes'] | default([]) }}"
  loop_control:
    loop_var: compute_node
  when: hostvars[compute_node].os == 'windows'

- name: Join the Windows computes in the cluster
  ansible.windows.win_powershell:
    script: |
      $env:Path += ";C:\k;C:\Program Files\containerd;C:\Program Files\nssm"
      # The kubelet service automatically joins the node to the cluster as kubelet is running as an nssm service
      # powershell C:\k\k8s_join_command.ps1
  # TODO:FIXME: The following variables should be
  # added as group vars for those compute nodes which the
  # os is equals to 'windows'
  # TODO:FIXME: The usage of ansible_shell_type might
  # be different depending on the win_* task, the
  # supported values are [cmd | powershell]
  vars:
    ansible_shell_type: 'cmd'
    ansible_remote_tmp: 'C:\Windows\Temp'
  delegate_to: "{{ compute_node }}"
  loop: "{{ groups['all_compute_nodes'] | default([]) }}"
  loop_control:
    loop_var: compute_node
  when: hostvars[compute_node].os == 'windows'

- name: Tag Windows compute nodes as workers
  ansible.builtin.shell: |
    set -o pipefail
    kubectl label node {{ compute_node }}.{{ kubeinit_cluster_fqdn }} node-role.kubernetes.io/worker=worker
  args:
    executable: /bin/bash
  register: _result
  changed_when: "_result.rc == 0"
  loop: "{{ groups['all_compute_nodes'] | default([]) }}"
  loop_control:
    loop_var: compute_node
  when: hostvars[compute_node].os == 'windows'
  delegate_to: "{{ kubeinit_first_controller_node }}"