Ansible.K3sCluster/playbook.yml

237 lines
7.6 KiB
YAML
Raw Normal View History

- name: Provision VM's
hosts: localhost
2022-04-18 10:58:57 +00:00
gather_facts: false
vars_files:
- hypervisor.vcenter.yml
- cluster.k3s.yml
2022-04-18 10:58:57 +00:00
tasks:
- name: Download OVF-template
ansible.builtin.get_url:
url: "https://{{ repo_username }}:{{ repo_password }}@{{ image.ova_url }}"
dest: /scratch/image.ova
2022-04-19 11:27:38 +00:00
- name: Deploy VM's from OVF-template
community.vmware.vmware_deploy_ovf:
hostname: "{{ hv.hostname }}"
username: "{{ hv.username }}"
password: "{{ hv_password }}"
2022-04-18 10:58:57 +00:00
validate_certs: no
datacenter: "{{ hv.datacenter }}"
folder: "{{ hv.folder }}"
cluster: "{{ hv.cluster }}"
name: "{{ cluster.name | upper }}-{{ (item.ip | checksum)[-5:] | upper }}"
datastore: "{{ hv.datastore }}"
2022-04-18 21:08:48 +00:00
disk_provisioning: thin
networks:
"LAN": "{{ hv.network }}"
power_on: yes
ovf: /scratch/image.ova
2022-04-18 21:08:48 +00:00
deployment_option: "{{ image.deployment_option }}"
properties:
guestinfo.hostname: "{{ cluster.name | upper }}-{{ (item.ip | checksum)[-5:] | upper }}"
2022-04-18 21:08:48 +00:00
guestinfo.rootpw: "{{ root_password }}"
2022-04-19 16:12:13 +00:00
guestinfo.rootsshkey: "{{ public_key }}"
guestinfo.ntpserver: "{{ network.ntpserver }}"
2022-04-18 21:12:30 +00:00
guestinfo.ipaddress: "{{ item.ip | ansible.utils.ipaddr('address') }}"
2022-04-19 08:45:18 +00:00
guestinfo.prefixlength: "{{ item.ip | ansible.utils.ipaddr('prefix') }}"
guestinfo.dnsserver: "{{ network.dnsserver }}"
guestinfo.gateway: "{{ network.gateway }}"
register: job_init
2022-04-19 10:22:56 +00:00
async: 300
poll: 0
delegate_to: localhost
loop: "{{ servers }}"
2022-04-20 07:58:05 +00:00
- name: Pause to allow initial calls to complete
ansible.builtin.pause:
seconds: 10
- name: Poll for completion
ansible.builtin.async_status:
2022-04-19 10:12:15 +00:00
jid: "{{ item.ansible_job_id }}"
register: job_poll
retries: 5
2022-04-29 10:00:01 +00:00
delay: 100
until: job_poll.finished
loop: "{{ job_init.results }}"
2022-04-22 21:58:31 +00:00
loop_control:
label: "{{ { 'ip': item.item.ip } }}"
- name: Parse results into dictionary
ansible.builtin.set_fact:
nodes: "{{ nodes | default([]) + [ {'name': item.instance.hw_name, 'ip': item.item.item.ip | ansible.utils.ipaddr('address')} ] }}"
loop: "{{ job_poll | json_query('results[*]') }}"
loop_control:
2022-04-22 21:37:58 +00:00
label: "{{ { 'name': item.instance.hw_name, 'ip': item.item.item.ip } }}"
2022-04-19 11:27:38 +00:00
- name: Register new VM's in inventory
ansible.builtin.add_host:
name: "{{ item.name }}"
ansible_host: "{{ item.ip }}"
groups: k3s_ha
loop: "{{ nodes }}"
- name: Wait for systems to become reachable over SSH
ansible.builtin.wait_for:
host: "{{ item.ip }}"
port: 22
timeout: 300
loop: "{{ nodes }}"
2022-04-20 07:16:55 +00:00
- name: Scan public keys
ansible.builtin.shell:
cmd: "ssh-keyscan -t rsa {{ item.ip }}"
register: publickeys
loop: "{{ nodes }}"
2022-04-22 21:31:35 +00:00
loop_control:
2022-04-22 21:37:58 +00:00
label: "{{ item.ip }}"
2022-04-20 07:16:55 +00:00
- name: Store public keys
ansible.builtin.known_hosts:
2022-04-20 07:46:01 +00:00
name: "{{ item.item.name | lower }}"
key: "{{ item.item.name | lower }},{{ item.stdout }}"
2022-04-20 07:16:55 +00:00
state: present
path: ~/.ssh/known_hosts
loop: "{{ publickeys.results }}"
loop_control:
2022-04-22 21:52:49 +00:00
label: "{{ { 'name': item.item.name, 'ip': item.item.ip } }}"
2022-04-20 06:35:27 +00:00
- name: Provision Kubernetes
hosts: k3s_ha
2022-04-20 13:13:04 +00:00
gather_facts: true
vars_files:
- cluster.k3s.yml
tasks:
2022-04-20 14:54:56 +00:00
- block:
2022-04-20 11:17:12 +00:00
2022-04-20 15:10:45 +00:00
- name: Initial node -- Install K3s binary
2022-04-20 11:17:12 +00:00
ansible.builtin.shell:
2022-04-29 09:55:51 +00:00
cmd: curl -sfL https://get.k3s.io | sh -s - server --cluster-init --disable local-storage
# cmd: "curl -sfL https://get.k3s.io | sh -s - server --cluster-init --disable local-storage --tls-san {{ cluster.virtualip | ansible.utils.ipaddr('address') }}"
2022-04-20 11:17:12 +00:00
2022-04-20 15:10:45 +00:00
- name: Initial node -- Retrieve token
2022-04-20 11:17:12 +00:00
ansible.builtin.slurp:
src: /var/lib/rancher/k3s/server/token
register: k3s_token
2022-04-25 18:54:47 +00:00
- name: Initial node -- Retrieve kubeconfig
ansible.builtin.shell:
cmd: kubectl config view --raw
register: k3s_kubeconfig
2022-04-20 15:10:45 +00:00
- name: Initial node -- Store token
2022-04-20 11:17:12 +00:00
ansible.builtin.set_fact:
cluster: "{{ cluster | combine( { 'token': ( k3s_token.content | b64decode | trim ) }, { 'kubeconfig': ( k3s_kubeconfig.stdout ) } ) }}"
2022-04-20 09:45:22 +00:00
2022-04-29 09:55:51 +00:00
# - block:
# - name: Install 'kube-vip' -- Retrieve RBAC-manifest
# ansible.builtin.uri:
# url: https://kube-vip.io/manifests/rbac.yaml
# return_content: yes
# register: manifest_rbac
# - name: Install 'kube-vip' -- Pull image
# ansible.builtin.shell:
# cmd: ctr image pull ghcr.io/kube-vip/kube-vip:latest
# - name: Install 'kube-vip' -- Generate daemonSet-manifest
# ansible.builtin.shell:
# cmd: "ctr run --rm --net-host ghcr.io/kube-vip/kube-vip:latest vip /kube-vip manifest daemonset --interface {{ ansible_default_ipv4.interface }} --address {{ cluster.virtualip | ansible.utils.ipaddr('address') }} --inCluster --taint --controlplane --services --arp --leaderElection"
# register: manifest_daemonset
# - name: Install 'kube-vip' -- Inject manifest
# ansible.builtin.template:
# src: kube-vip.j2
# dest: /var/lib/rancher/k3s/server/manifests/kube-vip.yml
# delegate_to: "{{ ansible_play_hosts[0] }}"
# run_once: true
# - name: All nodes -- Ensure API availability
# ansible.utils.cli_parse:
# command: "curl -k https://{{ cluster.virtualip | ansible.utils.ipaddr('address') }}:6443/livez?verbose"
# parser:
# name: ansible.utils.json
# set_fact: api_readycheck
# ignore_errors: yes
# until: api_readycheck.apiVersion is defined
# retries: 3
# delay: 30
- block:
2022-04-20 10:58:58 +00:00
- name: Additional nodes -- Install K3s binary
ansible.builtin.shell:
cmd: "curl -sfL https://get.k3s.io | sh -s - server --disable local-storage"
executable: /bin/bash
environment:
K3S_TOKEN: "{{ cluster.token }}" # (hostvars[ansible_play_hosts[0]]).cluster.token
K3S_URL: "{{ 'https://' + ( cluster.virtualip | ansible.utils.ipaddr('address') ) + ':6443' }}"
throttle: 1
when: inventory_hostname != ansible_play_hosts[0]
- name: Deploy applications
hosts: localhost
gather_facts: false
vars_files:
- applications.k3s.yml
2022-04-26 07:19:24 +00:00
- cluster.k3s.yml
tasks:
2022-04-26 12:28:04 +00:00
- block:
- name: Initialize tempfile
ansible.builtin.tempfile:
state: file
register: kubeconfig
2022-04-29 14:09:17 +00:00
2022-04-26 12:28:04 +00:00
- name: Store kubeconfig in tempfile
ansible.builtin.copy:
dest: "{{ kubeconfig.path }}"
2022-04-29 14:09:17 +00:00
content: "{{ ( lookup('dict', hostvars) ).value.cluster.kubeconfig }}"
2022-04-26 12:28:04 +00:00
mode: 0600
2022-04-29 09:55:51 +00:00
2022-04-26 12:28:04 +00:00
- name: Replace API url
ansible.builtin.replace:
path: "{{ kubeconfig.path }}"
regexp: 'server: https:\/\/127\.0\.0\.1:6443$'
2022-04-29 09:55:51 +00:00
replace: "server: https://{{ servers[0].ip | ansible.utils.ipaddr('address') }}:6443"
# replace: "server: https://{{ cluster.virtualip | ansible.utils.ipaddr('address') }}:6443"
2022-04-26 07:19:24 +00:00
- name: Add Helm chart repositories
kubernetes.core.helm_repository:
name: "{{ item.name }}"
repo_url: "{{ item.url }}"
2022-04-26 12:12:57 +00:00
no_log: true
loop: "{{ helm.repositories }}"
2022-04-24 10:32:34 +00:00
2022-05-02 09:00:21 +00:00
- name: Install Helm Diff
kubernetes.core.helm_plugin:
state: present
plugin_path: "{{ item.path }}"
loop: "{{ helm.plugins }}"
- name: Install Helm charts
kubernetes.core.helm:
name: "{{ item.name }}"
chart_ref: "{{ item.ref }}"
namespace: "{{ item.namespace }}"
create_namespace: yes
2022-05-02 09:00:21 +00:00
# wait: yes
kubeconfig: "{{ kubeconfig.path }}"
values: "{{ item.setvalues | default(omit) }}"
loop: "{{ helm.charts }}"
2022-05-02 08:35:02 +00:00
throttle: 1
2022-05-02 09:00:21 +00:00
- name: Apply manifests
kubernetes.core.k8s:
definition: "{{ item.definition }}"
wait: yes
loop: "{{ manifests }}"
loop_control:
label: "{{ item.name }}"