khuedoan-homelab/metal/roles/k3s/tasks/main.yml

82 lines
2.1 KiB
YAML
Raw Normal View History

2021-09-01 04:19:05 +07:00
- name: Download k3s binary
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"
delegate_to: localhost
run_once: true
register: k3s_binary
- name: Copy k3s binary to nodes
copy:
src: bin/k3s
2021-09-01 04:19:05 +07:00
dest: /usr/local/bin/k3s
owner: root
group: root
mode: 0755
- name: Ensure config directories exist
file:
path: "{{ item }}"
state: directory
loop:
- /etc/rancher/k3s
- /etc/rancher/node
- name: Check if k3s token file exists on the first node
run_once: yes
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: yes
when: not k3s_token_file_stat.stat.exists
copy:
content: "{{ lookup('community.general.random_string', length=32) }}"
2021-09-01 04:19:05 +07:00
dest: "{{ k3s_token_file }}"
- name: Get k3s token from the first node
run_once: yes
slurp:
src: "{{ k3s_token_file }}"
register: k3s_token_base64
- name: Ensure all nodes has the same token
copy:
content: "{{ k3s_token_base64.content | b64decode }}"
dest: "{{ k3s_token_file }}"
2021-09-01 04:58:47 +07:00
- name: Copy k3s config files
2021-09-01 04:19:05 +07:00
template:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
loop:
- src: config.yaml.j2
dest: "{{ k3s_config_file }}"
- src: k3s.service.j2
dest: "{{ k3s_service_file }}"
- name: Enable k3s service
systemd:
name: k3s
enabled: yes
state: started
register: k3s_service
until: k3s_service is succeeded
retries: 5
2021-09-01 04:19:05 +07:00
2021-09-01 05:43:35 +07:00
- name: Get Kubernetes config file
run_once: yes
slurp:
2021-09-01 05:43:35 +07:00
src: /etc/rancher/k3s/k3s.yaml
register: kubeconfig_base64
- name: Write Kubernetes config file with the correct cluster address
copy:
content: "{{ kubeconfig_base64.content | b64decode | replace('127.0.0.1', hostvars[groups['masters'][0]].ansible_host) }}"
2021-09-01 05:43:35 +07:00
dest: "{{ playbook_dir }}/kubeconfig.yaml"
mode: 0600
delegate_to: localhost
run_once: true