From 122d6a11ac5c5aa2bab740c9fc0d5e1751fd765c Mon Sep 17 00:00:00 2001 From: The Witch Date: Sun, 19 May 2024 20:22:18 -0400 Subject: [PATCH 1/2] Make agent and server groups configurable Signed-off-by: Meagan Harris --- roles/k3s_server/defaults/main.yml | 2 ++ roles/k3s_server/tasks/main.yml | 18 +++++++++--------- roles/k3s_upgrade/defaults/main.yml | 2 ++ roles/k3s_upgrade/tasks/main.yml | 4 ++-- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/roles/k3s_server/defaults/main.yml b/roles/k3s_server/defaults/main.yml index 2ce9352ac..ed9245d01 100644 --- a/roles/k3s_server/defaults/main.yml +++ b/roles/k3s_server/defaults/main.yml @@ -5,3 +5,5 @@ api_port: 6443 # noqa var-naming[no-role-prefix] kubeconfig: ~/.kube/config.new # noqa var-naming[no-role-prefix] user_kubectl: true # noqa var-naming[no-role-prefix] cluster_context: k3s-ansible # noqa var-naming[no-role-prefix] +server_group: server # noqa var-naming[no-role-prefix] +agent_group: agent # noqa var-naming[no-role-prefix] diff --git a/roles/k3s_server/tasks/main.yml b/roles/k3s_server/tasks/main.yml index 0b38616e5..8b9c86883 100644 --- a/roles/k3s_server/tasks/main.yml +++ b/roles/k3s_server/tasks/main.yml @@ -55,10 +55,10 @@ mode: 0644 - name: Init first server node - when: inventory_hostname == groups['server'][0] + when: inventory_hostname == groups[server_group][0] block: - name: Copy K3s service file [Single] - when: groups['server'] | length == 1 + when: groups[server_group] | length == 1 ansible.builtin.template: src: "k3s-single.service.j2" dest: "{{ systemd_dir }}/k3s.service" @@ -67,7 +67,7 @@ mode: 0644 - name: Copy K3s service file [HA] - when: groups['server'] | length > 1 + when: groups[server_group] | length > 1 ansible.builtin.template: src: "k3s-cluster-init.service.j2" dest: "{{ systemd_dir }}/k3s.service" @@ -90,7 +90,7 @@ enabled: true - name: Pause to allow first server startup - when: (groups['server'] | length) > 1 + when: (groups[server_group] | length) > 1 ansible.builtin.pause: seconds: 10 @@ -144,11 +144,11 @@ - name: Start other server if any and verify status when: - - (groups['server'] | length) > 1 - - inventory_hostname != groups['server'][0] + - (groups[server_group] | length) > 1 + - inventory_hostname != groups[server_group][0] block: - name: Copy K3s service file [HA] - when: groups['server'] | length > 1 + when: groups[server_group] | length > 1 ansible.builtin.template: src: "k3s-ha.service.j2" dest: "{{ systemd_dir }}/k3s.service" @@ -164,12 +164,12 @@ enabled: true - name: Verify that all server nodes joined - when: (groups['server'] | length) > 1 + when: (groups[server_group] | length) > 1 ansible.builtin.command: cmd: > k3s kubectl get nodes -l "node-role.kubernetes.io/control-plane=true" -o=jsonpath="{.items[*].metadata.name}" register: nodes - until: nodes.rc == 0 and (nodes.stdout.split() | length) == (groups['server'] | length) + until: nodes.rc == 0 and (nodes.stdout.split() | length) == (groups[server_group] | length) retries: 20 delay: 10 changed_when: false diff --git a/roles/k3s_upgrade/defaults/main.yml b/roles/k3s_upgrade/defaults/main.yml index 59e7e4681..b6c802626 100644 --- a/roles/k3s_upgrade/defaults/main.yml +++ b/roles/k3s_upgrade/defaults/main.yml @@ -1,2 +1,4 @@ --- systemd_dir: /etc/systemd/system # noqa var-naming[no-role-prefix] +server_group: server # noqa var-naming[no-role-prefix] +agent_group: agent # noqa var-naming[no-role-prefix] diff --git a/roles/k3s_upgrade/tasks/main.yml b/roles/k3s_upgrade/tasks/main.yml index 6c1d82d3a..69dd11244 100644 --- a/roles/k3s_upgrade/tasks/main.yml +++ b/roles/k3s_upgrade/tasks/main.yml @@ -54,14 +54,14 @@ loop: "{{ k3s_service_files.files }}" - name: Restart K3s service [server] - when: "'server' in group_names" + when: "server_group in group_names" ansible.builtin.systemd: state: restarted daemon_reload: true name: k3s - name: Restart K3s service [agent] - when: "'agent' in group_names" + when: "server_agent in group_names" ansible.builtin.systemd: state: restarted daemon_reload: true From e45d6ff5bff2ea970cec8ba4c7979e5b89d31d5f Mon Sep 17 00:00:00 2001 From: Meagan Harris <47128741+simagick@users.noreply.github.com> Date: Tue, 4 Jun 2024 12:37:00 -0400 Subject: [PATCH 2/2] Fix typo in upgrade role Co-authored-by: Derek Nola Signed-off-by: Meagan Harris <47128741+simagick@users.noreply.github.com> --- roles/k3s_upgrade/tasks/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/roles/k3s_upgrade/tasks/main.yml b/roles/k3s_upgrade/tasks/main.yml index 69dd11244..a977c5795 100644 --- a/roles/k3s_upgrade/tasks/main.yml +++ b/roles/k3s_upgrade/tasks/main.yml @@ -61,7 +61,7 @@ name: k3s - name: Restart K3s service [agent] - when: "server_agent in group_names" + when: "agent_group in group_names" ansible.builtin.systemd: state: restarted daemon_reload: true