homeserver/ansible: add playbook to spin up k8s cluster
- add new playbook to spin-up kubernetes cluster using k0sctl and k0sctl config file
This commit is contained in:
97
ansible/roles/create-kubernetes-cluster/tasks/main.yaml
Normal file
97
ansible/roles/create-kubernetes-cluster/tasks/main.yaml
Normal file
@ -0,0 +1,97 @@
|
||||
- name: Remove known_hosts file if it exists
|
||||
delegate_to: localhost
|
||||
run_once: true
|
||||
ansible.builtin.file:
|
||||
path: /home/taqi/.ssh/known_hosts
|
||||
state: absent
|
||||
|
||||
- name: Remove k0ctl lock file if it exists
|
||||
ansible.builtin.file:
|
||||
path: /run/lock/k0sctl
|
||||
state: absent
|
||||
become: true
|
||||
|
||||
- name: Install k0sctl on host
|
||||
delegate_to: localhost
|
||||
ansible.builtin.command:
|
||||
cmd: "go install github.com/k0sproject/k0sctl@latest"
|
||||
|
||||
- name: Ensure k0sctl is installed on host
|
||||
delegate_to: localhost
|
||||
run_once: true
|
||||
ansible.builtin.command:
|
||||
cmd: "k0sctl version"
|
||||
register: k0sctl_version
|
||||
changed_when: false
|
||||
|
||||
- name: Generate k0sctl configuration file
|
||||
delegate_to: localhost
|
||||
run_once: true
|
||||
ansible.builtin.template:
|
||||
src: k0sctl.yaml.j2
|
||||
dest: /tmp/k0sctl.yaml
|
||||
when: k0sctl_version is defined
|
||||
tags:
|
||||
- generate-k0sctl-config
|
||||
|
||||
- name: Generate MetalLB IP Address Pool configuration file
|
||||
delegate_to: localhost
|
||||
run_once: true
|
||||
ansible.builtin.template:
|
||||
src: ipAddressPool.yaml.j2
|
||||
dest: /tmp/ipAddressPool.yaml
|
||||
when: k0sctl_version is defined
|
||||
tags:
|
||||
- generatemetallb-ippool
|
||||
- metallb-ippool
|
||||
|
||||
- name: Create Cluster using k0sctl from host
|
||||
delegate_to: localhost
|
||||
run_once: true
|
||||
ansible.builtin.command:
|
||||
cmd: "k0sctl apply --config /tmp/k0sctl.yaml"
|
||||
when: k0sctl_version is defined
|
||||
|
||||
- name: Save kubeconfig file on host
|
||||
delegate_to: localhost
|
||||
run_once: true
|
||||
ansible.builtin.shell:
|
||||
cmd: "cd /tmp && k0sctl kubeconfig > /home/taqi/.kube/k0s_config.yaml"
|
||||
register: kubeconfig_result
|
||||
retries: 3
|
||||
delay: 5
|
||||
until: kubeconfig_result.rc == 0
|
||||
when: k0sctl_version is defined
|
||||
tags:
|
||||
- generate-kubeconfig
|
||||
|
||||
- name: Apply IP Pool for MetalLB from host
|
||||
delegate_to: localhost
|
||||
run_once: true
|
||||
ansible.builtin.shell:
|
||||
cmd: "kubectl apply -f /tmp/ipAddressPool.yaml --kubeconfig /home/taqi/.kube/k0s_config.yaml"
|
||||
register: metallb_ippool_result
|
||||
retries: 3
|
||||
delay: 5
|
||||
until: metallb_ippool_result.rc == 0
|
||||
when: k0sctl_version is defined
|
||||
tags:
|
||||
- metallb-ippool
|
||||
|
||||
- name: Cleanup temporary files
|
||||
delegate_to: localhost
|
||||
run_once: true
|
||||
block:
|
||||
- name: Remove k0sctl.yaml temporary file
|
||||
ansible.builtin.file:
|
||||
path: /tmp/k0sctl.yaml
|
||||
state: absent
|
||||
|
||||
- name: Remove ipAddressPool.yaml temporary file
|
||||
ansible.builtin.shell:
|
||||
cmd: "rm -f /tmp/ipAddressPool.yaml"
|
||||
delegate_to: localhost
|
||||
run_once: true
|
||||
tags:
|
||||
- cleanup
|
||||
when: k0sctl_version is defined
|
||||
Reference in New Issue
Block a user