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

87 lines
2.3 KiB
YAML
Raw Normal View History

2021-09-01 04:19:05 +07:00
- name: Download k3s binary
2022-05-14 21:24:39 +07:00
ansible.builtin.get_url:
2021-09-01 04:19:05 +07:00
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"
2022-05-14 21:24:39 +07:00
mode: 0755
delegate_to: localhost
run_once: true
register: k3s_binary
- name: Copy k3s binary to nodes
2022-05-14 21:24:39 +07:00
ansible.builtin.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
2022-05-14 21:24:39 +07:00
ansible.builtin.file:
2021-09-01 04:19:05 +07:00
path: "{{ item }}"
state: directory
2021-12-26 12:12:36 +07:00
mode: 0755
2021-09-01 04:19:05 +07:00
loop:
- /etc/rancher/k3s
- /etc/rancher/node
- name: Check if k3s token file exists on the first node
2021-12-26 12:12:36 +07:00
run_once: true
2022-05-14 21:24:39 +07:00
ansible.builtin.stat:
2021-09-01 04:19:05 +07:00
path: "{{ k3s_token_file }}"
register: k3s_token_file_stat
- name: Generate k3s token file on the first node if not exist yet
2021-12-26 12:12:36 +07:00
run_once: true
2021-09-01 04:19:05 +07:00
when: not k3s_token_file_stat.stat.exists
2022-05-14 21:24:39 +07:00
ansible.builtin.copy:
content: "{{ lookup('community.general.random_string', length=32) }}"
2021-09-01 04:19:05 +07:00
dest: "{{ k3s_token_file }}"
2021-12-26 12:12:36 +07:00
mode: 0600
2021-09-01 04:19:05 +07:00
- name: Get k3s token from the first node
2021-12-26 12:12:36 +07:00
run_once: true
2022-05-14 21:24:39 +07:00
ansible.builtin.slurp:
2021-09-01 04:19:05 +07:00
src: "{{ k3s_token_file }}"
register: k3s_token_base64
- name: Ensure all nodes has the same token
2022-05-14 21:24:39 +07:00
ansible.builtin.copy:
2021-09-01 04:19:05 +07:00
content: "{{ k3s_token_base64.content | b64decode }}"
dest: "{{ k3s_token_file }}"
2021-12-26 12:12:36 +07:00
mode: 0600
2021-09-01 04:19:05 +07:00
2021-09-01 04:58:47 +07:00
- name: Copy k3s config files
2022-05-14 21:24:39 +07:00
ansible.builtin.template:
2021-09-01 04:19:05 +07:00
src: "{{ item.src }}"
dest: "{{ item.dest }}"
2021-12-26 12:12:36 +07:00
mode: 0644
2021-09-01 04:19:05 +07:00
loop:
- src: config.yaml.j2
dest: "{{ k3s_config_file }}"
- src: k3s.service.j2
dest: "{{ k3s_service_file }}"
- name: Enable k3s service
2022-05-14 21:24:39 +07:00
ansible.builtin.systemd:
name: k3s
2021-12-26 12:12:36 +07:00
enabled: true
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
2021-12-26 12:12:36 +07:00
run_once: true
2022-05-14 21:24:39 +07:00
ansible.builtin.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
2022-05-14 21:24:39 +07:00
ansible.builtin.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