role_k3s/tasks/main.yml

79 lines
2.4 KiB
YAML

---
# tasks file for main.yml
############################################################
# display which host will be the master node
- debug:
msg: "The host {{ master_host }} will be the master node. Any other hosts, if any, will be worker nodes."
run_once: true
############################################################
# ensure curl is installed
#######################################
- name: ensure curl is installed (Debian)
apt:
name:
- curl
state: latest
when: ansible_os_family == "Debian"
#######################################
- name: ensure curl is installed (Arch)
pacman:
name:
- curl
state: latest
when: ansible_os_family == "Archlinux"
###########################################################
- name: Install k3s server on master node
shell:
cmd: curl -sfL https://get.k3s.io | sh -
when: ansible_hostname == master_host
###########################################################
- name: create /home/{{ k3s_user }}/.kube
file:
path: "/home/{{ k3s_user }}/.kube"
state: directory
owner: "{{ k3s_user }}"
group: "{{ k3s_user }}"
mode: "0755"
when: k3s_user != "root" and ansible_hostname == master_host
###########################################################
- name: Copy kubernetes admin conf to user .kube directory
copy:
remote_src: true
src: "/etc/rancher/k3s/k3s.yaml"
dest: "/home/{{ k3s_user }}/.kube/config"
owner: "{{ k3s_user }}"
group: "{{ k3s_user }}"
mode: "0600"
when: k3s_user != "root" and ansible_hostname == master_host
###########################################################
- name: Add "KUBECONFIG" environment variables to .bashrc
blockinfile:
path: "/home/{{ k3s_user }}/.bashrc"
block: |
export KUBECONFIG=${HOME}/.kube/config
when: k3s_user != "root" and ansible_hostname == master_host
###########################################################
- name: Retrieve nodetoken from master node
shell:
cmd: cat /var/lib/rancher/k3s/server/node-token
register: nodetoken
delegate_to: "{{ master_host }}"
when: numhosts > "1" and ansible_hostname != master_host
###########################################################
- name: Install k3s agent on worker nodes
shell:
cmd: curl -sfL https://get.k3s.io | K3S_URL=https://{{ master_ip }}:6443 K3S_TOKEN={{ nodetoken.stdout }} sh -
when: numhosts > "1" and ansible_hostname != master_host
# EOF