From 89fd23f66ad3f766237454a0f24e13fc30f7446f Mon Sep 17 00:00:00 2001 From: Danny Bessems Date: Sat, 25 Mar 2023 19:13:11 +0100 Subject: [PATCH] Reference node template by inventory path --- .../workloadcluster/tasks/clusterapi.yml | 2 +- .../workloadcluster/tasks/nodetemplates.yml | 120 ++++++++--------- .../workloadcluster/tasks/nodetemplates.yml | 122 ++++++++---------- 3 files changed, 109 insertions(+), 135 deletions(-) diff --git a/ansible/roles/firstboot/files/ansible_payload/bootstrap/roles/workloadcluster/tasks/clusterapi.yml b/ansible/roles/firstboot/files/ansible_payload/bootstrap/roles/workloadcluster/tasks/clusterapi.yml index 4281c54..a6bea8f 100644 --- a/ansible/roles/firstboot/files/ansible_payload/bootstrap/roles/workloadcluster/tasks/clusterapi.yml +++ b/ansible/roles/firstboot/files/ansible_payload/bootstrap/roles/workloadcluster/tasks/clusterapi.yml @@ -47,7 +47,7 @@ resourcepool: "{{ vcenter_info.resourcepool }}" folder: "{{ vcenter_info.folder }}" cluster: - nodetemplate: "{{ (components.clusterapi.workload.node_template.url | basename | split('.'))[:-1] | join('.') }}" + nodetemplate: "{{ nodetemplate_inventorypath }}" publickey: "{{ vapp['guestinfo.rootsshkey'] }}" version: "{{ components.clusterapi.workload.version.k8s }}" vip: "{{ vapp['workloadcluster.vip'] }}" diff --git a/ansible/roles/firstboot/files/ansible_payload/bootstrap/roles/workloadcluster/tasks/nodetemplates.yml b/ansible/roles/firstboot/files/ansible_payload/bootstrap/roles/workloadcluster/tasks/nodetemplates.yml index 4674ca2..e867b67 100644 --- a/ansible/roles/firstboot/files/ansible_payload/bootstrap/roles/workloadcluster/tasks/nodetemplates.yml +++ b/ansible/roles/firstboot/files/ansible_payload/bootstrap/roles/workloadcluster/tasks/nodetemplates.yml @@ -1,82 +1,68 @@ - block: - - name: Check for existing templates on hypervisor + - name: Check for existing template on hypervisor community.vmware.vmware_guest_info: - name: "{{ (item | basename | split('.'))[:-1] | join('.') }}" + name: "{{ (filename | basename | split('.'))[:-1] | join('.') }}" register: existing_ova - loop: "{{ query('ansible.builtin.fileglob', '/opt/metacluster/node-templates/*.ova') | first }}" ignore_errors: yes - - name: Parse OVA files for network mappings - ansible.builtin.shell: - cmd: govc import.spec -json {{ item }} - environment: - GOVC_INSECURE: '1' - GOVC_URL: "{{ vapp['hv.fqdn'] }}" - GOVC_USERNAME: "{{ vapp['hv.username'] }}" - GOVC_PASSWORD: "{{ vapp['hv.password'] }}" - register: ova_spec - when: existing_ova.results[index] is failed - loop: "{{ query('ansible.builtin.fileglob', '/opt/metacluster/node-templates/*.ova') | first }}" - loop_control: - index_var: index + - name: Store inventory path of existing template + ansible.builtin.set_fact: + nodetemplate_inventorypath: "{{ existing_ova.instance.hw_folder ~ '/' ~ existing_ova.instance.hw_name }}" + when: existing_ova is not failed - - name: Deploy OVA templates on hypervisor - community.vmware.vmware_deploy_ovf: - cluster: "{{ vcenter_info.cluster }}" - datastore: "{{ vcenter_info.datastore }}" - name: "{{ (item | basename | split('.'))[:-1] | join('.') }}" - networks: "{u'{{ ova_spec.results[index].stdout | from_json | json_query('NetworkMapping[0].Name') }}':u'{{ vcenter_info.network }}'}" - allow_duplicates: no - power_on: false - ovf: "{{ item }}" - register: ova_deploy - when: existing_ova.results[index] is failed - loop: "{{ query('ansible.builtin.fileglob', '/opt/metacluster/node-templates/*.ova') | first }}" - loop_control: - index_var: index + - block: - - debug: - msg: "{{ existing_ova.results | first }}" - - debug: - msg: "{{ ova_deploy.results | first }}" + - name: Parse OVA file for network mappings + ansible.builtin.shell: + cmd: govc import.spec -json {{ filename }} + environment: + GOVC_INSECURE: '1' + GOVC_URL: "{{ vapp['hv.fqdn'] }}" + GOVC_USERNAME: "{{ vapp['hv.username'] }}" + GOVC_PASSWORD: "{{ vapp['hv.password'] }}" + register: ova_spec - - name: Add additional placeholder disk - community.vmware.vmware_guest_disk: - name: "{{ item.instance.hw_name }}" - disk: - - size: 1Gb - scsi_controller: 1 - scsi_type: paravirtual - unit_number: 0 - when: ova_deploy.results[index] is not skipped - loop: "{{ ova_deploy.results }}" - loop_control: - index_var: index - label: "{{ item.item }}" + - name: Deploy OVA template on hypervisor + community.vmware.vmware_deploy_ovf: + cluster: "{{ vcenter_info.cluster }}" + datastore: "{{ vcenter_info.datastore }}" + name: "{{ (filename | basename | split('.'))[:-1] | join('.') }}" + networks: "{u'{{ ova_spec.stdout | from_json | json_query('NetworkMapping[0].Name') }}':u'{{ vcenter_info.network }}'}" + allow_duplicates: no + power_on: false + ovf: "{{ filename }}" + register: ova_deploy - # Disabled to allow disks to be resized; at the cost of cloning speed - # - name: Create snapshot on deployed VM's - # community.vmware.vmware_guest_snapshot: - # name: "{{ item.instance.hw_name }}" - # state: present - # snapshot_name: "{{ ansible_date_time.iso8601_basic_short }}-base" - # when: ova_deploy.results[index] is not skipped - # loop: "{{ ova_deploy.results }}" - # loop_control: - # index_var: index - # label: "{{ item.item }}" + - name: Add additional placeholder disk + community.vmware.vmware_guest_disk: + name: "{{ ova_deploy.instance.hw_name }}" + disk: + - size: 1Gb + scsi_controller: 1 + scsi_type: paravirtual + unit_number: 0 - - name: Mark deployed VM's as templates - community.vmware.vmware_guest: - name: "{{ item.instance.hw_name }}" - is_template: yes - when: ova_deploy.results[index] is not skipped - loop: "{{ ova_deploy.results }}" - loop_control: - index_var: index - label: "{{ item.item }}" + # Disabled to allow disks to be resized; at the cost of cloning speed + # - name: Create snapshot on deployed VM + # community.vmware.vmware_guest_snapshot: + # name: "{{ ova_deploy.instance.hw_name }}" + # state: present + # snapshot_name: "{{ ansible_date_time.iso8601_basic_short }}-base" + - name: Mark deployed VM as templates + community.vmware.vmware_guest: + name: "{{ ova_deploy.instance.hw_name }}" + is_template: yes + + - name: Store inventory path of deployed template + ansible.builtin.set_fact: + nodetemplate_inventorypath: "{{ ova_deploy.instance.hw_folder ~ '/' ~ ova_deploy.instance.hw_name }}" + + when: existing_ova is failed + + vars: + filename: "{{ query('ansible.builtin.fileglob', '/opt/metacluster/node-templates/*.ova') | first }}" module_defaults: group/vmware: hostname: "{{ vapp['hv.fqdn'] }}" diff --git a/ansible/roles/firstboot/files/ansible_payload/upgrade/roles/workloadcluster/tasks/nodetemplates.yml b/ansible/roles/firstboot/files/ansible_payload/upgrade/roles/workloadcluster/tasks/nodetemplates.yml index 38ab4ea..e867b67 100644 --- a/ansible/roles/firstboot/files/ansible_payload/upgrade/roles/workloadcluster/tasks/nodetemplates.yml +++ b/ansible/roles/firstboot/files/ansible_payload/upgrade/roles/workloadcluster/tasks/nodetemplates.yml @@ -1,81 +1,68 @@ - block: - - name: Check for existing templates on hypervisor + - name: Check for existing template on hypervisor community.vmware.vmware_guest_info: - name: "{{ (item | basename | split('.'))[:-1] | join('.') }}" + name: "{{ (filename | basename | split('.'))[:-1] | join('.') }}" register: existing_ova - loop: "{{ query('ansible.builtin.fileglob', '/opt/workloadcluster/node-templates/*.ova') | first }}" ignore_errors: yes - - name: Parse OVA files for network mappings - ansible.builtin.shell: - cmd: govc import.spec -json {{ item }} - environment: - GOVC_INSECURE: '1' - GOVC_URL: "{{ vapp['hv.fqdn'] }}" - GOVC_USERNAME: "{{ vapp['hv.username'] }}" - GOVC_PASSWORD: "{{ vapp['hv.password'] }}" - register: ova_spec - when: existing_ova.results[index] is failed - loop: "{{ query('ansible.builtin.fileglob', '/opt/workloadcluster/node-templates/*.ova') | first }}" - loop_control: - index_var: index + - name: Store inventory path of existing template + ansible.builtin.set_fact: + nodetemplate_inventorypath: "{{ existing_ova.instance.hw_folder ~ '/' ~ existing_ova.instance.hw_name }}" + when: existing_ova is not failed - - name: Deploy OVA templates on hypervisor - community.vmware.vmware_deploy_ovf: - cluster: "{{ vcenter_info.cluster }}" - datastore: "{{ vcenter_info.datastore }}" - folder: "{{ vcenter_info.folder }}" - name: "{{ (item | basename | split('.'))[:-1] | join('.') }}" - networks: "{u'{{ ova_spec.results[index].stdout | from_json | json_query('NetworkMapping[0].Name') }}':u'{{ vcenter_info.network }}'}" - allow_duplicates: no - power_on: false - ovf: "{{ item }}" - register: ova_deploy - when: existing_ova.results[index] is failed - loop: "{{ query('ansible.builtin.fileglob', '/opt/workloadcluster/node-templates/*.ova') | first }}" - loop_control: - index_var: index + - block: - - name: Add vApp properties on deployed VM's - ansible.builtin.shell: - cmd: >- - npp-prepper \ - --server "{{ vapp['hv.fqdn'] }}" \ - --username "{{ vapp['hv.username'] }}" \ - --password "{{ vapp['hv.password'] }}" \ - vm \ - --datacenter "{{ vcenter_info.datacenter }}" \ - --portgroup "{{ vcenter_info.network }}" \ - --name "{{ item.instance.hw_name }}" - when: existing_ova.results[index] is failed - loop: "{{ ova_deploy.results }}" - loop_control: - index_var: index - label: "{{ item.item }}" + - name: Parse OVA file for network mappings + ansible.builtin.shell: + cmd: govc import.spec -json {{ filename }} + environment: + GOVC_INSECURE: '1' + GOVC_URL: "{{ vapp['hv.fqdn'] }}" + GOVC_USERNAME: "{{ vapp['hv.username'] }}" + GOVC_PASSWORD: "{{ vapp['hv.password'] }}" + register: ova_spec - - name: Create snapshot on deployed VM's - community.vmware.vmware_guest_snapshot: - folder: "{{ vcenter_info.folder }}" - name: "{{ item.instance.hw_name }}" - state: present - snapshot_name: "{{ ansible_date_time.iso8601_basic_short }}-base" - when: ova_deploy.results[index] is not skipped - loop: "{{ ova_deploy.results }}" - loop_control: - index_var: index - label: "{{ item.item }}" + - name: Deploy OVA template on hypervisor + community.vmware.vmware_deploy_ovf: + cluster: "{{ vcenter_info.cluster }}" + datastore: "{{ vcenter_info.datastore }}" + name: "{{ (filename | basename | split('.'))[:-1] | join('.') }}" + networks: "{u'{{ ova_spec.stdout | from_json | json_query('NetworkMapping[0].Name') }}':u'{{ vcenter_info.network }}'}" + allow_duplicates: no + power_on: false + ovf: "{{ filename }}" + register: ova_deploy - - name: Mark deployed VM's as templates - community.vmware.vmware_guest: - name: "{{ item.instance.hw_name }}" - is_template: yes - when: ova_deploy.results[index] is not skipped - loop: "{{ ova_deploy.results }}" - loop_control: - index_var: index - label: "{{ item.item }}" + - name: Add additional placeholder disk + community.vmware.vmware_guest_disk: + name: "{{ ova_deploy.instance.hw_name }}" + disk: + - size: 1Gb + scsi_controller: 1 + scsi_type: paravirtual + unit_number: 0 + # Disabled to allow disks to be resized; at the cost of cloning speed + # - name: Create snapshot on deployed VM + # community.vmware.vmware_guest_snapshot: + # name: "{{ ova_deploy.instance.hw_name }}" + # state: present + # snapshot_name: "{{ ansible_date_time.iso8601_basic_short }}-base" + + - name: Mark deployed VM as templates + community.vmware.vmware_guest: + name: "{{ ova_deploy.instance.hw_name }}" + is_template: yes + + - name: Store inventory path of deployed template + ansible.builtin.set_fact: + nodetemplate_inventorypath: "{{ ova_deploy.instance.hw_folder ~ '/' ~ ova_deploy.instance.hw_name }}" + + when: existing_ova is failed + + vars: + filename: "{{ query('ansible.builtin.fileglob', '/opt/metacluster/node-templates/*.ova') | first }}" module_defaults: group/vmware: hostname: "{{ vapp['hv.fqdn'] }}" @@ -83,3 +70,4 @@ username: "{{ vapp['hv.username'] }}" password: "{{ vapp['hv.password'] }}" datacenter: "{{ vcenter_info.datacenter }}" + folder: "{{ vcenter_info.folder }}"