Use run_once for kubeadm init phase upload-certs (#12759)

Before "5ca23e3bf (Changed to use first_kube_control_plane to parse
kubeadm_certificate_key (#11875), 2025-01-14)", kubespray would have
problem adding new control planes when the order of the nodes in kubectl
output and the ansible inventory were not the same.

But the underlying problem is that the operation is fundamentally
something that should be done only once, and recorded for all host in
play.

Since `register` and `sef_fact` when used with `run_once` set the
variable for all the hosts, use it. Also allows to use the variable
directly instead of relying on hostvars to make the task more readable.
This commit is contained in:
Max Gautier
2025-12-08 05:07:27 +00:00
committed by GitHub
parent 2edf176294
commit 88df61357b

View File

@@ -11,24 +11,23 @@
tags: tags:
- facts - facts
- name: Upload certificates so they are fresh and not expired - name: Obtain kubeadm certificate key for joining control planes nodes
command: >-
{{ bin_dir }}/kubeadm init phase
--config {{ kube_config_dir }}/kubeadm-config.yaml
upload-certs
--upload-certs
register: kubeadm_upload_cert
when: when:
- inventory_hostname == first_kube_control_plane
- not kube_external_ca_mode - not kube_external_ca_mode
- name: Parse certificate key if not set
set_fact:
kubeadm_certificate_key: "{{ hostvars[first_kube_control_plane]['kubeadm_upload_cert'].stdout_lines[-1] | trim }}"
run_once: true run_once: true
when: block:
- hostvars[first_kube_control_plane]['kubeadm_upload_cert'] is defined - name: Upload certificates so they are fresh and not expired
- hostvars[first_kube_control_plane]['kubeadm_upload_cert'] is not skipped command: >-
{{ bin_dir }}/kubeadm init phase
--config {{ kube_config_dir }}/kubeadm-config.yaml
upload-certs
--upload-certs
register: kubeadm_upload_cert
delegate_to: "{{ first_kube_control_plane }}"
- name: Parse certificate key if not set
set_fact:
kubeadm_certificate_key: "{{ kubeadm_upload_cert.stdout_lines[-1] | trim }}"
- name: Wait for k8s apiserver - name: Wait for k8s apiserver
wait_for: wait_for: