Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve check mode support, add configs for agents, restart k3s.service when config changed #388

Merged
merged 1 commit into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions roles/k3s_agent/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
ignore_errors: true

- name: Set k3s installed version
when: k3s_version_output.rc == 0
when: not ansible_check_mode and k3s_version_output.rc == 0
ansible.builtin.set_fact:
installed_k3s_version: "{{ k3s_version_output.stdout_lines[0].split(' ')[2] }}"

Expand All @@ -15,7 +15,7 @@
# - we couldn't get k3s installed version in the first task of this role
# - the installed version of K3s on the nodes is older than the requested version in ansible vars
- name: Download artifact only if needed
when: airgap_dir is undefined and ( k3s_version_output.rc != 0 or installed_k3s_version is version(k3s_version, '<') )
when: not ansible_check_mode and airgap_dir is undefined and ( k3s_version_output.rc != 0 or installed_k3s_version is version(k3s_version, '<') )
block:
- name: Download K3s install script
ansible.builtin.get_url:
Expand All @@ -35,6 +35,21 @@
INSTALL_K3S_EXEC: "agent"
changed_when: true

- name: Setup optional config file
when: agent_config_yaml is defined
block:
- name: Make config directory
ansible.builtin.file:
path: "/etc/rancher/k3s"
mode: "0755"
state: directory
- name: Copy config values
ansible.builtin.copy:
content: "{{ agent_config_yaml }}"
dest: "/etc/rancher/k3s/config.yaml"
mode: "0644"
register: _agent_config_result

- name: Get the token from the first server
ansible.builtin.set_fact:
token: "{{ hostvars[groups[server_group][0]].token }}"
Expand Down Expand Up @@ -66,5 +81,5 @@
ansible.builtin.systemd:
name: k3s-agent
daemon_reload: "{{ true if k3s_agent_service.changed else false }}"
state: "{{ 'restarted' if k3s_agent_service.changed else 'started' }}"
state: "{{ 'restarted' if (k3s_agent_service.changed or _agent_config_result.changed) else 'started' }}"
enabled: true
11 changes: 6 additions & 5 deletions roles/k3s_server/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
ignore_errors: true

- name: Set k3s installed version
when: k3s_version_output.rc == 0
when: not ansible_check_mode and k3s_version_output.rc == 0
ansible.builtin.set_fact:
installed_k3s_version: "{{ k3s_version_output.stdout_lines[0].split(' ')[2] }}"

Expand All @@ -15,7 +15,7 @@
# - we couldn't get k3s installed version in the first task of this role
# - the installed version of K3s on the nodes is older than the requested version in ansible vars
- name: Download artifact only if needed
when: airgap_dir is undefined and ( k3s_version_output.rc != 0 or installed_k3s_version is version(k3s_version, '<') )
when: not ansible_check_mode and airgap_dir is undefined and ( k3s_version_output.rc != 0 or installed_k3s_version is version(k3s_version, '<') )
block:
- name: Download K3s install script
ansible.builtin.get_url:
Expand Down Expand Up @@ -53,6 +53,7 @@
content: "{{ server_config_yaml }}"
dest: "/etc/rancher/k3s/config.yaml"
mode: "0644"
register: _server_config_result

- name: Init first server node
when: inventory_hostname == groups[server_group][0]
Expand Down Expand Up @@ -105,7 +106,7 @@
when:
- ansible_facts.services['k3s.service'] is defined
- ansible_facts.services['k3s.service'].state == 'running'
- service_file_single.changed or service_file_ha.changed
- service_file_single.changed or service_file_ha.changed or _server_config_result.changed
ansible.builtin.systemd:
name: k3s
daemon_reload: true
Expand Down Expand Up @@ -247,7 +248,7 @@
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
- service_file_ha.changed or service_file_external_db.changed or _server_config_result.changed
ansible.builtin.systemd:
name: k3s
daemon_reload: true
Expand All @@ -262,7 +263,7 @@
enabled: true

- name: Verify that all server nodes joined
when: (groups[server_group] | length) > 1
when: not ansible_check_mode and (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}"
Expand Down
Loading