From e89505bef666439689d8c422dbd09566d7b23105 Mon Sep 17 00:00:00 2001 From: Danny Bessems Date: Wed, 3 Aug 2022 08:53:40 +0200 Subject: [PATCH] Handle existing templates --- .../roles/workloadcluster/tasks/main.yml | 32 +++++++++++++------ 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/ansible/roles/firstboot/files/ansible_payload/roles/workloadcluster/tasks/main.yml b/ansible/roles/firstboot/files/ansible_payload/roles/workloadcluster/tasks/main.yml index 095f10f..1e42b6d 100644 --- a/ansible/roles/firstboot/files/ansible_payload/roles/workloadcluster/tasks/main.yml +++ b/ansible/roles/firstboot/files/ansible_payload/roles/workloadcluster/tasks/main.yml @@ -50,6 +50,13 @@ - block: + - name: Check for existing templates on hypervisor + community.vmware.vmware_guest_info: + name: "{{ (item | basename | split('.'))[:-1] | join('.') }}" + register: existing_ova + with_fileglob: /opt/workloadcluster/node-templates/*.ova + ignore_errors: yes + - name: Parse OVA files for network mappings ansible.builtin.shell: cmd: govc import.spec -json {{ item }} @@ -59,36 +66,41 @@ GOVC_USERNAME: "{{ vapp['hv.username'] }}" GOVC_PASSWORD: "{{ vapp['hv.password'] }}" register: ova_spec + when: existing_ova.results[index] is failed with_fileglob: /opt/workloadcluster/node-templates/*.ova + loop_control: + index_var: index - name: Deploy OVA templates on hypervisor community.vmware.vmware_deploy_ovf: - hostname: "{{ vapp['hv.fqdn'] }}" - validate_certs: no - username: "{{ vapp['hv.username'] }}" - password: "{{ vapp['hv.password'] }}" - datacenter: "{{ vcenter_info.datacenter }}" 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 with_fileglob: /opt/workloadcluster/node-templates/*.ova loop_control: index_var: index - 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 }}" + + module_defaults: + group/vmware: hostname: "{{ vapp['hv.fqdn'] }}" validate_certs: no username: "{{ vapp['hv.username'] }}" password: "{{ vapp['hv.password'] }}" datacenter: "{{ vcenter_info.datacenter }}" - name: "{{ item.instance.hw_name }}" - is_template: yes - loop: "{{ ova_deploy.results }}" - loop_control: - label: "{{ item.instance.moid }}"