From a1c47b1b20fa107d546836353316b8ecebc01a77 Mon Sep 17 00:00:00 2001
From: Max Gautier <mg@max.gautier.name>
Date: Mon, 4 Dec 2023 23:24:00 +0100
Subject: [PATCH] Factorize some identical playbooks steps into their own
 sub-playbooks (#10633)

* Factorize identical playboooks steps in sub-playbooks

* Copy legacy_groups.yml into its sole user
---
 .../{legacy_groups.yml => boilerplate.yml}    | 13 +++++-
 playbooks/cluster.yml                         | 45 ++-----------------
 playbooks/install_etcd.yml                    | 31 +++++++++++++
 playbooks/recover_control_plane.yml           | 15 +------
 playbooks/remove_node.yml                     | 15 +------
 playbooks/reset.yml                           | 15 +------
 playbooks/scale.yml                           | 15 +------
 playbooks/upgrade_cluster.yml                 | 45 ++-----------------
 8 files changed, 57 insertions(+), 137 deletions(-)
 rename playbooks/{legacy_groups.yml => boilerplate.yml} (71%)
 create mode 100644 playbooks/install_etcd.yml

diff --git a/playbooks/legacy_groups.yml b/playbooks/boilerplate.yml
similarity index 71%
rename from playbooks/legacy_groups.yml
rename to playbooks/boilerplate.yml
index 643032ff0..137a4c2c5 100644
--- a/playbooks/legacy_groups.yml
+++ b/playbooks/boilerplate.yml
@@ -1,5 +1,8 @@
 ---
-# This is an inventory compatibility playbook to ensure we keep compatibility with old style group names
+- name: Check ansible version
+  import_playbook: ansible_version.yml
+
+# These are inventory compatibility tasks to ensure we keep compatibility with old style group names
 
 - name: Add kube-master nodes to kube_control_plane
   hosts: kube-master
@@ -45,3 +48,11 @@
     - name: Add nodes to no-floating group
       group_by:
         key: 'no_floating'
+
+- name: Install bastion ssh config
+  hosts: bastion[0]
+  gather_facts: False
+  environment: "{{ proxy_disable_env }}"
+  roles:
+    - { role: kubespray-defaults }
+    - { role: bastion-ssh-config, tags: ["localhost", "bastion"] }
diff --git a/playbooks/cluster.yml b/playbooks/cluster.yml
index 85a483a3c..a6fd770b9 100644
--- a/playbooks/cluster.yml
+++ b/playbooks/cluster.yml
@@ -1,17 +1,6 @@
 ---
-- name: Check ansible version
-  import_playbook: ansible_version.yml
-
-- name: Ensure compatibility with old groups
-  import_playbook: legacy_groups.yml
-
-- name: Install bastion ssh config
-  hosts: bastion[0]
-  gather_facts: False
-  environment: "{{ proxy_disable_env }}"
-  roles:
-    - { role: kubespray-defaults }
-    - { role: bastion-ssh-config, tags: ["localhost", "bastion"] }
+- name: Common tasks for every playbooks
+  import_playbook: boilerplate.yml
 
 - name: Gather facts
   import_playbook: facts.yml
@@ -28,35 +17,7 @@
     - { role: download, tags: download, when: "not skip_downloads" }
 
 - name: Install etcd
-  hosts: etcd:kube_control_plane
-  gather_facts: False
-  any_errors_fatal: "{{ any_errors_fatal | default(true) }}"
-  environment: "{{ proxy_disable_env }}"
-  roles:
-    - { role: kubespray-defaults }
-    - role: etcd
-      tags: etcd
-      vars:
-        etcd_cluster_setup: true
-        etcd_events_cluster_setup: "{{ etcd_events_cluster_enabled }}"
-      when: etcd_deployment_type != "kubeadm"
-
-- name: Install etcd certs on nodes if required
-  hosts: k8s_cluster
-  gather_facts: False
-  any_errors_fatal: "{{ any_errors_fatal | default(true) }}"
-  environment: "{{ proxy_disable_env }}"
-  roles:
-    - { role: kubespray-defaults }
-    - role: etcd
-      tags: etcd
-      vars:
-        etcd_cluster_setup: false
-        etcd_events_cluster_setup: false
-      when:
-        - etcd_deployment_type != "kubeadm"
-        - kube_network_plugin in ["calico", "flannel", "canal", "cilium"] or cilium_deploy_additionally | default(false) | bool
-        - kube_network_plugin != "calico" or calico_datastore == "etcd"
+  import_playbook: install_etcd.yml
 
 - name: Install Kubernetes nodes
   hosts: k8s_cluster
diff --git a/playbooks/install_etcd.yml b/playbooks/install_etcd.yml
new file mode 100644
index 000000000..9bd13dfcd
--- /dev/null
+++ b/playbooks/install_etcd.yml
@@ -0,0 +1,31 @@
+---
+- name: Install etcd
+  hosts: etcd:kube_control_plane
+  gather_facts: False
+  any_errors_fatal: "{{ any_errors_fatal | default(true) }}"
+  environment: "{{ proxy_disable_env }}"
+  roles:
+    - { role: kubespray-defaults }
+    - role: etcd
+      tags: etcd
+      vars:
+        etcd_cluster_setup: true
+        etcd_events_cluster_setup: "{{ etcd_events_cluster_enabled }}"
+      when: etcd_deployment_type != "kubeadm"
+
+- name: Install etcd certs on nodes if required
+  hosts: k8s_cluster
+  gather_facts: False
+  any_errors_fatal: "{{ any_errors_fatal | default(true) }}"
+  environment: "{{ proxy_disable_env }}"
+  roles:
+    - { role: kubespray-defaults }
+    - role: etcd
+      tags: etcd
+      vars:
+        etcd_cluster_setup: false
+        etcd_events_cluster_setup: false
+      when:
+        - etcd_deployment_type != "kubeadm"
+        - kube_network_plugin in ["calico", "flannel", "canal", "cilium"] or cilium_deploy_additionally | default(false) | bool
+        - kube_network_plugin != "calico" or calico_datastore == "etcd"
diff --git a/playbooks/recover_control_plane.yml b/playbooks/recover_control_plane.yml
index d2bb57427..35ed2c276 100644
--- a/playbooks/recover_control_plane.yml
+++ b/playbooks/recover_control_plane.yml
@@ -1,17 +1,6 @@
 ---
-- name: Check ansible version
-  import_playbook: ansible_version.yml
-
-- name: Ensure compatibility with old groups
-  import_playbook: legacy_groups.yml
-
-- name: Install bastion ssh config
-  hosts: bastion[0]
-  gather_facts: False
-  environment: "{{ proxy_disable_env }}"
-  roles:
-    - { role: kubespray-defaults}
-    - { role: bastion-ssh-config, tags: ["localhost", "bastion"]}
+- name: Common tasks for every playbooks
+  import_playbook: boilerplate.yml
 
 - name: Recover etcd
   hosts: etcd[0]
diff --git a/playbooks/remove_node.yml b/playbooks/remove_node.yml
index 63df85938..e01338965 100644
--- a/playbooks/remove_node.yml
+++ b/playbooks/remove_node.yml
@@ -1,17 +1,6 @@
 ---
-- name: Check ansible version
-  import_playbook: ansible_version.yml
-
-- name: Ensure compatibility with old groups
-  import_playbook: legacy_groups.yml
-
-- name: Install bastion ssh config
-  hosts: bastion[0]
-  gather_facts: False
-  environment: "{{ proxy_disable_env }}"
-  roles:
-    - { role: kubespray-defaults }
-    - { role: bastion-ssh-config, tags: ["localhost", "bastion"] }
+- name: Common tasks for every playbooks
+  import_playbook: boilerplate.yml
 
 - name: Confirm node removal
   hosts: "{{ node | default('etcd:k8s_cluster:calico_rr') }}"
diff --git a/playbooks/reset.yml b/playbooks/reset.yml
index 0b4312fbd..5742bd844 100644
--- a/playbooks/reset.yml
+++ b/playbooks/reset.yml
@@ -1,17 +1,6 @@
 ---
-- name: Check ansible version
-  import_playbook: ansible_version.yml
-
-- name: Ensure compatibility with old groups
-  import_playbook: legacy_groups.yml
-
-- name: Install bastion ssh config
-  hosts: bastion[0]
-  gather_facts: False
-  environment: "{{ proxy_disable_env }}"
-  roles:
-    - { role: kubespray-defaults}
-    - { role: bastion-ssh-config, tags: ["localhost", "bastion"]}
+- name: Common tasks for every playbooks
+  import_playbook: boilerplate.yml
 
 - name: Gather facts
   import_playbook: facts.yml
diff --git a/playbooks/scale.yml b/playbooks/scale.yml
index aaa1bf584..b8f87f484 100644
--- a/playbooks/scale.yml
+++ b/playbooks/scale.yml
@@ -1,17 +1,6 @@
 ---
-- name: Check ansible version
-  import_playbook: ansible_version.yml
-
-- name: Ensure compatibility with old groups
-  import_playbook: legacy_groups.yml
-
-- name: Install bastion ssh config
-  hosts: bastion[0]
-  gather_facts: False
-  environment: "{{ proxy_disable_env }}"
-  roles:
-    - { role: kubespray-defaults }
-    - { role: bastion-ssh-config, tags: ["localhost", "bastion"] }
+- name: Common tasks for every playbooks
+  import_playbook: boilerplate.yml
 
 - name: Gather facts
   import_playbook: facts.yml
diff --git a/playbooks/upgrade_cluster.yml b/playbooks/upgrade_cluster.yml
index 3e2125c83..a79cf0aa7 100644
--- a/playbooks/upgrade_cluster.yml
+++ b/playbooks/upgrade_cluster.yml
@@ -1,17 +1,6 @@
 ---
-- name: Check ansible version
-  import_playbook: ansible_version.yml
-
-- name: Ensure compatibility with old groups
-  import_playbook: legacy_groups.yml
-
-- name: Install bastion ssh config
-  hosts: bastion[0]
-  gather_facts: False
-  environment: "{{ proxy_disable_env }}"
-  roles:
-    - { role: kubespray-defaults }
-    - { role: bastion-ssh-config, tags: ["localhost", "bastion"] }
+- name: Common tasks for every playbooks
+  import_playbook: boilerplate.yml
 
 - name: Gather facts
   import_playbook: facts.yml
@@ -47,35 +36,7 @@
     - { role: container-engine, tags: "container-engine", when: deploy_container_engine }
 
 - name: Install etcd
-  hosts: etcd:kube_control_plane
-  gather_facts: False
-  any_errors_fatal: "{{ any_errors_fatal | default(true) }}"
-  environment: "{{ proxy_disable_env }}"
-  roles:
-    - { role: kubespray-defaults }
-    - role: etcd
-      tags: etcd
-      vars:
-        etcd_cluster_setup: true
-        etcd_events_cluster_setup: "{{ etcd_events_cluster_enabled }}"
-      when: etcd_deployment_type != "kubeadm"
-
-- name: Install etcd certs on nodes if required
-  hosts: k8s_cluster
-  gather_facts: False
-  any_errors_fatal: "{{ any_errors_fatal | default(true) }}"
-  environment: "{{ proxy_disable_env }}"
-  roles:
-    - { role: kubespray-defaults }
-    - role: etcd
-      tags: etcd
-      vars:
-        etcd_cluster_setup: false
-        etcd_events_cluster_setup: false
-      when:
-        - etcd_deployment_type != "kubeadm"
-        - kube_network_plugin in ["calico", "flannel", "canal", "cilium"] or cilium_deploy_additionally | default(false) | bool
-        - kube_network_plugin != "calico" or calico_datastore == "etcd"
+  import_playbook: install_etcd.yml
 
 - name: Handle upgrades to master components first to maintain backwards compat.
   gather_facts: False
-- 
GitLab