khuedoan-homelab/metal/roles/k3s/tasks/main.yml
2021-12-26 12:12:36 +07:00

86 lines
2.1 KiB
YAML

- 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
dest: /usr/local/bin/k3s
owner: root
group: root
mode: 0755
- name: Ensure config directories exist
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
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
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
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 }}"
mode: 0600
- name: Copy k3s config files
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
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
slurp:
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) }}"
dest: "{{ playbook_dir }}/kubeconfig.yaml"
mode: 0600
delegate_to: localhost
run_once: true