mirror of
https://github.com/kubesphere/kubekey.git
synced 2025-12-26 01:22:51 +00:00
feat: add module add_hostvars (#2629)
Signed-off-by: joyceliu <joyceliu@yunify.com>
This commit is contained in:
parent
03c6ce1fe7
commit
b629ec57ad
|
|
@ -22,9 +22,12 @@
|
|||
cat {{ .cloud_config_dir }}/cloud-config/value
|
||||
register: cloud_config_out
|
||||
register_type: yaml
|
||||
- name: set_fact of cloud-config value
|
||||
set_fact:
|
||||
cloud_config: "{{ .cloud_config_out.stdout | toJson }}"
|
||||
- name: add cloud_config to all hosts
|
||||
add_hostvars:
|
||||
hosts: all
|
||||
vars:
|
||||
cloud_config: >-
|
||||
{{ .hostVars.localhost.cloud_config_out.stdout }}
|
||||
roles:
|
||||
- role: init/init-artifacts
|
||||
when: .kubernetes_installed | default false | eq false
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@
|
|||
ignore_errors: true
|
||||
copy:
|
||||
src: >-
|
||||
{{ .binary_dir }}/repository/{{ .os.release.ID_LIKE }}-{{ .os.release.VERSION_ID }}-{{ .binary_type.stdout }}.iso
|
||||
{{ .binary_dir }}/repository/{{ .os.release.ID_LIKE }}-{{ .os.release.VERSION_ID }}-{{ .binary_type }}.iso
|
||||
dest: >-
|
||||
{{ .tmp_dir }}/repository.iso
|
||||
- name: Mount iso file
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@
|
|||
- name: Sync calicoctl to remote
|
||||
copy:
|
||||
src: >-
|
||||
{{ .binary_dir }}/cni/calico/{{ .calico_version }}/{{ .binary_type.stdout }}/calicoctl
|
||||
{{ .binary_dir }}/cni/calico/{{ .calico_version }}/{{ .binary_type }}/calicoctl
|
||||
dest: /usr/local/bin/calicoctl
|
||||
mode: 0755
|
||||
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@
|
|||
when: or (.runc_install_version.stderr | empty | not) (.runc_install_version.stdout | contains (printf "runc version %s\n" (.runc_version | default "" | trimPrefix "v" )) | not)
|
||||
copy:
|
||||
src: >-
|
||||
{{ .binary_dir }}/runc/{{ .runc_version }}/{{ .binary_type.stdout }}/runc.{{ .binary_type.stdout }}
|
||||
{{ .binary_dir }}/runc/{{ .runc_version }}/{{ .binary_type }}/runc.{{ .binary_type }}
|
||||
dest: /usr/local/bin/runc
|
||||
mode: 0755
|
||||
|
||||
|
|
@ -21,12 +21,12 @@
|
|||
- name: Sync containerd binary to remote
|
||||
copy:
|
||||
src: >-
|
||||
{{ .binary_dir }}/containerd/{{ .containerd_version }}/{{ .binary_type.stdout }}/containerd-{{ .containerd_version | default "" | trimPrefix "v" }}-linux-{{ .binary_type.stdout }}.tar.gz
|
||||
{{ .binary_dir }}/containerd/{{ .containerd_version }}/{{ .binary_type }}/containerd-{{ .containerd_version | default "" | trimPrefix "v" }}-linux-{{ .binary_type }}.tar.gz
|
||||
dest: >-
|
||||
{{ .tmp_dir }}/containerd-{{ .containerd_version | default "" | trimPrefix "v" }}-linux-{{ .binary_type.stdout }}.tar.gz
|
||||
{{ .tmp_dir }}/containerd-{{ .containerd_version | default "" | trimPrefix "v" }}-linux-{{ .binary_type }}.tar.gz
|
||||
- name: Unpackage containerd binary
|
||||
command: |
|
||||
tar -xvf {{ .tmp_dir }}/containerd-{{ .containerd_version | default "" | trimPrefix "v" }}-linux-{{ .binary_type.stdout }}.tar.gz --strip-components=1 -C /usr/local/bin/
|
||||
tar -xvf {{ .tmp_dir }}/containerd-{{ .containerd_version | default "" | trimPrefix "v" }}-linux-{{ .binary_type }}.tar.gz --strip-components=1 -C /usr/local/bin/
|
||||
- name: Generate containerd config file
|
||||
template:
|
||||
src: containerd.config
|
||||
|
|
|
|||
|
|
@ -10,12 +10,12 @@
|
|||
- name: Sync crictl binary to remote
|
||||
copy:
|
||||
src: >-
|
||||
{{ .binary_dir }}/crictl/{{ .crictl_version }}/{{ .binary_type.stdout }}/crictl-{{ .crictl_version }}-linux-{{ .binary_type.stdout }}.tar.gz
|
||||
{{ .binary_dir }}/crictl/{{ .crictl_version }}/{{ .binary_type }}/crictl-{{ .crictl_version }}-linux-{{ .binary_type }}.tar.gz
|
||||
dest: >-
|
||||
{{ .tmp_dir }}/crictl-{{ .crictl_version }}-linux-{{ .binary_type.stdout }}.tar.gz
|
||||
{{ .tmp_dir }}/crictl-{{ .crictl_version }}-linux-{{ .binary_type }}.tar.gz
|
||||
- name: Unpackage crictl binary
|
||||
command: |
|
||||
tar -xvf {{ .tmp_dir }}/crictl-{{ .crictl_version }}-linux-{{ .binary_type.stdout }}.tar.gz -C /usr/local/bin/
|
||||
tar -xvf {{ .tmp_dir }}/crictl-{{ .crictl_version }}-linux-{{ .binary_type }}.tar.gz -C /usr/local/bin/
|
||||
- name: Generate crictl config file
|
||||
template:
|
||||
src: crictl.config
|
||||
|
|
|
|||
|
|
@ -10,12 +10,12 @@
|
|||
- name: Sync cri-dockerd Binary to remote
|
||||
copy:
|
||||
src: >-
|
||||
{{ .binary_dir }}/cri-dockerd/{{ .cridockerd_version }}/{{ .binary_type.stdout }}/cri-dockerd-{{ .cridockerd_version | default "" | trimPrefix "v" }}.{{ .binary_type.stdout }}.tgz
|
||||
{{ .binary_dir }}/cri-dockerd/{{ .cridockerd_version }}/{{ .binary_type }}/cri-dockerd-{{ .cridockerd_version | default "" | trimPrefix "v" }}.{{ .binary_type }}.tgz
|
||||
dest: >-
|
||||
{{ .tmp_dir }}/cri-dockerd-{{ .cridockerd_version | default "" | trimPrefix "v" }}.{{ .binary_type.stdout }}.tgz
|
||||
{{ .tmp_dir }}/cri-dockerd-{{ .cridockerd_version | default "" | trimPrefix "v" }}.{{ .binary_type }}.tgz
|
||||
- name: Unpackage cri-dockerd binary
|
||||
command: |
|
||||
tar -xvf {{ .tmp_dir }}/cri-dockerd-{{ .cridockerd_version | default "" | trimPrefix "v" }}.{{ .binary_type.stdout }}.tgz --strip-components=1 -C /usr/local/bin/
|
||||
tar -xvf {{ .tmp_dir }}/cri-dockerd-{{ .cridockerd_version | default "" | trimPrefix "v" }}.{{ .binary_type }}.tgz --strip-components=1 -C /usr/local/bin/
|
||||
- name: Generate cri-dockerd Service file
|
||||
template:
|
||||
src: cri-dockerd.service
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@
|
|||
- name: Sync docker binary to remote
|
||||
copy:
|
||||
src: >-
|
||||
{{ .binary_dir }}/docker/{{ .docker_version }}/{{ .binary_type.stdout }}/docker-{{ .docker_version }}.tgz
|
||||
{{ .binary_dir }}/docker/{{ .docker_version }}/{{ .binary_type }}/docker-{{ .docker_version }}.tgz
|
||||
dest: >-
|
||||
{{ .tmp_dir }}/docker-{{ .docker_version }}.tgz
|
||||
- name: Unpackage docker binary
|
||||
|
|
|
|||
|
|
@ -9,12 +9,12 @@
|
|||
- name: Sync helm to remote
|
||||
copy:
|
||||
src: >-
|
||||
{{ .binary_dir }}/helm/{{ .helm_version }}/{{ .binary_type.stdout }}/helm-{{ .helm_version }}-linux-{{ .binary_type.stdout }}.tar.gz
|
||||
{{ .binary_dir }}/helm/{{ .helm_version }}/{{ .binary_type }}/helm-{{ .helm_version }}-linux-{{ .binary_type }}.tar.gz
|
||||
dest: >-
|
||||
{{ .tmp_dir }}/helm-{{ .helm_version }}-linux-{{ .binary_type.stdout }}.tar.gz
|
||||
{{ .tmp_dir }}/helm-{{ .helm_version }}-linux-{{ .binary_type }}.tar.gz
|
||||
- name: Install helm
|
||||
command: |
|
||||
tar --strip-components=1 -zxvf {{ .tmp_dir }}/helm-{{ .helm_version }}-linux-{{ .binary_type.stdout }}.tar.gz -C /usr/local/bin linux-{{ .binary_type.stdout }}/helm
|
||||
tar --strip-components=1 -zxvf {{ .tmp_dir }}/helm-{{ .helm_version }}-linux-{{ .binary_type }}.tar.gz -C /usr/local/bin linux-{{ .binary_type }}/helm
|
||||
|
||||
- name: Check if kubeadm is installed
|
||||
ignore_errors: true
|
||||
|
|
@ -24,7 +24,7 @@
|
|||
when: or (.kubeadm_install_version.stderr | empty | not) (.kubeadm_install_version.stdout | ne .kube_version)
|
||||
copy:
|
||||
src: >-
|
||||
{{ .binary_dir }}/kube/{{ .kube_version }}/{{ .binary_type.stdout }}/kubeadm
|
||||
{{ .binary_dir }}/kube/{{ .kube_version }}/{{ .binary_type }}/kubeadm
|
||||
dest: /usr/local/bin/kubeadm
|
||||
mode: 0755
|
||||
|
||||
|
|
@ -38,7 +38,7 @@
|
|||
or (.kubectl_install_version.stderr | empty | not) ((get .kubectl_install_version.stdout "Server Version") | ne .kube_version)
|
||||
copy:
|
||||
src: >-
|
||||
{{ .binary_dir }}/kube/{{ .kube_version }}/{{ .binary_type.stdout }}/kubectl
|
||||
{{ .binary_dir }}/kube/{{ .kube_version }}/{{ .binary_type }}/kubectl
|
||||
dest: /usr/local/bin/kubectl
|
||||
mode: 0755
|
||||
|
||||
|
|
@ -52,7 +52,7 @@
|
|||
- name: Sync kubelet to remote
|
||||
copy:
|
||||
src: >-
|
||||
{{ .binary_dir }}/kube/{{ .kube_version }}/{{ .binary_type.stdout }}/kubelet
|
||||
{{ .binary_dir }}/kube/{{ .kube_version }}/{{ .binary_type }}/kubelet
|
||||
dest: /usr/local/bin/kubelet
|
||||
mode: 0755
|
||||
- name: Sync kubelet env to remote
|
||||
|
|
@ -72,9 +72,9 @@
|
|||
- name: Sync cni-plugin to remote
|
||||
copy:
|
||||
src: >-
|
||||
{{ .binary_dir }}/cni/plugins/{{ .cni_plugins_version }}/{{ .binary_type.stdout }}/cni-plugins-linux-{{ .binary_type.stdout }}-{{ .cni_plugins_version }}.tgz
|
||||
{{ .binary_dir }}/cni/plugins/{{ .cni_plugins_version }}/{{ .binary_type }}/cni-plugins-linux-{{ .binary_type }}-{{ .cni_plugins_version }}.tgz
|
||||
dest: >-
|
||||
{{ .tmp_dir }}/cni-plugins-linux-{{ .binary_type.stdout }}-{{ .cni_plugins_version }}.tgz
|
||||
{{ .tmp_dir }}/cni-plugins-linux-{{ .binary_type }}-{{ .cni_plugins_version }}.tgz
|
||||
- name: Install cni-plugin
|
||||
command: |
|
||||
tar -zxvf {{ .tmp_dir }}/cni-plugins-linux-{{ .binary_type.stdout }}-{{ .cni_plugins_version }}.tgz -C /opt/cni/bin/
|
||||
tar -zxvf {{ .tmp_dir }}/cni-plugins-linux-{{ .binary_type }}-{{ .cni_plugins_version }}.tgz -C /opt/cni/bin/
|
||||
|
|
@ -49,6 +49,8 @@
|
|||
that: .kubernetes_install_version.stdout | default "" | trimPrefix "Kubernetes " | eq .kube_version
|
||||
fail_msg: >-
|
||||
kubernetes has installed with version:{{ .kubernetes_install_version.stdout | default "" | trimPrefix "Kubernetes " }}. but not match kube_version: {{ .kube_version }}
|
||||
- name: Set_Fact kubernetes_version
|
||||
set_fact:
|
||||
kubernetes_installed: true
|
||||
- name: add kubernetes_version variable to all hosts
|
||||
add_hostvars:
|
||||
hosts: all
|
||||
vars:
|
||||
kubernetes_installed: true
|
||||
|
|
|
|||
|
|
@ -43,19 +43,21 @@
|
|||
tasks:
|
||||
- name: select init node
|
||||
run_once: true
|
||||
set_fact:
|
||||
init_kubernetes_node: >-
|
||||
{{- $initNodes := list -}}
|
||||
{{- range .groups.kube_control_plane -}}
|
||||
{{- if index $.hostvars . "kubernetes_install_service" "stdout" | eq "active" -}}
|
||||
{{- $initNodes = append $initNodes . -}}
|
||||
add_hostvars:
|
||||
hosts: k8s_cluster
|
||||
vars:
|
||||
init_kubernetes_node: >-
|
||||
{{- $initNodes := list -}}
|
||||
{{- range .groups.kube_control_plane -}}
|
||||
{{- if index $.hostvars . "kubernetes_install_service" "stdout" | eq "active" -}}
|
||||
{{- $initNodes = append $initNodes . -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- if $initNodes | len | eq 1 -}}
|
||||
{{ $initNodes | first }}
|
||||
{{- else if $initNodes | len | lt 1 -}}
|
||||
{{ index $initNodes (randInt 0 ((sub ($initNodes | len) 1) | int)) }}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- if $initNodes | len | eq 1 -}}
|
||||
{{ $initNodes | first }}
|
||||
{{- else if $initNodes | len | lt 1 -}}
|
||||
{{ index $initNodes (randInt 0 ((sub ($initNodes | len) 1) | int)) }}
|
||||
{{- end -}}
|
||||
- name: init node
|
||||
when: eq .inventory_hostname .init_kubernetes_node
|
||||
block:
|
||||
|
|
@ -67,17 +69,21 @@
|
|||
/usr/local/bin/kubeadm init phase upload-certs --upload-certs --config /etc/kubernetes/kubeadm-config.yaml 2>&1 \
|
||||
| awk '/Using certificate key:/{getline; print}'
|
||||
register: kubeadm_cert_result
|
||||
- name: Set_Fact certificate key to all hosts
|
||||
set_fact:
|
||||
kubeadm_cert: >-
|
||||
{{ .kubeadm_cert_result.stdout }}
|
||||
- name: add certificate key to all hosts
|
||||
add_hostvars:
|
||||
hosts: k8s_cluster
|
||||
vars:
|
||||
kubeadm_cert: >-
|
||||
{{ .kubeadm_cert_result.stdout }}
|
||||
- name: Generate token by kubeadm
|
||||
command: /usr/local/bin/kubeadm token create
|
||||
register: kubeadm_token_result
|
||||
- name: Set_Fact token to all hosts
|
||||
set_fact:
|
||||
kubeadm_token: >-
|
||||
{{ .kubeadm_token_result.stdout }}
|
||||
- name: add token to all hosts
|
||||
add_hostvars:
|
||||
hosts: k8s_cluster
|
||||
vars:
|
||||
kubeadm_token: >-
|
||||
{{ .kubeadm_token_result.stdout }}
|
||||
|
||||
- hosts:
|
||||
- k8s_cluster
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@
|
|||
tags: ["certs"]
|
||||
roles:
|
||||
- role: certs/renew-etcd
|
||||
when: and (.groups.etcd | default list | len | lt 0) .renew_etcd
|
||||
when: and (.groups.etcd | default list | empty | not) .renew_etcd
|
||||
|
||||
- hosts:
|
||||
- image_registry
|
||||
|
|
|
|||
|
|
@ -76,7 +76,7 @@
|
|||
{{- range $k, $v := .kubernetes.custom_label }}
|
||||
/usr/local/bin/kubectl label --overwrite node {{ $.hostname }} {{ $k }}={{ $v }}
|
||||
{{- end }}
|
||||
when: .kubernetes.custom_label | len | lt 0
|
||||
when: .kubernetes.custom_label | empty | not
|
||||
|
||||
- hosts:
|
||||
- kube_control_plane|random
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@
|
|||
{{- $cpNodes = append $cpNodes . -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- if (subtractList $cpNodes .delete_nodes) | len | lt 0 }}
|
||||
{{- if (subtractList $cpNodes .delete_nodes) | empty | not }}
|
||||
exit 0
|
||||
{{- else }}
|
||||
echo "should keep at least one control_plane"
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@
|
|||
ignore_errors: true
|
||||
copy:
|
||||
src: >-
|
||||
{{ .binary_dir }}/repository/{{ .os.release.ID_LIKE }}-{{ .os.release.VERSION_ID }}-{{ .binary_type.stdout }}.iso
|
||||
{{ .binary_dir }}/repository/{{ .os.release.ID_LIKE }}-{{ .os.release.VERSION_ID }}-{{ .binary_type }}.iso
|
||||
dest: >-
|
||||
{{ .tmp_dir }}/repository.iso
|
||||
- name: Mount iso file
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@
|
|||
- name: Sync calicoctl to remote
|
||||
copy:
|
||||
src: >-
|
||||
{{ .binary_dir }}/cni/calico/{{ .calico_version }}/{{ .binary_type.stdout }}/calicoctl
|
||||
{{ .binary_dir }}/cni/calico/{{ .calico_version }}/{{ .binary_type }}/calicoctl
|
||||
dest: /usr/local/bin/calicoctl
|
||||
mode: 0755
|
||||
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ image_registry:
|
|||
registry: >-
|
||||
{{- if .image_registry.ha_vip | empty | not -}}
|
||||
{{ .image_registry.ha_vip }}
|
||||
{{- else if .groups.image_registry | default list | len | lt 0 -}}
|
||||
{{- else if .groups.image_registry | default list | empty | not -}}
|
||||
{{- $internalIPv4 := index .hostvars (.groups.image_registry | default list | first) "internal_ipv4" | default "" -}}
|
||||
{{- $internalIPv6 := index .hostvars (.groups.image_registry | default list | first) "internal_ipv6" | default "" -}}
|
||||
{{- if $internalIPv4 | empty | not -}}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@
|
|||
when: or (.runc_install_version.stderr | empty | not) (.runc_install_version.stdout | contains (printf "runc version %s\n" (.runc_version | default "" | trimPrefix "v" )) | not)
|
||||
copy:
|
||||
src: >-
|
||||
{{ .binary_dir }}/runc/{{ .runc_version }}/{{ .binary_type.stdout }}/runc.{{ .binary_type.stdout }}
|
||||
{{ .binary_dir }}/runc/{{ .runc_version }}/{{ .binary_type }}/runc.{{ .binary_type }}
|
||||
dest: /usr/local/bin/runc
|
||||
mode: 0755
|
||||
|
||||
|
|
@ -21,12 +21,12 @@
|
|||
- name: Sync containerd binary to remote
|
||||
copy:
|
||||
src: >-
|
||||
{{ .binary_dir }}/containerd/{{ .containerd_version }}/{{ .binary_type.stdout }}/containerd-{{ .containerd_version | default "" | trimPrefix "v" }}-linux-{{ .binary_type.stdout }}.tar.gz
|
||||
{{ .binary_dir }}/containerd/{{ .containerd_version }}/{{ .binary_type }}/containerd-{{ .containerd_version | default "" | trimPrefix "v" }}-linux-{{ .binary_type }}.tar.gz
|
||||
dest: >-
|
||||
{{ .tmp_dir }}/containerd-{{ .containerd_version | default "" | trimPrefix "v" }}-linux-{{ .binary_type.stdout }}.tar.gz
|
||||
{{ .tmp_dir }}/containerd-{{ .containerd_version | default "" | trimPrefix "v" }}-linux-{{ .binary_type }}.tar.gz
|
||||
- name: Unpackage containerd binary
|
||||
command: |
|
||||
tar -xvf {{ .tmp_dir }}/containerd-{{ .containerd_version | default "" | trimPrefix "v" }}-linux-{{ .binary_type.stdout }}.tar.gz --strip-components=1 -C /usr/local/bin/
|
||||
tar -xvf {{ .tmp_dir }}/containerd-{{ .containerd_version | default "" | trimPrefix "v" }}-linux-{{ .binary_type }}.tar.gz --strip-components=1 -C /usr/local/bin/
|
||||
- name: Generate containerd config file
|
||||
template:
|
||||
src: containerd.config
|
||||
|
|
|
|||
|
|
@ -10,12 +10,12 @@
|
|||
- name: Sync crictl binary to remote
|
||||
copy:
|
||||
src: >-
|
||||
{{ .binary_dir }}/crictl/{{ .crictl_version }}/{{ .binary_type.stdout }}/crictl-{{ .crictl_version }}-linux-{{ .binary_type.stdout }}.tar.gz
|
||||
{{ .binary_dir }}/crictl/{{ .crictl_version }}/{{ .binary_type }}/crictl-{{ .crictl_version }}-linux-{{ .binary_type }}.tar.gz
|
||||
dest: >-
|
||||
{{ .tmp_dir }}/crictl-{{ .crictl_version }}-linux-{{ .binary_type.stdout }}.tar.gz
|
||||
{{ .tmp_dir }}/crictl-{{ .crictl_version }}-linux-{{ .binary_type }}.tar.gz
|
||||
- name: Unpackage crictl binary
|
||||
command: |
|
||||
tar -xvf {{ .tmp_dir }}/crictl-{{ .crictl_version }}-linux-{{ .binary_type.stdout }}.tar.gz -C /usr/local/bin/
|
||||
tar -xvf {{ .tmp_dir }}/crictl-{{ .crictl_version }}-linux-{{ .binary_type }}.tar.gz -C /usr/local/bin/
|
||||
- name: Generate crictl config file
|
||||
template:
|
||||
src: crictl.config
|
||||
|
|
|
|||
|
|
@ -10,12 +10,12 @@
|
|||
- name: Sync cri-dockerd Binary to remote
|
||||
copy:
|
||||
src: >-
|
||||
{{ .binary_dir }}/cri-dockerd/{{ .cridockerd_version }}/{{ .binary_type.stdout }}/cri-dockerd-{{ .cridockerd_version | default "" | trimPrefix "v" }}.{{ .binary_type.stdout }}.tgz
|
||||
{{ .binary_dir }}/cri-dockerd/{{ .cridockerd_version }}/{{ .binary_type }}/cri-dockerd-{{ .cridockerd_version | default "" | trimPrefix "v" }}.{{ .binary_type }}.tgz
|
||||
dest: >-
|
||||
{{ .tmp_dir }}/cri-dockerd-{{ .cridockerd_version | default "" | trimPrefix "v" }}.{{ .binary_type.stdout }}.tgz
|
||||
{{ .tmp_dir }}/cri-dockerd-{{ .cridockerd_version | default "" | trimPrefix "v" }}.{{ .binary_type }}.tgz
|
||||
- name: Unpackage cri-dockerd binary
|
||||
command: |
|
||||
tar -xvf {{ .tmp_dir }}/cri-dockerd-{{ .cridockerd_version | default "" | trimPrefix "v" }}.{{ .binary_type.stdout }}.tgz --strip-components=1 -C /usr/local/bin/
|
||||
tar -xvf {{ .tmp_dir }}/cri-dockerd-{{ .cridockerd_version | default "" | trimPrefix "v" }}.{{ .binary_type }}.tgz --strip-components=1 -C /usr/local/bin/
|
||||
- name: Generate cri-dockerd Service file
|
||||
template:
|
||||
src: cri-dockerd.service
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@
|
|||
- name: Sync docker binary to remote
|
||||
copy:
|
||||
src: >-
|
||||
{{ .binary_dir }}/docker/{{ .docker_version }}/{{ .binary_type.stdout }}/docker-{{ .docker_version }}.tgz
|
||||
{{ .binary_dir }}/docker/{{ .docker_version }}/{{ .binary_type }}/docker-{{ .docker_version }}.tgz
|
||||
dest: >-
|
||||
{{ .tmp_dir }}/docker-{{ .docker_version }}.tgz
|
||||
- name: Unpackage docker binary
|
||||
|
|
|
|||
|
|
@ -2,14 +2,14 @@
|
|||
- name: Sync etcd binary to node
|
||||
copy:
|
||||
src: >-
|
||||
{{ .binary_dir }}/etcd/{{ .etcd_version }}/{{ .binary_type.stdout }}/etcd-{{ .etcd_version }}-linux-{{ .binary_type.stdout }}.tar.gz
|
||||
{{ .binary_dir }}/etcd/{{ .etcd_version }}/{{ .binary_type }}/etcd-{{ .etcd_version }}-linux-{{ .binary_type }}.tar.gz
|
||||
dest: >-
|
||||
{{ .tmp_dir }}/etcd-{{ .etcd_version }}-linux-{{ .binary_type.stdout }}.tar.gz
|
||||
{{ .tmp_dir }}/etcd-{{ .etcd_version }}-linux-{{ .binary_type }}.tar.gz
|
||||
|
||||
- name: Extract etcd binary
|
||||
command: |
|
||||
tar --strip-components=1 -C /usr/local/bin/ -xvf {{ .tmp_dir }}/etcd-{{ .etcd_version }}-linux-{{ .binary_type.stdout }}.tar.gz \
|
||||
--wildcards etcd-{{ .etcd_version }}-linux-{{ .binary_type.stdout }}/etcd*
|
||||
tar --strip-components=1 -C /usr/local/bin/ -xvf {{ .tmp_dir }}/etcd-{{ .etcd_version }}-linux-{{ .binary_type }}.tar.gz \
|
||||
--wildcards etcd-{{ .etcd_version }}-linux-{{ .binary_type }}/etcd*
|
||||
|
||||
- name: Sync ca file to remote
|
||||
copy:
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@
|
|||
- name: Sync docker binary to remote
|
||||
copy:
|
||||
src: >-
|
||||
{{ .binary_dir }}/docker/{{ .docker_version }}/{{ .binary_type.stdout }}/docker-{{ .docker_version }}.tgz
|
||||
{{ .binary_dir }}/docker/{{ .docker_version }}/{{ .binary_type }}/docker-{{ .docker_version }}.tgz
|
||||
dest: >-
|
||||
{{ .tmp_dir }}/docker-{{ .docker_version }}.tgz
|
||||
- name: Generate docker config file
|
||||
|
|
|
|||
|
|
@ -8,6 +8,6 @@
|
|||
when: or (.dockercompose_install_version.stderr | empty | not) (.dockercompose_install_version.stdout | ne (printf "Docker Compose version %s" .dockercompose_version))
|
||||
copy:
|
||||
src: >-
|
||||
{{ .binary_dir }}/image-registry/docker-compose/{{ .dockercompose_version }}/{{ .binary_type.stdout }}/docker-compose
|
||||
{{ .binary_dir }}/image-registry/docker-compose/{{ .dockercompose_version }}/{{ .binary_type }}/docker-compose
|
||||
dest: /usr/local/bin/docker-compose
|
||||
mode: 0755
|
||||
|
|
|
|||
|
|
@ -2,9 +2,9 @@
|
|||
- name: Sync registry image to remote
|
||||
copy:
|
||||
src: >-
|
||||
{{ .binary_dir }}/image-registry/registry/{{ .registry_version }}/{{ .binary_type.stdout }}/registry-{{ .registry_version }}-linux-{{ .binary_type.stdout }}.tgz
|
||||
{{ .binary_dir }}/image-registry/registry/{{ .registry_version }}/{{ .binary_type }}/registry-{{ .registry_version }}-linux-{{ .binary_type }}.tgz
|
||||
dest: >-
|
||||
/opt/registry/{{ .registry_version }}/registry-{{ .registry_version }}-linux-{{ .binary_type.stdout }}.tgz
|
||||
/opt/registry/{{ .registry_version }}/registry-{{ .registry_version }}-linux-{{ .binary_type }}.tgz
|
||||
|
||||
- name: Mount NFS dir
|
||||
command: |
|
||||
|
|
@ -29,7 +29,7 @@
|
|||
|
||||
- name: Load registry image
|
||||
command: |
|
||||
docker load -i /opt/registry/{{ .registry_version }}/registry-{{ .registry_version }}-linux-{{ .binary_type.stdout }}.tgz
|
||||
docker load -i /opt/registry/{{ .registry_version }}/registry-{{ .registry_version }}-linux-{{ .binary_type }}.tgz
|
||||
|
||||
- name: Sync image registry cert file to remote
|
||||
copy:
|
||||
|
|
|
|||
|
|
@ -1,26 +1,28 @@
|
|||
---
|
||||
- name: select init node
|
||||
run_once: true
|
||||
set_fact:
|
||||
init_kubernetes_node: >-
|
||||
{{- $initNodes := list -}}
|
||||
{{- $notInitNodes := list -}}
|
||||
{{- range .groups.kube_control_plane -}}
|
||||
{{- if index $.hostvars . "kubernetes_install_service" "stdout" | eq "active" -}}
|
||||
{{- $initNodes = append $initNodes . -}}
|
||||
{{- else if index $.hostvars . "kubernetes_install_service" "stdout" | eq "inactive" -}}
|
||||
{{- $notInitNodes = append $notInitNodes . -}}
|
||||
add_hostvars:
|
||||
hosts: k8s_cluster
|
||||
vars:
|
||||
init_kubernetes_node: >-
|
||||
{{- $initNodes := list -}}
|
||||
{{- $notInitNodes := list -}}
|
||||
{{- range .groups.kube_control_plane -}}
|
||||
{{- if index $.hostvars . "kubernetes_install_service" "stdout" | eq "active" -}}
|
||||
{{- $initNodes = append $initNodes . -}}
|
||||
{{- else if index $.hostvars . "kubernetes_install_service" "stdout" | eq "inactive" -}}
|
||||
{{- $notInitNodes = append $notInitNodes . -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- if $initNodes | len | eq 1 -}}
|
||||
{{ $initNodes | first }}
|
||||
{{- else if $initNodes | len | lt 1 -}}
|
||||
{{ index $initNodes (randInt 0 ((sub ($initNodes | len) 1) | int)) }}
|
||||
{{- else if $notInitNodes | len | eq 1 -}}
|
||||
{{ $notInitNodes | first }}
|
||||
{{- else if $notInitNodes | len | lt 1 -}}
|
||||
{{ index $notInitNodes (randInt 0 ((sub ($notInitNodes | len) 1) | int)) }}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- if $initNodes | len | eq 1 -}}
|
||||
{{ $initNodes | first }}
|
||||
{{- else if $initNodes | len | lt 1 -}}
|
||||
{{ index $initNodes (randInt 0 ((sub ($initNodes | len) 1) | int)) }}
|
||||
{{- else if $notInitNodes | len | eq 1 -}}
|
||||
{{ $notInitNodes | first }}
|
||||
{{- else if $notInitNodes | len | lt 1 -}}
|
||||
{{ index $notInitNodes (randInt 0 ((sub ($notInitNodes | len) 1) | int)) }}
|
||||
{{- end -}}
|
||||
|
||||
- name: Set control_plane_endpoint hosts to localDNS file
|
||||
when:
|
||||
|
|
@ -56,16 +58,20 @@
|
|||
/usr/local/bin/kubeadm init phase upload-certs --upload-certs --config=/etc/kubernetes/kubeadm-config.yaml 2>&1 \
|
||||
| awk '/Using certificate key:/{getline; print}'
|
||||
register: kubeadm_cert_result
|
||||
- name: Set_Fact certificate key to all hosts
|
||||
set_fact:
|
||||
kubeadm_cert: >-
|
||||
{{ .kubeadm_cert_result.stdout }}
|
||||
- name: add certificate key to all hosts
|
||||
add_hostvars:
|
||||
hosts: k8s_cluster
|
||||
vars:
|
||||
kubeadm_cert: >-
|
||||
{{ .kubeadm_cert_result.stdout }}
|
||||
- name: Generate kubeadm token
|
||||
block:
|
||||
- name: Generate token by kubeadm
|
||||
command: /usr/local/bin/kubeadm token create
|
||||
register: kubeadm_token_result
|
||||
- name: Set_Fact token to all hosts
|
||||
set_fact:
|
||||
kubeadm_token: >-
|
||||
{{ .kubeadm_token_result.stdout }}
|
||||
- name: add token to all hosts
|
||||
add_hostvars:
|
||||
hosts: k8s_cluster
|
||||
vars:
|
||||
kubeadm_token: >-
|
||||
{{ .kubeadm_token_result.stdout }}
|
||||
|
|
|
|||
|
|
@ -66,7 +66,7 @@ data:
|
|||
|
||||
{{- range .forward }}
|
||||
forward {{ .from }} {{ .to | join " " }} {
|
||||
{{- if .except | len | lt 0 }}
|
||||
{{- if .except | empty | not }}
|
||||
except {{ .except | join " " }}
|
||||
{{- end }}
|
||||
{{- if .force_tcp }}
|
||||
|
|
|
|||
|
|
@ -9,12 +9,12 @@
|
|||
- name: Sync helm to remote
|
||||
copy:
|
||||
src: >-
|
||||
{{ .binary_dir }}/helm/{{ .helm_version }}/{{ .binary_type.stdout }}/helm-{{ .helm_version }}-linux-{{ .binary_type.stdout }}.tar.gz
|
||||
{{ .binary_dir }}/helm/{{ .helm_version }}/{{ .binary_type }}/helm-{{ .helm_version }}-linux-{{ .binary_type }}.tar.gz
|
||||
dest: >-
|
||||
{{ .tmp_dir }}/helm-{{ .helm_version }}-linux-{{ .binary_type.stdout }}.tar.gz
|
||||
{{ .tmp_dir }}/helm-{{ .helm_version }}-linux-{{ .binary_type }}.tar.gz
|
||||
- name: Install helm
|
||||
command: |
|
||||
tar --strip-components=1 -zxvf {{ .tmp_dir }}/helm-{{ .helm_version }}-linux-{{ .binary_type.stdout }}.tar.gz -C /usr/local/bin linux-{{ .binary_type.stdout }}/helm
|
||||
tar --strip-components=1 -zxvf {{ .tmp_dir }}/helm-{{ .helm_version }}-linux-{{ .binary_type }}.tar.gz -C /usr/local/bin linux-{{ .binary_type }}/helm
|
||||
|
||||
- name: Check if kubeadm is installed
|
||||
ignore_errors: true
|
||||
|
|
@ -24,7 +24,7 @@
|
|||
when: or (.kubeadm_install_version.stderr | empty | not) (.kubeadm_install_version.stdout | ne .kube_version)
|
||||
copy:
|
||||
src: >-
|
||||
{{ .binary_dir }}/kube/{{ .kube_version }}/{{ .binary_type.stdout }}/kubeadm
|
||||
{{ .binary_dir }}/kube/{{ .kube_version }}/{{ .binary_type }}/kubeadm
|
||||
dest: /usr/local/bin/kubeadm
|
||||
mode: 0755
|
||||
|
||||
|
|
@ -38,7 +38,7 @@
|
|||
or (.kubectl_install_version.stderr | empty | not) ((get .kubectl_install_version.stdout "Server Version") | ne .kube_version)
|
||||
copy:
|
||||
src: >-
|
||||
{{ .binary_dir }}/kube/{{ .kube_version }}/{{ .binary_type.stdout }}/kubectl
|
||||
{{ .binary_dir }}/kube/{{ .kube_version }}/{{ .binary_type }}/kubectl
|
||||
dest: /usr/local/bin/kubectl
|
||||
mode: 0755
|
||||
|
||||
|
|
@ -52,7 +52,7 @@
|
|||
- name: Sync kubelet to remote
|
||||
copy:
|
||||
src: >-
|
||||
{{ .binary_dir }}/kube/{{ .kube_version }}/{{ .binary_type.stdout }}/kubelet
|
||||
{{ .binary_dir }}/kube/{{ .kube_version }}/{{ .binary_type }}/kubelet
|
||||
dest: /usr/local/bin/kubelet
|
||||
mode: 0755
|
||||
- name: Sync kubelet env to remote
|
||||
|
|
@ -72,9 +72,9 @@
|
|||
- name: Sync cni-plugin to remote
|
||||
copy:
|
||||
src: >-
|
||||
{{ .binary_dir }}/cni/plugins/{{ .cni_plugins_version }}/{{ .binary_type.stdout }}/cni-plugins-linux-{{ .binary_type.stdout }}-{{ .cni_plugins_version }}.tgz
|
||||
{{ .binary_dir }}/cni/plugins/{{ .cni_plugins_version }}/{{ .binary_type }}/cni-plugins-linux-{{ .binary_type }}-{{ .cni_plugins_version }}.tgz
|
||||
dest: >-
|
||||
{{ .tmp_dir }}/cni-plugins-linux-{{ .binary_type.stdout }}-{{ .cni_plugins_version }}.tgz
|
||||
{{ .tmp_dir }}/cni-plugins-linux-{{ .binary_type }}-{{ .cni_plugins_version }}.tgz
|
||||
- name: Install cni-plugin
|
||||
command: |
|
||||
tar -zxvf {{ .tmp_dir }}/cni-plugins-linux-{{ .binary_type.stdout }}-{{ .cni_plugins_version }}.tgz -C /opt/cni/bin/
|
||||
tar -zxvf {{ .tmp_dir }}/cni-plugins-linux-{{ .binary_type }}-{{ .cni_plugins_version }}.tgz -C /opt/cni/bin/
|
||||
|
|
@ -9,7 +9,7 @@
|
|||
|
||||
- name: Stop if etcd group is empty in external etcd mode
|
||||
assert:
|
||||
that: .groups.etcd | len | lt 0
|
||||
that: .groups.etcd | empty | not
|
||||
fail_msg: "group \"etcd\" cannot be empty in external etcd mode"
|
||||
run_once: true
|
||||
when: .kubernetes.etcd.deployment_type | eq "external"
|
||||
|
|
|
|||
|
|
@ -12,14 +12,16 @@
|
|||
- name: Stop if arch supported
|
||||
assert:
|
||||
that: or (.cluster_require.supported_architectures.amd64 | has .os.architecture) (.cluster_require.supported_architectures.arm64 | has .os.architecture)
|
||||
success_msg: >-
|
||||
fail_msg: "{{ .os.architecture }} is not a known arch"
|
||||
|
||||
- name: Set binary_type for each hosts
|
||||
set_fact:
|
||||
binary_type: >-
|
||||
{{- if .cluster_require.supported_architectures.amd64 | has .os.architecture -}}
|
||||
amd64
|
||||
{{- else -}}
|
||||
arm64
|
||||
{{- end -}}
|
||||
fail_msg: "{{ .os.architecture }} is not a known arch"
|
||||
register: binary_type
|
||||
|
||||
- name: Stop if memory is too small for masters
|
||||
assert:
|
||||
|
|
|
|||
|
|
@ -125,6 +125,9 @@ func (o *ArtifactImagesOptions) Complete(cmd *cobra.Command, args []string) (*kk
|
|||
Debug: o.Debug,
|
||||
Tags: []string{"only_image"},
|
||||
}
|
||||
if err := completeInventory(o.CommonOptions.InventoryFile, o.CommonOptions.Inventory); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := o.CommonOptions.Complete(playbook); err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
|
|
|
|||
|
|
@ -66,6 +66,9 @@ func (o *CertsRenewOptions) Complete(cmd *cobra.Command, args []string) (*kkcore
|
|||
Debug: o.Debug,
|
||||
Tags: []string{"certs"},
|
||||
}
|
||||
if err := completeInventory(o.CommonOptions.InventoryFile, o.CommonOptions.Inventory); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return playbook, o.CommonOptions.Complete(playbook)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -75,6 +75,9 @@ func (o *InitOSOptions) Complete(cmd *cobra.Command, args []string) (*kkcorev1.P
|
|||
Playbook: o.Playbook,
|
||||
Debug: o.Debug,
|
||||
}
|
||||
if err := completeInventory(o.CommonOptions.InventoryFile, o.CommonOptions.Inventory); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := o.CommonOptions.Complete(playbook); err != nil {
|
||||
return nil, err
|
||||
|
|
@ -142,6 +145,9 @@ func (o *InitRegistryOptions) Complete(cmd *cobra.Command, args []string) (*kkco
|
|||
Playbook: o.Playbook,
|
||||
Debug: o.Debug,
|
||||
}
|
||||
if err := completeInventory(o.CommonOptions.InventoryFile, o.CommonOptions.Inventory); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return playbook, o.CommonOptions.Complete(playbook)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -55,6 +55,7 @@ task执行时, 会在定义的host分别上执行.
|
|||
**rescue**: task集合, 非必填, 当block执行失败(task集合有一个执行失败即为该block失败)时,执行该task集合.
|
||||
**always**: task集合, 非必填, 当block和rescue执行完毕后(无论成功失败)都会执行该task集合.
|
||||
**module**: task实际要执行的操作, 非必填(当未block字段时, 必填).map格式的数据, key为module_name, value为args. 可用的module需提前在项目中进行注册。已注册的module如下
|
||||
- [add_hostvars](modules/add_hostvars.md)
|
||||
- [assert](modules/assert.md)
|
||||
- [command](modules/command.md)
|
||||
- [copy](modules/copy.md)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,31 @@
|
|||
# add_hostvars 模块
|
||||
|
||||
add_hostvars模块允许用户将变量设置到指定的主机中生效。
|
||||
|
||||
## 参数
|
||||
|
||||
| 参数 | 说明 | 类型 | 必填 | 默认值 |
|
||||
|------|------|------|------|-------|
|
||||
| hosts | 需要设置参数的目标主机 | 字符串或字符串数组 | 否 | - |
|
||||
| vars | 需要设置的参数 | map | 否 | - |
|
||||
|
||||
## 使用示例
|
||||
|
||||
1. 设置字符串参数
|
||||
```yaml
|
||||
- name: set string
|
||||
add_hostvars:
|
||||
name: all
|
||||
vars:
|
||||
c: d
|
||||
```
|
||||
|
||||
2. 设置map参数
|
||||
```yaml
|
||||
- name: set map
|
||||
add_hostvars:
|
||||
name: all
|
||||
vars:
|
||||
a:
|
||||
b: c
|
||||
```
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
# set_fact 模块
|
||||
|
||||
set_fact模块允许用户将变量设置到所有的主机中生效。
|
||||
set_fact模块允许用户将变量设置到当前执行的主机中生效。
|
||||
|
||||
## 参数
|
||||
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import (
|
|||
|
||||
"github.com/cockroachdb/errors"
|
||||
kkcorev1 "github.com/kubesphere/kubekey/api/core/v1"
|
||||
kkcorev1alpha1 "github.com/kubesphere/kubekey/api/core/v1alpha1"
|
||||
kkprojectv1 "github.com/kubesphere/kubekey/api/project/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
ctrl "sigs.k8s.io/controller-runtime"
|
||||
|
|
@ -172,7 +173,7 @@ func (e blockExecutor) dealTask(ctx context.Context, hosts []string, when []stri
|
|||
}
|
||||
}
|
||||
if task.Spec.Module.Name == "" { // action is necessary for a task
|
||||
return errors.Errorf("no module/action detected in task: %s", task.Name)
|
||||
return errors.Errorf("no module/action detected in task: [%s]%s", task.Annotations[kkcorev1alpha1.TaskAnnotationRelativePath], task.Spec.Name)
|
||||
}
|
||||
// complete by playbook
|
||||
task.GenerateName = e.playbook.Name + "-"
|
||||
|
|
|
|||
|
|
@ -0,0 +1,117 @@
|
|||
package modules
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/cockroachdb/errors"
|
||||
"gopkg.in/yaml.v3"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
||||
|
||||
"github.com/kubesphere/kubekey/v4/pkg/variable"
|
||||
)
|
||||
|
||||
/*
|
||||
Module: add_hostvars
|
||||
|
||||
Description:
|
||||
- Adds or updates host variables for one or more hosts.
|
||||
- Accepts a YAML mapping with "hosts" (string or array of strings) and "vars" (mapping of variables).
|
||||
- Similar in spirit to set_fact.go, but operates on multiple hosts.
|
||||
|
||||
Example Usage in Playbook Task:
|
||||
- name: Add custom variables to hosts
|
||||
add_hostvars:
|
||||
hosts: ["host1", "host2"]
|
||||
vars:
|
||||
custom_var: "value"
|
||||
another_var: 42
|
||||
|
||||
Return Values:
|
||||
- On success: returns empty stdout and stderr.
|
||||
- On failure: returns error message in stderr.
|
||||
*/
|
||||
|
||||
// addHostvarsArgs holds the parsed arguments for the add_hostvars module.
|
||||
type addHostvarsArgs struct {
|
||||
hosts []string // List of hosts to which variables will be added
|
||||
vars yaml.Node // Variables to add, as a YAML node
|
||||
}
|
||||
|
||||
// newAddHostvarsArgs parses the raw module arguments and returns an addHostvarsArgs struct.
|
||||
// The arguments must be a YAML mapping with "hosts" and "vars" keys.
|
||||
// "hosts" can be a string or a sequence of strings.
|
||||
// "vars" must be a mapping node.
|
||||
func newAddHostvarsArgs(_ context.Context, raw runtime.RawExtension, vars map[string]any) (*addHostvarsArgs, error) {
|
||||
var node yaml.Node
|
||||
// Unmarshal the YAML document into a root node.
|
||||
if err := yaml.Unmarshal(raw.Raw, &node); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// The root node should be a document node with a single mapping node as its content.
|
||||
if len(node.Content) != 1 && node.Content[0].Kind != yaml.MappingNode {
|
||||
return nil, errors.New("module argument format error")
|
||||
}
|
||||
args := &addHostvarsArgs{}
|
||||
// Iterate over the mapping node's key-value pairs.
|
||||
for i := 0; i < len(node.Content[0].Content); i += 2 {
|
||||
keyNode := node.Content[0].Content[i]
|
||||
valueNode := node.Content[0].Content[i+1]
|
||||
switch keyNode.Value {
|
||||
case "hosts":
|
||||
var val any
|
||||
if err := valueNode.Decode(&val); err != nil {
|
||||
return nil, errors.New("cannot decode \"hosts\"")
|
||||
}
|
||||
args.hosts, _ = variable.StringSliceVar(vars, map[string]any{"hosts": val}, "hosts")
|
||||
case "vars":
|
||||
// Store the "vars" node for later processing.
|
||||
args.vars = *valueNode
|
||||
}
|
||||
}
|
||||
|
||||
// Validate that hosts and vars are not empty.
|
||||
if len(args.hosts) == 0 {
|
||||
return nil, errors.New("\"hosts\" should be string or string array")
|
||||
}
|
||||
if args.vars.IsZero() {
|
||||
return nil, errors.New("\"vars\" should not be empty")
|
||||
}
|
||||
|
||||
return args, nil
|
||||
}
|
||||
|
||||
// ModuleAddHostvars handles the "add_hostvars" module, merging variables into the specified hosts.
|
||||
// Returns empty stdout and stderr on success, or error message in stderr on failure.
|
||||
func ModuleAddHostvars(ctx context.Context, options ExecOptions) (string, string) {
|
||||
// Get all host variables (for context, not used directly here).
|
||||
ha, err := options.getAllVariables()
|
||||
if err != nil {
|
||||
return "", err.Error()
|
||||
}
|
||||
// Parse module arguments.
|
||||
args, err := newAddHostvarsArgs(ctx, options.Args, ha)
|
||||
if err != nil {
|
||||
return "", err.Error()
|
||||
}
|
||||
ahn, err := options.Variable.Get(variable.GetHostnames(args.hosts))
|
||||
if err != nil {
|
||||
return "", err.Error()
|
||||
}
|
||||
hosts, ok := ahn.([]string)
|
||||
if !ok {
|
||||
return "", "failed to get actual hosts from given \"hosts\""
|
||||
}
|
||||
|
||||
// Merge the provided variables into the specified hosts.
|
||||
if err := options.Variable.Merge(variable.MergeHostsRuntimeVariable(args.vars, options.Host, hosts...)); err != nil {
|
||||
return "", fmt.Sprintf("add_hostvars error: %v", err)
|
||||
}
|
||||
|
||||
return "", ""
|
||||
}
|
||||
|
||||
func init() {
|
||||
utilruntime.Must(RegisterModule("add_hostvars", ModuleAddHostvars))
|
||||
}
|
||||
|
|
@ -0,0 +1,68 @@
|
|||
package modules
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
kkcorev1 "github.com/kubesphere/kubekey/api/core/v1"
|
||||
kkcorev1alpha1 "github.com/kubesphere/kubekey/api/core/v1alpha1"
|
||||
"github.com/stretchr/testify/require"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
)
|
||||
|
||||
func TestModuleAddHostvars(t *testing.T) {
|
||||
type testcase struct {
|
||||
name string
|
||||
args []byte
|
||||
expectStdout string
|
||||
expectStderr string
|
||||
}
|
||||
cases := []testcase{
|
||||
{
|
||||
name: "missing hosts",
|
||||
args: []byte(`
|
||||
vars:
|
||||
foo: bar
|
||||
`),
|
||||
expectStdout: "",
|
||||
expectStderr: "\"hosts\" should be string or string array",
|
||||
},
|
||||
{
|
||||
name: "missing vars",
|
||||
args: []byte(`
|
||||
hosts: node1
|
||||
`),
|
||||
expectStdout: "",
|
||||
expectStderr: "\"vars\" should not be empty",
|
||||
},
|
||||
{
|
||||
name: "invalid hosts type",
|
||||
args: []byte(`
|
||||
hosts:
|
||||
foo: bar
|
||||
vars:
|
||||
a: b
|
||||
`),
|
||||
expectStdout: "",
|
||||
expectStderr: "\"hosts\" should be string or string array",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||||
defer cancel()
|
||||
opt := ExecOptions{
|
||||
Args: runtime.RawExtension{Raw: tc.args},
|
||||
Host: "",
|
||||
Variable: &testVariable{},
|
||||
Task: kkcorev1alpha1.Task{},
|
||||
Playbook: kkcorev1.Playbook{},
|
||||
}
|
||||
stdout, stderr := ModuleAddHostvars(ctx, opt)
|
||||
require.Equal(t, tc.expectStdout, stdout, "stdout mismatch")
|
||||
require.Equal(t, tc.expectStderr, stderr, "stderr mismatch")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -110,6 +110,9 @@ func newAssertArgs(_ context.Context, raw runtime.RawExtension, vars map[string]
|
|||
aa.successMsg = StdoutTrue
|
||||
}
|
||||
aa.failMsg, _ = variable.StringVar(vars, args, "fail_msg")
|
||||
if aa.failMsg == "" {
|
||||
aa.failMsg = StdoutFalse
|
||||
}
|
||||
aa.msg, _ = variable.StringVar(vars, args, "msg")
|
||||
if aa.msg == "" {
|
||||
aa.msg = StdoutFalse
|
||||
|
|
|
|||
|
|
@ -67,7 +67,7 @@ func ModuleSetFact(_ context.Context, options ExecOptions) (string, string) {
|
|||
if err := yaml.Unmarshal(options.Args.Raw, &node); err != nil {
|
||||
return "", fmt.Sprintf("failed to unmarshal YAML error: %v", err)
|
||||
}
|
||||
if err := options.Variable.Merge(variable.MergeAllRuntimeVariable(node, options.Host)); err != nil {
|
||||
if err := options.Variable.Merge(variable.MergeRuntimeVariable(node, options.Host)); err != nil {
|
||||
return "", fmt.Sprintf("set_fact error: %v", err)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -68,8 +68,8 @@ var MergeRuntimeVariable = func(node yaml.Node, hosts ...string) MergeFunc {
|
|||
}
|
||||
}
|
||||
|
||||
// MergeAllRuntimeVariable parse variable by specific host and merge to all hosts.
|
||||
var MergeAllRuntimeVariable = func(node yaml.Node, hostname string) MergeFunc {
|
||||
// MergeHostsRuntimeVariable parse variable by specific host and merge to given hosts.
|
||||
var MergeHostsRuntimeVariable = func(node yaml.Node, hostname string, hosts ...string) MergeFunc {
|
||||
if node.IsZero() {
|
||||
// skip
|
||||
return emptyMergeFunc
|
||||
|
|
@ -94,7 +94,7 @@ var MergeAllRuntimeVariable = func(node yaml.Node, hostname string) MergeFunc {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for h := range vv.value.Hosts {
|
||||
for _, h := range hosts {
|
||||
hv := vv.value.Hosts[h]
|
||||
hv.RuntimeVars = CombineVariables(hv.RuntimeVars, data)
|
||||
vv.value.Hosts[h] = hv
|
||||
|
|
|
|||
|
|
@ -107,58 +107,3 @@ func TestMergeRuntimeVariable(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMergeAllRuntimeVariable(t *testing.T) {
|
||||
testcases := []struct {
|
||||
name string
|
||||
variable *variable
|
||||
hostname string
|
||||
data map[string]any
|
||||
except value
|
||||
}{
|
||||
{
|
||||
name: "success",
|
||||
variable: &variable{
|
||||
source: source.NewMemorySource(),
|
||||
value: &value{
|
||||
Hosts: map[string]host{
|
||||
"n1": {},
|
||||
"n2": {},
|
||||
},
|
||||
},
|
||||
},
|
||||
hostname: "n1",
|
||||
data: map[string]any{
|
||||
"k1": "k2",
|
||||
},
|
||||
except: value{
|
||||
Hosts: map[string]host{
|
||||
"n1": {
|
||||
RuntimeVars: map[string]any{
|
||||
"k1": "k2",
|
||||
},
|
||||
},
|
||||
"n2": {
|
||||
RuntimeVars: map[string]any{
|
||||
"k1": "k2",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testcases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
node, err := converter.ConvertMap2Node(tc.data)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := tc.variable.Merge(MergeAllRuntimeVariable(node, tc.hostname)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
assert.Equal(t, tc.except, *tc.variable.value)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue