From 8e680c45be334cdc57515f9894a18a650b51a875 Mon Sep 17 00:00:00 2001 From: Danny Bessems Date: Thu, 28 Jul 2022 23:22:41 +0200 Subject: [PATCH] Housekeeping;Provision node templates;Add vApp properties --- .../ansible_payload/roles/vapp/tasks/main.yml | 9 +- .../roles/workloadcluster/tasks/main.yml | 93 +++++++++++++++++++ ansible/vars/metacluster.yml | 2 - scripts/Update-OvfConfiguration.yml | 27 ++++++ 4 files changed, 125 insertions(+), 6 deletions(-) create mode 100644 ansible/roles/firstboot/files/ansible_payload/roles/workloadcluster/tasks/main.yml diff --git a/ansible/roles/firstboot/files/ansible_payload/roles/vapp/tasks/main.yml b/ansible/roles/firstboot/files/ansible_payload/roles/vapp/tasks/main.yml index 3abfe05..db9cb62 100644 --- a/ansible/roles/firstboot/files/ansible_payload/roles/vapp/tasks/main.yml +++ b/ansible/roles/firstboot/files/ansible_payload/roles/vapp/tasks/main.yml @@ -29,9 +29,10 @@ - name: Assign vApp properties to dictionary ansible.builtin.set_fact: vapp: >- - {{ vapp | default({}) | - combine({((item.values() | list)[0].values() | list)[0]: - ((item.values() | list)[0].values() | list)[1]}) + {{ vapp | default({}) | combine({ + ((item.values() | list)[0].values() | list)[0]: + ((item.values() | list)[0].values() | list)[1]}) }} loop: "{{ property_section.matches }}" - no_log: true + loop_control: + label: "{{ ((item.values() | list)[0].values() | list)[0] }}" 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 new file mode 100644 index 0000000..9df73f7 --- /dev/null +++ b/ansible/roles/firstboot/files/ansible_payload/roles/workloadcluster/tasks/main.yml @@ -0,0 +1,93 @@ +- block: + + - name: Gather hypervisor details + ansible.builtin.shell: + cmd: govc ls -L {{ item.moref }} | awk -F/ '{print ${{ item.part }}}' + environment: + GOVC_INSECURE: '1' + GOVC_URL: "{{ vapp['hv.fqdn'] }}" + GOVC_USERNAME: "{{ vapp['hv.username'] }}" + GOVC_PASSWORD: "{{ vapp['hv.password'] }}" + register: govc_inventory + loop: + - attribute: cluster + moref: >- + $(govc object.collect -json VirtualMachine:{{ moref_id }} | \ + jq -r '.[] | select(.Name == "runtime").Val.Host | .Type + ":" + .Value') + part: (NF-1) + - attribute: datacenter + moref: VirtualMachine:{{ moref_id }} + part: 2 + - attribute: datastore + moref: >- + $(govc object.collect -json VirtualMachine:{{ moref_id }} | \ + jq -r '.[] | select(.Name == "datastore").Val.ManagedObjectReference | .[].Type + ":" + .[].Value') + part: NF + - attribute: folder + moref: >- + $(govc object.collect -json VirtualMachine:{{ moref_id }} | \ + jq -r '.[] | select(.Name == "parent").Val | .Type + ":" + .Value') + part: 0 + # - attribute: host + # moref: >- + # $(govc object.collect -json VirtualMachine:{{ moref_id }} | \ + # jq -r '.[] | select(.Name == "runtime").Val.Host | .Type + ":" + .Value') + # part: NF + - attribute: network + moref: >- + $(govc object.collect -json VirtualMachine:{{ moref_id }} | \ + jq -r '.[] | select(.Name == "network").Val.ManagedObjectReference | .[].Type + ":" + .[].Value') + part: NF + loop_control: + label: "{{ item.attribute }}" + + - name: Store hypervisor details in dictionary + ansible.builtin.set_fact: + vcenter_info: "{{ vcenter_info | default({}) | combine({ item.item.attribute : item.stdout }) }}" + loop: "{{ govc_inventory.results }}" + loop_control: + label: "{{ item.item.attribute }}" + +- block: + + - 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 + with_fileglob: /opt/workloadcluster/node-templates/*.ova + + - name: Deploy OVA templates on hypervisor + community.vmware.vmware_ovf_deploy: + 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 }}" + networks: "{u'{{ ova_spec.results[index].stdout | from_json | json_query('NetworkMappings[0].Name') }}':u'{{ vcenter_info.network }}'}" + power_on: false + ovf: "{{ item }}" + register: ova_deploy + with_fileglob: /opt/workloadcluster/node-templates/*.ova + loop_control: + index_var: index + + - name: Mark deployed VM's as templates + community.vmware.vmware_guest: + 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 }}" diff --git a/ansible/vars/metacluster.yml b/ansible/vars/metacluster.yml index ba290c2..3f4d1f7 100644 --- a/ansible/vars/metacluster.yml +++ b/ansible/vars/metacluster.yml @@ -131,8 +131,6 @@ dependencies: # - quay.io/skopeo/stable:v1.8.0 static_binaries: - - filename: tea - url: https://dl.gitea.io/tea/0.8.0/tea-0.8.0-linux-amd64 - filename: govc url: https://github.com/vmware/govmomi/releases/download/v0.29.0/govc_Linux_x86_64.tar.gz archive: compressed diff --git a/scripts/Update-OvfConfiguration.yml b/scripts/Update-OvfConfiguration.yml index 2d7f744..2743580 100644 --- a/scripts/Update-OvfConfiguration.yml +++ b/scripts/Update-OvfConfiguration.yml @@ -114,6 +114,33 @@ PropertyCategories: Configurations: '*' UserConfigurable: true +- Name: 4) Hypervisor + ProductProperties: + + - Key: hv.fqdn + Type: string(1..) + Label: vCenter Appliance FQDN* + Description: '' + DefaultValue: '' + Configurations: '*' + UserConfigurable: true + + - Key: hv.username + Type: string(1..) + Label: User name ...* + Description: '' + DefaultValue: '' + Configurations: '*' + UserConfigurable: true + + - Key: hv.password + Type: string(1..) + Label: Password ...* + Description: '' + DefaultValue: '' + Configurations: '*' + UserConfigurable: true + AdvancedOptions: - Key: appliance.name Value: "{{ appliance.name }}"