aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorclyhtsuriva <aimeric@adjutor.xyz>2025-03-04 20:38:28 +0100
committerclyhtsuriva <aimeric@adjutor.xyz>2025-03-04 20:38:28 +0100
commit885aa73ca70a9fc5b8f3b77b5cf4476554d1415a (patch)
treee73a211eb82e77573ed170ee729b32e4bb400553
parente02cf37430f92859a0f4b644af6d2665847c3997 (diff)
ansible: Make the k3s installations idempotent
-rw-r--r--ansible/roles/k8s/tasks/install_k3s_master.yml28
-rw-r--r--ansible/roles/k8s/tasks/install_k3s_worker.yml27
2 files changed, 47 insertions, 8 deletions
diff --git a/ansible/roles/k8s/tasks/install_k3s_master.yml b/ansible/roles/k8s/tasks/install_k3s_master.yml
index 71d880e..dfb416f 100644
--- a/ansible/roles/k8s/tasks/install_k3s_master.yml
+++ b/ansible/roles/k8s/tasks/install_k3s_master.yml
@@ -1,20 +1,40 @@
---
-- name: Download k3s installer
+- name: Ensure /opt/k3s directory exists
+ ansible.builtin.file:
+ path: /opt/k3s
+ state: directory
+ mode: '0755'
+
+- name: Check if k3s installer script already exists
+ ansible.builtin.stat:
+ path: /opt/k3s/install_k3s.sh
+ register: k3s_installer_script
+
+- name: Download k3s installer if not already present
ansible.builtin.get_url:
url: https://get.k3s.io
- dest: /tmp/install_k3s.sh
+ dest: /opt/k3s/install_k3s.sh
mode: '0755'
+ when: not k3s_installer_script.stat.exists
+
+- name: Check if k3s is already installed (master)
+ ansible.builtin.stat:
+ path: /var/lib/rancher/k3s/server/node-token
+ register: k3s_installed
-- name: Install k3s server
- ansible.builtin.command: /tmp/install_k3s.sh server --cluster-init
+- name: Install k3s server if not already installed
+ ansible.builtin.command: /opt/k3s/install_k3s.sh server --cluster-init
become: true
+ when: not k3s_installed.stat.exists
- name: Retrieve k3s token
ansible.builtin.slurp:
path: /var/lib/rancher/k3s/server/node-token
register: k3s_token_file
+ when: not k3s_installed.stat.exists
- name: Set k3s token as a fact, for workers
ansible.builtin.set_fact:
k3s_token: "{{ k3s_token_file.content | b64decode }}"
+ when: k3s_token_file is defined and k3s_token_file.content is defined
...
diff --git a/ansible/roles/k8s/tasks/install_k3s_worker.yml b/ansible/roles/k8s/tasks/install_k3s_worker.yml
index ffe2af0..b550b88 100644
--- a/ansible/roles/k8s/tasks/install_k3s_worker.yml
+++ b/ansible/roles/k8s/tasks/install_k3s_worker.yml
@@ -3,17 +3,36 @@
ansible.builtin.set_fact:
k3s_token: "{{ hostvars[groups['tag_k3s_master'][0]].k3s_token }}"
k3s_master_ip: "{{ hostvars[groups['tag_k3s_master'][0]].ansible_default_ipv4.address }}"
+ when: hostvars[groups['tag_k3s_master'][0]].k3s_token is defined
-- name: Download k3s installation script
+- name: Ensure /opt/k3s directory exists
+ ansible.builtin.file:
+ path: /opt/k3s
+ state: directory
+ mode: '0755'
+
+- name: Check if k3s installer script already exists
+ ansible.builtin.stat:
+ path: /opt/k3s/install_k3s.sh
+ register: k3s_installer_script
+
+- name: Download k3s installation script if not already present
ansible.builtin.get_url:
url: https://get.k3s.io
- dest: /tmp/install_k3s.sh
+ dest: /opt/k3s/install_k3s.sh
mode: '0755'
+ when: not k3s_installer_script.stat.exists
+
+- name: Check if k3s agent is already installed
+ ansible.builtin.stat:
+ path: /var/lib/rancher/k3s/agent
+ register: k3s_agent_installed
-- name: Install k3s agent
+- name: Install k3s agent if not already installed
ansible.builtin.command: >
- /tmp/install_k3s.sh agent
+ /opt/k3s/install_k3s.sh agent
--server https://{{ k3s_master_ip }}:6443
--token {{ k3s_token }}
become: true
+ when: not k3s_agent_installed.stat.exists
...