Reference node template by inventory path
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Danny Bessems 2023-03-25 19:13:11 +01:00
parent fa0b72a903
commit 89fd23f66a
3 changed files with 109 additions and 135 deletions

View File

@ -47,7 +47,7 @@
resourcepool: "{{ vcenter_info.resourcepool }}" resourcepool: "{{ vcenter_info.resourcepool }}"
folder: "{{ vcenter_info.folder }}" folder: "{{ vcenter_info.folder }}"
cluster: cluster:
nodetemplate: "{{ (components.clusterapi.workload.node_template.url | basename | split('.'))[:-1] | join('.') }}" nodetemplate: "{{ nodetemplate_inventorypath }}"
publickey: "{{ vapp['guestinfo.rootsshkey'] }}" publickey: "{{ vapp['guestinfo.rootsshkey'] }}"
version: "{{ components.clusterapi.workload.version.k8s }}" version: "{{ components.clusterapi.workload.version.k8s }}"
vip: "{{ vapp['workloadcluster.vip'] }}" vip: "{{ vapp['workloadcluster.vip'] }}"

View File

@ -1,82 +1,68 @@
- block: - block:
- name: Check for existing templates on hypervisor - name: Check for existing template on hypervisor
community.vmware.vmware_guest_info: community.vmware.vmware_guest_info:
name: "{{ (item | basename | split('.'))[:-1] | join('.') }}" name: "{{ (filename | basename | split('.'))[:-1] | join('.') }}"
register: existing_ova register: existing_ova
loop: "{{ query('ansible.builtin.fileglob', '/opt/metacluster/node-templates/*.ova') | first }}"
ignore_errors: yes ignore_errors: yes
- name: Parse OVA files for network mappings - name: Store inventory path of existing template
ansible.builtin.shell: ansible.builtin.set_fact:
cmd: govc import.spec -json {{ item }} nodetemplate_inventorypath: "{{ existing_ova.instance.hw_folder ~ '/' ~ existing_ova.instance.hw_name }}"
environment: when: existing_ova is not failed
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: Deploy OVA templates on hypervisor - block:
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
- debug: - name: Parse OVA file for network mappings
msg: "{{ existing_ova.results | first }}" ansible.builtin.shell:
- debug: cmd: govc import.spec -json {{ filename }}
msg: "{{ ova_deploy.results | first }}" 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 - name: Deploy OVA template on hypervisor
community.vmware.vmware_guest_disk: community.vmware.vmware_deploy_ovf:
name: "{{ item.instance.hw_name }}" cluster: "{{ vcenter_info.cluster }}"
disk: datastore: "{{ vcenter_info.datastore }}"
- size: 1Gb name: "{{ (filename | basename | split('.'))[:-1] | join('.') }}"
scsi_controller: 1 networks: "{u'{{ ova_spec.stdout | from_json | json_query('NetworkMapping[0].Name') }}':u'{{ vcenter_info.network }}'}"
scsi_type: paravirtual allow_duplicates: no
unit_number: 0 power_on: false
when: ova_deploy.results[index] is not skipped ovf: "{{ filename }}"
loop: "{{ ova_deploy.results }}" register: ova_deploy
loop_control:
index_var: index
label: "{{ item.item }}"
# Disabled to allow disks to be resized; at the cost of cloning speed - name: Add additional placeholder disk
# - name: Create snapshot on deployed VM's community.vmware.vmware_guest_disk:
# community.vmware.vmware_guest_snapshot: name: "{{ ova_deploy.instance.hw_name }}"
# name: "{{ item.instance.hw_name }}" disk:
# state: present - size: 1Gb
# snapshot_name: "{{ ansible_date_time.iso8601_basic_short }}-base" scsi_controller: 1
# when: ova_deploy.results[index] is not skipped scsi_type: paravirtual
# loop: "{{ ova_deploy.results }}" unit_number: 0
# loop_control:
# index_var: index
# label: "{{ item.item }}"
- name: Mark deployed VM's as templates # Disabled to allow disks to be resized; at the cost of cloning speed
community.vmware.vmware_guest: # - name: Create snapshot on deployed VM
name: "{{ item.instance.hw_name }}" # community.vmware.vmware_guest_snapshot:
is_template: yes # name: "{{ ova_deploy.instance.hw_name }}"
when: ova_deploy.results[index] is not skipped # state: present
loop: "{{ ova_deploy.results }}" # snapshot_name: "{{ ansible_date_time.iso8601_basic_short }}-base"
loop_control:
index_var: index
label: "{{ item.item }}"
- 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: module_defaults:
group/vmware: group/vmware:
hostname: "{{ vapp['hv.fqdn'] }}" hostname: "{{ vapp['hv.fqdn'] }}"

View File

@ -1,81 +1,68 @@
- block: - block:
- name: Check for existing templates on hypervisor - name: Check for existing template on hypervisor
community.vmware.vmware_guest_info: community.vmware.vmware_guest_info:
name: "{{ (item | basename | split('.'))[:-1] | join('.') }}" name: "{{ (filename | basename | split('.'))[:-1] | join('.') }}"
register: existing_ova register: existing_ova
loop: "{{ query('ansible.builtin.fileglob', '/opt/workloadcluster/node-templates/*.ova') | first }}"
ignore_errors: yes ignore_errors: yes
- name: Parse OVA files for network mappings - name: Store inventory path of existing template
ansible.builtin.shell: ansible.builtin.set_fact:
cmd: govc import.spec -json {{ item }} nodetemplate_inventorypath: "{{ existing_ova.instance.hw_folder ~ '/' ~ existing_ova.instance.hw_name }}"
environment: when: existing_ova is not failed
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: Deploy OVA templates on hypervisor - block:
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
- name: Add vApp properties on deployed VM's - name: Parse OVA file for network mappings
ansible.builtin.shell: ansible.builtin.shell:
cmd: >- cmd: govc import.spec -json {{ filename }}
npp-prepper \ environment:
--server "{{ vapp['hv.fqdn'] }}" \ GOVC_INSECURE: '1'
--username "{{ vapp['hv.username'] }}" \ GOVC_URL: "{{ vapp['hv.fqdn'] }}"
--password "{{ vapp['hv.password'] }}" \ GOVC_USERNAME: "{{ vapp['hv.username'] }}"
vm \ GOVC_PASSWORD: "{{ vapp['hv.password'] }}"
--datacenter "{{ vcenter_info.datacenter }}" \ register: ova_spec
--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: Create snapshot on deployed VM's - name: Deploy OVA template on hypervisor
community.vmware.vmware_guest_snapshot: community.vmware.vmware_deploy_ovf:
folder: "{{ vcenter_info.folder }}" cluster: "{{ vcenter_info.cluster }}"
name: "{{ item.instance.hw_name }}" datastore: "{{ vcenter_info.datastore }}"
state: present name: "{{ (filename | basename | split('.'))[:-1] | join('.') }}"
snapshot_name: "{{ ansible_date_time.iso8601_basic_short }}-base" networks: "{u'{{ ova_spec.stdout | from_json | json_query('NetworkMapping[0].Name') }}':u'{{ vcenter_info.network }}'}"
when: ova_deploy.results[index] is not skipped allow_duplicates: no
loop: "{{ ova_deploy.results }}" power_on: false
loop_control: ovf: "{{ filename }}"
index_var: index register: ova_deploy
label: "{{ item.item }}"
- name: Mark deployed VM's as templates - name: Add additional placeholder disk
community.vmware.vmware_guest: community.vmware.vmware_guest_disk:
name: "{{ item.instance.hw_name }}" name: "{{ ova_deploy.instance.hw_name }}"
is_template: yes disk:
when: ova_deploy.results[index] is not skipped - size: 1Gb
loop: "{{ ova_deploy.results }}" scsi_controller: 1
loop_control: scsi_type: paravirtual
index_var: index unit_number: 0
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: module_defaults:
group/vmware: group/vmware:
hostname: "{{ vapp['hv.fqdn'] }}" hostname: "{{ vapp['hv.fqdn'] }}"
@ -83,3 +70,4 @@
username: "{{ vapp['hv.username'] }}" username: "{{ vapp['hv.username'] }}"
password: "{{ vapp['hv.password'] }}" password: "{{ vapp['hv.password'] }}"
datacenter: "{{ vcenter_info.datacenter }}" datacenter: "{{ vcenter_info.datacenter }}"
folder: "{{ vcenter_info.folder }}"