Packer.Images/ansible/roles/metacluster/tasks/components.yml
Danny Bessems eb46c384a8
Some checks failed
continuous-integration/drone/push Build is failing
Fix label
2022-11-07 02:39:51 +01:00

130 lines
6.3 KiB
YAML

- name: Create folder structure(s)
ansible.builtin.file:
path: "{{ item }}"
state: directory
loop:
- /opt/metacluster/helm-charts
- /opt/metacluster/manifests/bootstrap-kubeadm/{{ components.clusterapi.manifest.version.base }}
- /opt/metacluster/manifests/cluster-api/{{ components.clusterapi.manifest.version.base }}
- /opt/metacluster/manifests/control-plane-kubeadm/{{ components.clusterapi.manifest.version.base }}
- /opt/metacluster/manifests/infrastructure-vsphere/{{ components.clusterapi.manifest.version.infrastructure_vsphere }}
- /opt/metacluster/container-images
- name: Add helm repositories
kubernetes.core.helm_repository:
name: "{{ item.name }}"
repo_url: "{{ item.url }}"
state: present
loop: "{{ platform.helm_repositories }}"
- name: Fetch helm charts
ansible.builtin.command:
cmd: helm fetch {{ item.value.helm.chart }} --untar --version {{ item.value.helm.version }}
chdir: /opt/metacluster/helm-charts
when: item.value.helm is defined
loop: "{{ lookup('ansible.builtin.dict', components) }}"
loop_control:
label: "{{ item.key }}"
- block:
- name: Aggregate chart_values into dict
ansible.builtin.set_fact:
chart_values: "{{ chart_values | default({}) | combine({ (item.key | regex_replace('[^A-Za-z0-9]', '')): { 'chart_values': (item.value.helm.chart_values | from_yaml) } }) }}"
when: item.value.helm.chart_values is defined
loop: "{{ lookup('ansible.builtin.dict', components) }}"
loop_control:
label: "{{ item.key }}"
- name: Write dict to vars_file
ansible.builtin.copy:
dest: /opt/firstboot/ansible/vars/metacluster.yml
content: "{{ { 'components': chart_values } | to_nice_yaml(indent=2, width=4096) }}"
- block:
- name: Download ClusterAPI manifests
ansible.builtin.get_url:
url: https://github.com/kubernetes-sigs/{{ item.url }}
dest: /opt/metacluster/manifests/{{ item.dest }}
register: clusterapi_manifests
loop:
# This list is based on `clusterctl config repositories`
# Note: Each manifest also needs a metadata.yaml file stored in the respective folder
- url: cluster-api/releases/download/{{ components.clusterapi.manifest.version.base }}/bootstrap-components.yaml
dest: bootstrap-kubeadm/{{ components.clusterapi.manifest.version.base }}/bootstrap-components.yaml
- url: cluster-api/releases/download/{{ components.clusterapi.manifest.version.base }}/core-components.yaml
dest: cluster-api/{{ components.clusterapi.manifest.version.base }}/core-components.yaml
- url: cluster-api/releases/download/{{ components.clusterapi.manifest.version.base }}/control-plane-components.yaml
dest: control-plane-kubeadm/{{ components.clusterapi.manifest.version.base }}/control-plane-components.yaml
- url: cluster-api-provider-vsphere/releases/download/{{ components.clusterapi.manifest.version.infrastructure_vsphere }}/infrastructure-components.yaml
dest: infrastructure-vsphere/{{ components.clusterapi.manifest.version.infrastructure_vsphere }}/infrastructure-components.yaml
- url: cluster-api-provider-vsphere/releases/download/{{ components.clusterapi.manifest.version.infrastructure_vsphere }}/metadata.yaml
dest: infrastructure-vsphere/{{ components.clusterapi.manifest.version.infrastructure_vsphere }}/metadata.yaml
# This downloads the same metadata.yaml file to three separate folders
- url: cluster-api/releases/download/{{ components.clusterapi.manifest.version.base }}/metadata.yaml
dest: bootstrap-kubeadm/{{ components.clusterapi.manifest.version.base }}/metadata.yaml
- url: cluster-api/releases/download/{{ components.clusterapi.manifest.version.base }}/metadata.yaml
dest: cluster-api/{{ components.clusterapi.manifest.version.base }}/metadata.yaml
- url: cluster-api/releases/download/{{ components.clusterapi.manifest.version.base }}/metadata.yaml
dest: control-plane-kubeadm/{{ components.clusterapi.manifest.version.base }}/metadata.yaml
loop_control:
label: "{{ item.url | basename }}"
- name: Parse manifests
ansible.builtin.shell:
cmd: cat {{ item.dest }} | yq --no-doc eval '.. | .image? | select(.)' | awk '!/ /' | sort
register: parsedmanifests
loop: "{{ clusterapi_manifests.results }}"
loop_control:
label: "{{ item.dest | basename }}"
- name: Store container images
ansible.builtin.set_fact:
clusterapi_containerimages: "{{ parsedmanifests | json_query('results[*].stdout') }}"
- name: Configure clusterctl
ansible.builtin.template:
src: clusterctl.j2
dest: /opt/metacluster/manifests/clusterctl.yaml
vars:
_template:
hv:
fqdn: "{{ vapp['hv.fqdn'] }}"
username: "{{ vapp['hv.username'] }}"
password: "{{ vapp['hv.password'] }}"
version:
base: "{{ components.clusterapi.manifest.version.base }}"
infrastructure_vsphere: "{{ components.clusterapi.manifest.version.infrastructure_vsphere }}"
- name: Parse helm charts for container images
ansible.builtin.shell:
cmd: "{{ item.value.helm.parse_logic }}"
chdir: /opt/metacluster/helm-charts/{{ item.key }}
register: containerimages
loop: "{{ lookup('ansible.builtin.dict', components) }}"
loop_control:
label: "{{ item.key }}"
- name: Pull and store containerimages
ansible.builtin.shell:
cmd: >-
skopeo copy \
--insecure-policy \
--retry-times=5 \
docker://{{ item }} \
docker-archive:./{{ ( item | regex_findall('[^/:]+'))[-2] }}.tar:{{ item }}
chdir: /opt/metacluster/container-images
loop: "{{ ((containerimages.results | map(attribute='stdout_lines') | flatten) + dependencies.container_images + (clusterapi_containerimages | list)) | unique }}"
# - name: Inject manifests
# ansible.builtin.template:
# src: "{{ item.type }}.j2"
# dest: /var/lib/rancher/k3s/server/manifests/{{ item.name }}-manifest.yaml
# owner: root
# group: root
# mode: 0600
# loop: "{{ lookup('ansible.builtin.dict', components) | map(attribute='value.manifests') | list | select('defined') | flatten }}"
# loop_control:
# label: "{{ item.type + '/' + item.name }}"