mirror of
https://github.com/khuedoan/homelab.git
synced 2025-01-24 18:06:16 +07:00
87 lines
2.3 KiB
YAML
87 lines
2.3 KiB
YAML
- name: Download k3s binary
|
|
ansible.builtin.get_url:
|
|
url: https://github.com/k3s-io/k3s/releases/download/{{ k3s_version }}/k3s
|
|
checksum: sha256:https://github.com/k3s-io/k3s/releases/download/{{ k3s_version }}/sha256sum-amd64.txt
|
|
dest: "{{ role_path }}/files/bin/k3s"
|
|
mode: 0755
|
|
delegate_to: localhost
|
|
run_once: true
|
|
register: k3s_binary
|
|
|
|
- name: Copy k3s binary to nodes
|
|
ansible.builtin.copy:
|
|
src: bin/k3s
|
|
dest: /usr/local/bin/k3s
|
|
owner: root
|
|
group: root
|
|
mode: 0755
|
|
|
|
- name: Ensure config directories exist
|
|
ansible.builtin.file:
|
|
path: "{{ item }}"
|
|
state: directory
|
|
mode: 0755
|
|
loop:
|
|
- /etc/rancher/k3s
|
|
- /etc/rancher/node
|
|
|
|
- name: Check if k3s token file exists on the first node
|
|
run_once: true
|
|
ansible.builtin.stat:
|
|
path: "{{ k3s_token_file }}"
|
|
register: k3s_token_file_stat
|
|
|
|
- name: Generate k3s token file on the first node if not exist yet
|
|
run_once: true
|
|
when: not k3s_token_file_stat.stat.exists
|
|
ansible.builtin.copy:
|
|
content: "{{ lookup('community.general.random_string', length=32) }}"
|
|
dest: "{{ k3s_token_file }}"
|
|
mode: 0600
|
|
|
|
- name: Get k3s token from the first node
|
|
run_once: true
|
|
ansible.builtin.slurp:
|
|
src: "{{ k3s_token_file }}"
|
|
register: k3s_token_base64
|
|
|
|
- name: Ensure all nodes has the same token
|
|
ansible.builtin.copy:
|
|
content: "{{ k3s_token_base64.content | b64decode }}"
|
|
dest: "{{ k3s_token_file }}"
|
|
mode: 0600
|
|
|
|
- name: Copy k3s config files
|
|
ansible.builtin.template:
|
|
src: "{{ item.src }}"
|
|
dest: "{{ item.dest }}"
|
|
mode: 0644
|
|
loop:
|
|
- src: config.yaml.j2
|
|
dest: "{{ k3s_config_file }}"
|
|
- src: k3s.service.j2
|
|
dest: "{{ k3s_service_file }}"
|
|
|
|
- name: Enable k3s service
|
|
ansible.builtin.systemd:
|
|
name: k3s
|
|
enabled: true
|
|
state: started
|
|
register: k3s_service
|
|
until: k3s_service is succeeded
|
|
retries: 5
|
|
|
|
- name: Get Kubernetes config file
|
|
run_once: true
|
|
ansible.builtin.slurp:
|
|
src: /etc/rancher/k3s/k3s.yaml
|
|
register: kubeconfig_base64
|
|
|
|
- name: Write Kubernetes config file with the correct cluster address
|
|
ansible.builtin.copy:
|
|
content: "{{ kubeconfig_base64.content | b64decode | replace('127.0.0.1', hostvars[groups['masters'][0]].ansible_host) }}"
|
|
dest: "{{ playbook_dir }}/kubeconfig.yaml"
|
|
mode: 0600
|
|
delegate_to: localhost
|
|
run_once: true
|