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.set_fact:
nodetemplate_inventorypath: "{{ existing_ova.instance.hw_folder ~ '/' ~ existing_ova.instance.hw_name }}"
when: existing_ova is not failed
- block:
- name: Parse OVA file for network mappings
ansible.builtin.shell: ansible.builtin.shell:
cmd: govc import.spec -json {{ item }} cmd: govc import.spec -json {{ filename }}
environment: environment:
GOVC_INSECURE: '1' GOVC_INSECURE: '1'
GOVC_URL: "{{ vapp['hv.fqdn'] }}" GOVC_URL: "{{ vapp['hv.fqdn'] }}"
GOVC_USERNAME: "{{ vapp['hv.username'] }}" GOVC_USERNAME: "{{ vapp['hv.username'] }}"
GOVC_PASSWORD: "{{ vapp['hv.password'] }}" GOVC_PASSWORD: "{{ vapp['hv.password'] }}"
register: ova_spec 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 - name: Deploy OVA template on hypervisor
community.vmware.vmware_deploy_ovf: community.vmware.vmware_deploy_ovf:
cluster: "{{ vcenter_info.cluster }}" cluster: "{{ vcenter_info.cluster }}"
datastore: "{{ vcenter_info.datastore }}" datastore: "{{ vcenter_info.datastore }}"
name: "{{ (item | basename | split('.'))[:-1] | join('.') }}" name: "{{ (filename | basename | split('.'))[:-1] | join('.') }}"
networks: "{u'{{ ova_spec.results[index].stdout | from_json | json_query('NetworkMapping[0].Name') }}':u'{{ vcenter_info.network }}'}" networks: "{u'{{ ova_spec.stdout | from_json | json_query('NetworkMapping[0].Name') }}':u'{{ vcenter_info.network }}'}"
allow_duplicates: no allow_duplicates: no
power_on: false power_on: false
ovf: "{{ item }}" ovf: "{{ filename }}"
register: ova_deploy 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:
msg: "{{ existing_ova.results | first }}"
- debug:
msg: "{{ ova_deploy.results | first }}"
- name: Add additional placeholder disk - name: Add additional placeholder disk
community.vmware.vmware_guest_disk: community.vmware.vmware_guest_disk:
name: "{{ item.instance.hw_name }}" name: "{{ ova_deploy.instance.hw_name }}"
disk: disk:
- size: 1Gb - size: 1Gb
scsi_controller: 1 scsi_controller: 1
scsi_type: paravirtual scsi_type: paravirtual
unit_number: 0 unit_number: 0
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 # Disabled to allow disks to be resized; at the cost of cloning speed
# - name: Create snapshot on deployed VM's # - name: Create snapshot on deployed VM
# community.vmware.vmware_guest_snapshot: # community.vmware.vmware_guest_snapshot:
# name: "{{ item.instance.hw_name }}" # name: "{{ ova_deploy.instance.hw_name }}"
# state: present # state: present
# snapshot_name: "{{ ansible_date_time.iso8601_basic_short }}-base" # 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: Mark deployed VM's as templates - name: Mark deployed VM as templates
community.vmware.vmware_guest: community.vmware.vmware_guest:
name: "{{ item.instance.hw_name }}" name: "{{ ova_deploy.instance.hw_name }}"
is_template: yes is_template: yes
when: ova_deploy.results[index] is not skipped
loop: "{{ ova_deploy.results }}"
loop_control:
index_var: index
label: "{{ item.item }}"
- 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.set_fact:
nodetemplate_inventorypath: "{{ existing_ova.instance.hw_folder ~ '/' ~ existing_ova.instance.hw_name }}"
when: existing_ova is not failed
- block:
- name: Parse OVA file for network mappings
ansible.builtin.shell: ansible.builtin.shell:
cmd: govc import.spec -json {{ item }} cmd: govc import.spec -json {{ filename }}
environment: environment:
GOVC_INSECURE: '1' GOVC_INSECURE: '1'
GOVC_URL: "{{ vapp['hv.fqdn'] }}" GOVC_URL: "{{ vapp['hv.fqdn'] }}"
GOVC_USERNAME: "{{ vapp['hv.username'] }}" GOVC_USERNAME: "{{ vapp['hv.username'] }}"
GOVC_PASSWORD: "{{ vapp['hv.password'] }}" GOVC_PASSWORD: "{{ vapp['hv.password'] }}"
register: ova_spec 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 - name: Deploy OVA template on hypervisor
community.vmware.vmware_deploy_ovf: community.vmware.vmware_deploy_ovf:
cluster: "{{ vcenter_info.cluster }}" cluster: "{{ vcenter_info.cluster }}"
datastore: "{{ vcenter_info.datastore }}" datastore: "{{ vcenter_info.datastore }}"
folder: "{{ vcenter_info.folder }}" name: "{{ (filename | basename | split('.'))[:-1] | join('.') }}"
name: "{{ (item | basename | split('.'))[:-1] | join('.') }}" networks: "{u'{{ ova_spec.stdout | from_json | json_query('NetworkMapping[0].Name') }}':u'{{ vcenter_info.network }}'}"
networks: "{u'{{ ova_spec.results[index].stdout | from_json | json_query('NetworkMapping[0].Name') }}':u'{{ vcenter_info.network }}'}"
allow_duplicates: no allow_duplicates: no
power_on: false power_on: false
ovf: "{{ item }}" ovf: "{{ filename }}"
register: ova_deploy 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: Add additional placeholder disk
ansible.builtin.shell: community.vmware.vmware_guest_disk:
cmd: >- name: "{{ ova_deploy.instance.hw_name }}"
npp-prepper \ disk:
--server "{{ vapp['hv.fqdn'] }}" \ - size: 1Gb
--username "{{ vapp['hv.username'] }}" \ scsi_controller: 1
--password "{{ vapp['hv.password'] }}" \ scsi_type: paravirtual
vm \ unit_number: 0
--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: Create snapshot on deployed VM's # Disabled to allow disks to be resized; at the cost of cloning speed
community.vmware.vmware_guest_snapshot: # - name: Create snapshot on deployed VM
folder: "{{ vcenter_info.folder }}" # community.vmware.vmware_guest_snapshot:
name: "{{ item.instance.hw_name }}" # name: "{{ ova_deploy.instance.hw_name }}"
state: present # state: present
snapshot_name: "{{ ansible_date_time.iso8601_basic_short }}-base" # 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: Mark deployed VM's as templates - name: Mark deployed VM as templates
community.vmware.vmware_guest: community.vmware.vmware_guest:
name: "{{ item.instance.hw_name }}" name: "{{ ova_deploy.instance.hw_name }}"
is_template: yes is_template: yes
when: ova_deploy.results[index] is not skipped
loop: "{{ ova_deploy.results }}"
loop_control:
index_var: index
label: "{{ item.item }}"
- 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 }}"