restructured to support more generic k3s single host or cluster deployment

This commit is contained in:
Radar231 2023-01-07 20:54:29 +00:00
parent f197dbca52
commit f70470b435
1 changed files with 42 additions and 23 deletions

View File

@ -1,59 +1,78 @@
---
# tasks file for main.yml
###########################################################
- name: Check to make sure we have at least {{ minhosts }} target hosts (if cluster)
fail:
msg: "You need to have at least {{ minhosts }} target hosts for a cluster!"
when: (do_cluster == true) and (minhosts > numhosts)
############################################################
# 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
- name: Install k3s server on master node
shell:
cmd: curl -sfL https://get.k3s.io | sh -
when: "'master' in group_names"
when: ansible_hostname == master_host
###########################################################
- name: create /home/{{ username }}/.kube
- name: create /home/{{ k3s_user }}/.kube
file:
path: "/home/{{ username }}/.kube"
path: "/home/{{ k3s_user }}/.kube"
state: directory
owner: "{{ username }}"
group: "{{ username }}"
owner: "{{ k3s_user }}"
group: "{{ k3s_user }}"
mode: "0755"
when: (username != "root") and ('master' in group_names)
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/{{ username }}/.kube/config"
owner: "{{ username }}"
group: "{{ username }}"
dest: "/home/{{ k3s_user }}/.kube/config"
owner: "{{ k3s_user }}"
group: "{{ k3s_user }}"
mode: "0600"
when: (username != "root") and ('master' in group_names)
when: (k3s_user != "root") and (ansible_hostname == master_host)
###########################################################
- name: Add "KUBECONFIG" environment variables to .bashrc
blockinfile:
path: "/home/{{ username }}/.bashrc"
path: "/home/{{ k3s_user }}/.bashrc"
block: |
export KUBECONFIG=${HOME}/.kube/config
when: (username != "root") and ('master' in group_names)
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: "{{ hostvars[groups['master'][0]].inventory_hostname }}"
when: do_cluster == true
delegate_to: "{{ master_host }}"
when: (numhosts > "1") and (ansible_hostname != master_host)
###########################################################
- name: Run agent command on worker nodes
- name: Install k3s agent on worker nodes
shell:
cmd: curl -sfL https://get.k3s.io | K3S_URL=https://{{ hostvars[groups['master'][0]].ansible_host }}:6443 K3S_TOKEN={{ nodetoken.stdout }} sh -
when: (do_cluster == true) and ('workers' in group_names)
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