feat(roles): added k3s role

This commit is contained in:
ITQ
2026-03-19 14:41:05 +03:00
parent 3f5f2c4f18
commit 8a9cc4a1ce
7 changed files with 112 additions and 10 deletions
-10
View File
@@ -1,10 +0,0 @@
---
- name: Docker Setup
hosts: servers
gather_facts: true
become: true
serial: "100%"
roles:
- role: geerlingguy.docker
tags: docker
+2
View File
@@ -0,0 +1,2 @@
---
extra_agent_args: ""
+7
View File
@@ -0,0 +1,7 @@
---
kubeconfig: ~/.kube/config.new
user_kubectl: false
cluster_context: k3s-ansible
use_external_database: false
extra_server_args: ""
+18
View File
@@ -0,0 +1,18 @@
---
api_port: 6443
server_group: k3s_servers
agent_group: k3s_agents
systemd_dir: /etc/systemd/system
k3s_server_location: /var/lib/rancher/k3s
extra_install_envs: {}
extra_service_envs: []
k3s_version: v1.34.1+k3s1
# Set via ANSIBLE_K3S_TOKEN env var or override per-host with ansible-vault
# Generate with: openssl rand -base64 48
token: "{{ lookup('env', 'ANSIBLE_K3S_TOKEN') }}"
api_endpoint: "{{ hostvars[groups['server'][0]]['ansible_host'] | default(groups['server'][0]) }}"
+3
View File
@@ -8,6 +8,9 @@ collections:
version: 4.8.1
- name: artis3n.tailscale
version: 1.1.0
- name: https://github.com/k3s-io/k3s-ansible.git
type: git
version: main
roles:
- src: geerlingguy.docker
+2
View File
@@ -0,0 +1,2 @@
---
k3s_state: installed
+80
View File
@@ -0,0 +1,80 @@
---
- name: Validate k3s_state parameter
ansible.builtin.assert:
that:
- k3s_state in ['installed', 'latest', 'absent']
fail_msg: "k3s_state must be one of: installed, latest, absent"
quiet: true
tags: always
- name: Check if k3s binary exists
ansible.builtin.stat:
path: /usr/local/bin/k3s
register: _k3s_binary
tags: always
- name: Set k3s installation fact
ansible.builtin.set_fact:
_k3s_installed: "{{ _k3s_binary.stat.exists }}"
tags: always
- name: Display k3s state
ansible.builtin.debug:
msg:
- "Desired state: {{ k3s_state }}"
- "Currently installed: {{ _k3s_installed }}"
verbosity: 1
tags: always
# The actual k3s installation/management is handled by the external
# k3s-io/k3s-ansible collection (k3s.orchestration.k3s_server) which
# is invoked directly from containers.yaml. This role only provides
# detection and state validation as a lightweight wrapper.
- name: Uninstall k3s
when:
- k3s_state == 'absent'
- _k3s_installed | bool
tags: k3s, uninstall
block:
- name: Run k3s uninstall script
ansible.builtin.command:
cmd: /usr/local/bin/k3s-uninstall.sh
removes: /usr/local/bin/k3s-uninstall.sh
register: _k3s_uninstall
changed_when: _k3s_uninstall.rc == 0
- name: Run k3s agent uninstall script (if present)
ansible.builtin.command:
cmd: /usr/local/bin/k3s-agent-uninstall.sh
removes: /usr/local/bin/k3s-agent-uninstall.sh
register: _k3s_agent_uninstall
changed_when: _k3s_agent_uninstall.rc == 0
- name: Verify k3s binary is removed
ansible.builtin.stat:
path: /usr/local/bin/k3s
register: _k3s_post_uninstall
- name: Confirm k3s removal
ansible.builtin.assert:
that:
- not _k3s_post_uninstall.stat.exists
fail_msg: "k3s binary still exists after uninstall"
quiet: true
- name: Show skip message when already installed
ansible.builtin.debug:
msg: "k3s is already installed — use the k3s.orchestration collection for upgrades"
when:
- k3s_state in ['installed', 'latest']
- _k3s_installed | bool
tags: k3s
- name: Show skip message when not installed and absent requested
ansible.builtin.debug:
msg: "k3s is not installed — nothing to remove"
when:
- k3s_state == 'absent'
- not (_k3s_installed | bool)
tags: k3s