Ansible.K3sCluster/playbook.yml

95 lines
3.2 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 }}"
guestinfo.rootsshkey: "{{ private_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 }}"
delegate_to: localhost
2022-04-18 10:58:57 +00:00
with_items: "{{ servers }}"
register: job_init
2022-04-19 10:22:56 +00:00
async: 300
poll: 0
- name: Poll for completion
ansible.builtin.async_status:
2022-04-19 10:12:15 +00:00
jid: "{{ item.ansible_job_id }}"
with_items: "{{ job_init.results }}"
register: job_poll
retries: 5
2022-04-19 10:22:56 +00:00
delay: 100
until: job_poll.finished
- 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.
no_log: true
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
with_items: "{{ nodes }}"
- name: Provision Kubernetes
hosts: k3s_ha
gather_facts: false
vars_files:
- cluster.k3s.yml
tasks:
- name: Iterate over hosts
block:
- name: Install K3s binary
ansible.builtin.command:
cmd: "curl -sfL https://get.k3s.io | sh -s - server --cluster-init --disable local-storage,traefik --tls-san {{ cluster.virtualip | ansible.utils.ipaddr('address') }}"
environment:
K3S_TOKEN: cluster.mastertoken | default('', true)
K3S_URL: cluster.apiurl | default('', true)
- name: Retrieve token & reference new cluster
ansible.builtin.set_fact:
cluster: "{{ cluster | combine( { mastertoken: lookup('file', /var/lib/rancher/k3s/server/token) }, { apiurl: 'https://{{ cluster.virtualip | ansible.utils.ipaddr('address') }}:6443' } ) }}"
when: cluster.mastertoken is not defined
- ansible.builtin.debug:
var: cluster
throttle: 1