From 4c31a36ddea33e103542987a1902d74adecb35da Mon Sep 17 00:00:00 2001 From: Peter Klijn Date: Sat, 6 Jul 2024 19:19:19 +0200 Subject: [PATCH 1/2] Add a handler to restart the K3s Server when the service file changes Signed-off-by: Peter Klijn --- roles/k3s_server/handlers/main.yml | 6 ++++++ roles/k3s_server/tasks/main.yml | 4 ++++ 2 files changed, 10 insertions(+) create mode 100644 roles/k3s_server/handlers/main.yml diff --git a/roles/k3s_server/handlers/main.yml b/roles/k3s_server/handlers/main.yml new file mode 100644 index 000000000..fbecc0445 --- /dev/null +++ b/roles/k3s_server/handlers/main.yml @@ -0,0 +1,6 @@ +--- +- name: Restart K3s Server + ansible.builtin.systemd: + name: k3s + daemon_reload: true + state: restarted diff --git a/roles/k3s_server/tasks/main.yml b/roles/k3s_server/tasks/main.yml index baaa9abb0..f633f445c 100644 --- a/roles/k3s_server/tasks/main.yml +++ b/roles/k3s_server/tasks/main.yml @@ -65,6 +65,7 @@ owner: root group: root mode: "0644" + notify: Restart K3s Server - name: Copy K3s service file [HA] when: @@ -76,6 +77,7 @@ owner: root group: root mode: "0644" + notify: Restart K3s Server - name: Add service environment variables when: extra_service_envs is defined @@ -159,6 +161,7 @@ owner: root group: root mode: "0644" + notify: Restart K3s Server - name: Copy K3s service file [External DB] when: @@ -170,6 +173,7 @@ owner: root group: root mode: "0644" + notify: Restart K3s Server - name: Enable and check K3s service ansible.builtin.systemd: From 9325d9dda31cd5631111109c00450f332c3958af Mon Sep 17 00:00:00 2001 From: Peter Klijn Date: Sat, 13 Jul 2024 14:39:30 +0200 Subject: [PATCH 2/2] Refactored to tasks as per review comments Signed-off-by: Peter Klijn --- roles/k3s_server/handlers/main.yml | 6 ------ roles/k3s_server/tasks/main.yml | 30 ++++++++++++++++++++++++++---- 2 files changed, 26 insertions(+), 10 deletions(-) delete mode 100644 roles/k3s_server/handlers/main.yml diff --git a/roles/k3s_server/handlers/main.yml b/roles/k3s_server/handlers/main.yml deleted file mode 100644 index fbecc0445..000000000 --- a/roles/k3s_server/handlers/main.yml +++ /dev/null @@ -1,6 +0,0 @@ ---- -- name: Restart K3s Server - ansible.builtin.systemd: - name: k3s - daemon_reload: true - state: restarted diff --git a/roles/k3s_server/tasks/main.yml b/roles/k3s_server/tasks/main.yml index f633f445c..67362270a 100644 --- a/roles/k3s_server/tasks/main.yml +++ b/roles/k3s_server/tasks/main.yml @@ -65,7 +65,7 @@ owner: root group: root mode: "0644" - notify: Restart K3s Server + register: service_file_single - name: Copy K3s service file [HA] when: @@ -77,7 +77,7 @@ owner: root group: root mode: "0644" - notify: Restart K3s Server + register: service_file_ha - name: Add service environment variables when: extra_service_envs is defined @@ -86,7 +86,18 @@ line: "{{ item }}" with_items: "{{ extra_service_envs }}" + - name: Restart K3s service + when: + - ansible_facts.services['k3s.service'] is defined + - ansible_facts.services['k3s.service'].state == 'running' + - service_file_single.changed or service_file_ha.changed + ansible.builtin.systemd: + name: k3s + daemon_reload: true + state: restarted + - name: Enable and check K3s service + when: ansible_facts.services['k3s.service'] is not defined or ansible_facts.services['k3s.service'].state != 'running' ansible.builtin.systemd: name: k3s daemon_reload: true @@ -161,7 +172,7 @@ owner: root group: root mode: "0644" - notify: Restart K3s Server + register: service_file_ha - name: Copy K3s service file [External DB] when: @@ -173,9 +184,20 @@ owner: root group: root mode: "0644" - notify: Restart K3s Server + register: service_file_external_db + + - name: Restart K3s service + when: + - ansible_facts.services['k3s.service'] is defined + - ansible_facts.services['k3s.service'].state == 'running' + - service_file_ha.changed or service_file_external_db.changed + ansible.builtin.systemd: + name: k3s + daemon_reload: true + state: restarted - name: Enable and check K3s service + when: ansible_facts.services['k3s.service'] is not defined or ansible_facts.services['k3s.service'].state != 'running' ansible.builtin.systemd: name: k3s daemon_reload: true