2022-04-19 14:36:19 +00:00
|
|
|
- name: Provision VM's
|
|
|
|
hosts: localhost
|
2022-04-18 10:58:57 +00:00
|
|
|
gather_facts: false
|
|
|
|
vars_files:
|
2022-04-18 12:28:05 +00:00
|
|
|
- hypervisor.vcenter.yml
|
2022-04-18 19:35:28 +00:00
|
|
|
- cluster.k3s.yml
|
2022-04-18 10:58:57 +00:00
|
|
|
tasks:
|
2022-04-19 10:07:26 +00:00
|
|
|
|
2022-04-19 07:10:44 +00:00
|
|
|
- name: Download OVF-template
|
|
|
|
ansible.builtin.get_url:
|
|
|
|
url: "https://{{ repo_username }}:{{ repo_password }}@{{ image.ova_url }}"
|
|
|
|
dest: /scratch/image.ova
|
2022-04-19 10:07:26 +00:00
|
|
|
|
2022-04-19 11:27:38 +00:00
|
|
|
- name: Deploy VM's from OVF-template
|
2022-04-18 20:49:50 +00:00
|
|
|
community.vmware.vmware_deploy_ovf:
|
2022-04-18 12:28:05 +00:00
|
|
|
hostname: "{{ hv.hostname }}"
|
|
|
|
username: "{{ hv.username }}"
|
|
|
|
password: "{{ hv_password }}"
|
2022-04-18 10:58:57 +00:00
|
|
|
validate_certs: no
|
2022-04-18 12:28:05 +00:00
|
|
|
datacenter: "{{ hv.datacenter }}"
|
|
|
|
folder: "{{ hv.folder }}"
|
|
|
|
cluster: "{{ hv.cluster }}"
|
2022-04-19 14:36:19 +00:00
|
|
|
name: "{{ cluster.name | upper }}-{{ (item.ip | checksum)[-5:] | upper }}"
|
2022-04-18 20:49:50 +00:00
|
|
|
datastore: "{{ hv.datastore }}"
|
2022-04-18 21:08:48 +00:00
|
|
|
disk_provisioning: thin
|
|
|
|
networks:
|
|
|
|
"LAN": "{{ hv.network }}"
|
2022-04-18 20:49:50 +00:00
|
|
|
power_on: yes
|
2022-04-19 07:10:44 +00:00
|
|
|
ovf: /scratch/image.ova
|
2022-04-18 21:08:48 +00:00
|
|
|
deployment_option: "{{ image.deployment_option }}"
|
|
|
|
properties:
|
2022-04-19 14:36:19 +00:00
|
|
|
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 }}"
|
2022-04-19 07:53:20 +00:00
|
|
|
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') }}"
|
2022-04-19 07:53:20 +00:00
|
|
|
guestinfo.dnsserver: "{{ network.dnsserver }}"
|
|
|
|
guestinfo.gateway: "{{ network.gateway }}"
|
2022-04-18 12:28:05 +00:00
|
|
|
delegate_to: localhost
|
2022-04-18 10:58:57 +00:00
|
|
|
with_items: "{{ servers }}"
|
2022-04-19 10:07:26 +00:00
|
|
|
register: job_init
|
2022-04-19 10:22:56 +00:00
|
|
|
async: 300
|
2022-04-19 10:07:26 +00:00
|
|
|
poll: 0
|
|
|
|
|
2022-04-20 07:58:05 +00:00
|
|
|
- name: Pause to allow initial calls to complete
|
|
|
|
ansible.builtin.pause:
|
|
|
|
seconds: 10
|
|
|
|
|
2022-04-19 10:07:26 +00:00
|
|
|
- name: Poll for completion
|
|
|
|
ansible.builtin.async_status:
|
2022-04-19 10:12:15 +00:00
|
|
|
jid: "{{ item.ansible_job_id }}"
|
2022-04-19 10:07:26 +00:00
|
|
|
with_items: "{{ job_init.results }}"
|
|
|
|
register: job_poll
|
|
|
|
retries: 5
|
2022-04-19 10:22:56 +00:00
|
|
|
delay: 100
|
2022-04-19 10:07:26 +00:00
|
|
|
until: job_poll.finished
|
|
|
|
|
2022-04-19 13:49:12 +00:00
|
|
|
- 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')} ] }}"
|
|
|
|
with_items: "{{ job_poll | json_query('results[*]') }}"
|
2022-04-19 15:04:09 +00:00
|
|
|
# Purely to avoid large amount of spam; no sensitive data here.
|
2022-04-19 13:49:12 +00:00
|
|
|
no_log: true
|
|
|
|
|
2022-04-19 11:27:38 +00:00
|
|
|
- name: Register new VM's in inventory
|
|
|
|
ansible.builtin.add_host:
|
2022-04-19 13:49:12 +00:00
|
|
|
name: "{{ item.name }}"
|
|
|
|
ansible_host: "{{ item.ip }}"
|
|
|
|
groups: k3s_ha
|
|
|
|
with_items: "{{ nodes }}"
|
2022-04-19 14:36:19 +00:00
|
|
|
|
2022-04-20 08:31:27 +00:00
|
|
|
- name: Wait for systems to become reachable over SSH
|
|
|
|
ansible.builtin.wait_for:
|
|
|
|
host: "{{ item.ip }}"
|
|
|
|
port: 22
|
|
|
|
timeout: 300
|
|
|
|
with_items: "{{ 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
|
|
|
|
with_items: "{{ nodes }}"
|
|
|
|
|
|
|
|
- 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
|
|
|
|
with_items: "{{ publickeys.results }}"
|
2022-04-20 07:52:12 +00:00
|
|
|
# Purely to avoid large amount of spam; no sensitive data here.
|
|
|
|
no_log: true
|
2022-04-20 06:35:27 +00:00
|
|
|
|
2022-04-19 14:36:19 +00:00
|
|
|
- name: Provision Kubernetes
|
|
|
|
hosts: k3s_ha
|
2022-04-20 13:13:04 +00:00
|
|
|
gather_facts: true
|
2022-04-19 14:53:33 +00:00
|
|
|
vars_files:
|
|
|
|
- cluster.k3s.yml
|
2022-04-19 14:36:19 +00:00
|
|
|
tasks:
|
|
|
|
|
2022-04-20 11:17:12 +00:00
|
|
|
- name: Initial node
|
|
|
|
block:
|
|
|
|
|
|
|
|
- name: Install K3s binary
|
|
|
|
ansible.builtin.shell:
|
|
|
|
cmd: "curl -sfL https://get.k3s.io | sh -s - server --cluster-init --disable local-storage,traefik --tls-san {{ cluster.virtualip | ansible.utils.ipaddr('address') }}"
|
|
|
|
|
|
|
|
- name: Retrieve token
|
|
|
|
ansible.builtin.slurp:
|
|
|
|
src: /var/lib/rancher/k3s/server/token
|
|
|
|
register: k3s_token
|
|
|
|
|
2022-04-20 13:13:04 +00:00
|
|
|
- name: Store token
|
2022-04-20 11:17:12 +00:00
|
|
|
ansible.builtin.set_fact:
|
2022-04-20 13:13:04 +00:00
|
|
|
clustertoken: "{{ k3s_token.content | b64decode | trim }}"
|
2022-04-20 09:45:22 +00:00
|
|
|
|
2022-04-20 12:27:31 +00:00
|
|
|
- name: Pull 'kube-vip' image
|
|
|
|
ansible.builtin.shell:
|
|
|
|
cmd: ctr image pull ghcr.io/kube-vip/kube-vip:latest
|
|
|
|
|
|
|
|
- name: Generate manifest for daemonSet
|
|
|
|
ansible.builtin.shell:
|
|
|
|
cmd: "ctr run --rm --net-host ghcr.io/kube-vip/kube-vip:latest vip /kube-vip manifest daemonset --interface {{ ansible_interfaces | difference(['lo']) }} --address {{ cluster.virtualip | ansible.utils.ipaddr('address') }} --inCluster --taint --controlplane --services --arp --leaderElection"
|
|
|
|
register: manifest
|
|
|
|
|
|
|
|
- ansible.builtin.debug:
|
|
|
|
var: manifest
|
|
|
|
|
2022-04-20 10:46:21 +00:00
|
|
|
when: inventory_hostname == ansible_play_hosts[0]
|
2022-04-19 14:53:33 +00:00
|
|
|
|
2022-04-20 11:17:12 +00:00
|
|
|
- name: Additional nodes
|
|
|
|
block:
|
2022-04-20 10:58:58 +00:00
|
|
|
|
2022-04-20 11:17:12 +00:00
|
|
|
- name: Install K3s binary
|
|
|
|
ansible.builtin.shell:
|
|
|
|
cmd: "curl -sfL https://get.k3s.io | sh -s - server --disable local-storage,traefik"
|
|
|
|
environment:
|
2022-04-20 13:13:04 +00:00
|
|
|
K3S_TOKEN: "{{ hostvars[ansible_play_hosts[0]]['clustertoken'] }}"
|
2022-04-20 11:17:12 +00:00
|
|
|
K3S_URL: "{{ 'https://' + ( cluster.virtualip | ansible.utils.ipaddr('address') ) + ':6443' }}"
|
2022-04-20 10:58:58 +00:00
|
|
|
|
2022-04-20 10:42:01 +00:00
|
|
|
when: inventory_hostname != ansible_play_hosts[0]
|