From 44e97410dde230ea5f4629ff53c1ef23f7b241ee Mon Sep 17 00:00:00 2001 From: Mike Morency Date: Mon, 10 Jun 2024 15:54:26 -0400 Subject: [PATCH 1/5] adding export vm as ovf role --- playbooks/export_vm_as_ovf.yml | 6 ++ roles/export_vm_as_ovf/README.md | 102 ++++++++++++++++++ roles/export_vm_as_ovf/tasks/main.yml | 35 ++++++ .../targets/vmware_export_vm_as_ovf/run.yml | 23 ++++ .../targets/vmware_export_vm_as_ovf/runme.sh | 14 +++ .../vmware_export_vm_as_ovf/tasks/main.yml | 41 +++++++ .../vmware_export_vm_as_ovf/vars/main.yml | 9 ++ 7 files changed, 230 insertions(+) create mode 100644 playbooks/export_vm_as_ovf.yml create mode 100644 roles/export_vm_as_ovf/README.md create mode 100644 roles/export_vm_as_ovf/tasks/main.yml create mode 100644 tests/integration/targets/vmware_export_vm_as_ovf/run.yml create mode 100755 tests/integration/targets/vmware_export_vm_as_ovf/runme.sh create mode 100644 tests/integration/targets/vmware_export_vm_as_ovf/tasks/main.yml create mode 100644 tests/integration/targets/vmware_export_vm_as_ovf/vars/main.yml diff --git a/playbooks/export_vm_as_ovf.yml b/playbooks/export_vm_as_ovf.yml new file mode 100644 index 00000000..be103554 --- /dev/null +++ b/playbooks/export_vm_as_ovf.yml @@ -0,0 +1,6 @@ +- name: Export VM as OVF File + hosts: all + gather_facts: false + + roles: + - role: cloud.vmware_ops.export_vm_as_ovf diff --git a/roles/export_vm_as_ovf/README.md b/roles/export_vm_as_ovf/README.md new file mode 100644 index 00000000..fb0d624b --- /dev/null +++ b/roles/export_vm_as_ovf/README.md @@ -0,0 +1,102 @@ +# Export VM As OVF Role + +A role to export a VM from VCenter or ESXi as an OVF. The VM is exported to the local filesystem of the host running the tasks (anisble_host). + +## Requirements + +N/A + +## Role Variables +### Auth +- **export_vm_as_ovf_username**: + - The vSphere vCenter username. + +- **export_vm_as_ovf_password**: + - The vSphere vCenter password. + +- **export_vm_as_ovf_hostname**: + - The hostname or IP address of the vSphere vCenter. + +- **export_vm_as_ovf_validate_certs** + - Allows connection when SSL certificates are not valid. Set to false when certificates are not trusted. + +- **export_vm_as_ovf_cluster_name**: + - The name of the cluster in vSphere vCenter to configure. + +- **export_vm_as_ovf_datacenter_name**: + - The name of the datacenter in vSphere vCenter which contains the cluster to configure. + +- **export_vm_as_ovf_port**: + - str or int, The port to use to authenticate to the vSphere vCenter which contains the cluster to configure. + +### VM Options +- **export_vm_as_ovf_vm_datacenter**: + - str, The name of the datacenter that contains the VM that should be exported. + +- **export_vm_as_ovf_vm_folder**: + - str, The VCenter folder that contains the VM that should be exported. This should be the full folder path + +- **export_vm_as_ovf_vm_moid**: + - str, The MOID of the VM that should be exported. + - One of `export_vm_as_ovf_vm_uuid`, `export_vm_as_ovf_vm_name`, or `export_vm_as_ovf_vm_moid` needs to be defined. + +- **export_vm_as_ovf_vm_name**: + - str, The name of the VM that should be exported. + - One of `export_vm_as_ovf_vm_uuid`, `export_vm_as_ovf_vm_name`, or `export_vm_as_ovf_vm_moid` needs to be defined. + +- **export_vm_as_ovf_vm_uuid**: + - str, The UUID of the VM that should be exported. + - One of `export_vm_as_ovf_vm_uuid`, `export_vm_as_ovf_vm_name`, or `export_vm_as_ovf_vm_moid` needs to be defined. + +### Export Options +- **export_vm_as_ovf_download_timeout**: + - int, The maximum number of seconds that the OVF export process can take before a timeout error is thrown. + +- **export_vm_as_ovf_export_dir**: + - str, Required. The local path to the directory where the OVF file should be created. + - If the path does not exist, it will be created + +- **export_vm_as_ovf_export_with_extra_config**: + - bool, If true any extra configuration settings applied to the VM will be included in the exported OVF + +- **export_vm_as_ovf_export_with_images**: + - bool, If true any ISO files attached to the VM will be included in the exported OVF + +### Other +- **export_vm_as_ovf_proxy_host**: + - str, The hostname of a proxy host that should be used for all HTTPs communication by the role. Optional + +- **export_vm_as_ovf_proxy_port**: + - str, The port of a proxy host that should be used for all HTTPs communication by the role. Optional + + +## Dependencies + +- community.vmware + +## Example Playbook +```yaml +--- +- name: Export VM To Lolcahost + hosts: localhost + gather_facts: false + + roles: + - role: cloud.vmware_ops.export_vm_as_ovf + export_vm_as_ovf_vm_name: my-test-vm + export_vm_as_ovf_export_dir: /tmp/my-test-vm-ovf + export_vm_as_ovf_datacenter: DC1 + export_vm_as_ovf_hostname: <> + export_vm_as_ovf_password: <> + export_vm_as_ovf_username: <> + +``` +## License + +GNU General Public License v3.0 or later + +See [LICENCE](https://github.com/ansible-collections/cloud.aws_troubleshooting/blob/main/LICENSE) to see the full text. + +## Author Information + +- Ansible Cloud Content Team diff --git a/roles/export_vm_as_ovf/tasks/main.yml b/roles/export_vm_as_ovf/tasks/main.yml new file mode 100644 index 00000000..c15a6689 --- /dev/null +++ b/roles/export_vm_as_ovf/tasks/main.yml @@ -0,0 +1,35 @@ +--- +- name: Check Mandatory Variables Are Defined + ansible.builtin.assert: + that: + - export_vm_as_ovf_hostname is defined + - export_vm_as_ovf_username is defined + - export_vm_as_ovf_password is defined + - export_vm_as_ovf_export_dir is defined + - >- + export_vm_as_ovf_vm_name is defined or + export_vm_as_ovf_vm_uuid is defined or + export_vm_as_ovf_vm_moid is defined + quiet: true + fail_msg: Variable must be set when using this role. + +- name: Export VM As OVF + community.vmware.vmware_export_ovf: + hostname: "{{ export_vm_as_ovf_hostname }}" + username: "{{ export_vm_as_ovf_username }}" + password: "{{ export_vm_as_ovf_password }}" + validate_certs: "{{ export_vm_as_ovf_validate_certs | default(omit) }}" + port: "{{ export_vm_as_ovf_port | default(omit) }}" + proxy_host: "{{ export_vm_as_ovf_proxy_host | default(omit) }}" + proxy_port: "{{ export_vm_as_ovf_proxy_port | default(omit) }}" + + datacenter: "{{ export_vm_as_ovf_vm_datacenter_name | default(omit) }}" + folder: "{{ export_vm_as_ovf_vm_folder | default(omit) }}" + name: "{{ export_vm_as_ovf_vm_name | default(omit) }}" + uuid: "{{ export_vm_as_ovf_vm_uuid | default(omit) }}" + moid: "{{ export_vm_as_ovf_vm_moid | default(omit) }}" + + export_dir: "{{ export_vm_as_ovf_export_dir }}" + export_with_images: "{{ export_vm_as_ovf_export_with_images | default(omit) }}" + export_with_extraconfig: "{{ export_vm_as_ovf_export_with_extra_config | default(omit) }}" + download_timeout: "{{ export_vm_as_ovf_download_timeout | default(omit) }}" diff --git a/tests/integration/targets/vmware_export_vm_as_ovf/run.yml b/tests/integration/targets/vmware_export_vm_as_ovf/run.yml new file mode 100644 index 00000000..e3fc7b08 --- /dev/null +++ b/tests/integration/targets/vmware_export_vm_as_ovf/run.yml @@ -0,0 +1,23 @@ +--- +- hosts: localhost + gather_facts: no + collections: + - community.general + + tasks: + - name: Import eco-vcenter credentials + ansible.builtin.include_vars: + file: ../../integration_config.yml + tags: eco-vcenter-ci + + - name: Import simulator vars + ansible.builtin.include_vars: + file: vars.yml + tags: integration-ci + + - name: Import provision VM role + ansible.builtin.import_role: + name: export_vm_as_ovf + tags: + - eco-vcenter-ci + - integration-ci diff --git a/tests/integration/targets/vmware_export_vm_as_ovf/runme.sh b/tests/integration/targets/vmware_export_vm_as_ovf/runme.sh new file mode 100755 index 00000000..a3298746 --- /dev/null +++ b/tests/integration/targets/vmware_export_vm_as_ovf/runme.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash +source ../init.sh + +# Extract the ansible_tags from integration_config.yml +ANSIBLE_TAGS=$(awk '/ansible_tags/ {print $2}' ../../integration_config.yml) + +# Check if the ANSIBLE_TAGS variable is set +if [[ -n "$ANSIBLE_TAGS" ]]; then + echo "ANSIBLE_TAGS is set to: $ANSIBLE_TAGS" + exec ansible-playbook run.yml --tags "$ANSIBLE_TAGS" +else + echo "ANSIBLE_TAGS is not set for Eco vCenter. Running on simulator." + exec ansible-playbook run.yml --tags integration-ci +fi diff --git a/tests/integration/targets/vmware_export_vm_as_ovf/tasks/main.yml b/tests/integration/targets/vmware_export_vm_as_ovf/tasks/main.yml new file mode 100644 index 00000000..6466ed5e --- /dev/null +++ b/tests/integration/targets/vmware_export_vm_as_ovf/tasks/main.yml @@ -0,0 +1,41 @@ +--- +- name: Create Test VM + community.vmware.vmware_guest: + hostname: "{{ export_vm_as_ovf_hostname }}" + username: "{{ export_vm_as_ovf_username }}" + password: "{{ export_vm_as_ovf_password }}" + validate_certs: false + name: "{{ export_vm_as_ovf_vm_name }}" + state: poweredoff + # networks: + # - name: "VM Network" + # device_type: "vmxnet3" + # type: "dhcp" + disk: + - size_gb: 5 + type: thin + datastore: "datastore1" + # provision_vm_hardware: + # memory_mb: 2000 + # num_cpus: 4 + # boot_firmware: efi + # secure_boot: true + guest_id: rhel9_64Guest + +- name: Test Export VM As OVF + ansible.builtin.import_role: + name: cloud.vmware_ops.export_vm_as_ovf + +- name: Destroy Test VM + community.vmware.vmware_guest: + hostname: "{{ export_vm_as_ovf_hostname }}" + username: "{{ export_vm_as_ovf_username }}" + password: "{{ export_vm_as_ovf_password }}" + validate_certs: false + name: "{{ export_vm_as_ovf_vm_name }}" + state: absent + +- name: Delete Exported OVF + ansible.builtin.file: + state: absent + file: "{{ export_vm_as_ovf_export_dir }}" diff --git a/tests/integration/targets/vmware_export_vm_as_ovf/vars/main.yml b/tests/integration/targets/vmware_export_vm_as_ovf/vars/main.yml new file mode 100644 index 00000000..1489fbb3 --- /dev/null +++ b/tests/integration/targets/vmware_export_vm_as_ovf/vars/main.yml @@ -0,0 +1,9 @@ +--- +export_vm_as_ovf_hostname: "{{ vcenter_hostname }}" +export_vm_as_ovf_username: "{{ vcenter_username }}" +export_vm_as_ovf_password: "{{ vcenter_password }}" +export_vm_as_ovf_validate_certs: false +export_vm_as_ovf_datacenter_name: Eco-Datacenter + +export_vm_as_ovf_export_dir: /tmp/export_vm_as_ovf +export_vm_as_ovf_vm_name: export-vm-as-ovf-test From 2e429dd64c35dbf75be761539f723da9bf94b8ea Mon Sep 17 00:00:00 2001 From: Mike Morency Date: Tue, 11 Jun 2024 08:53:49 -0400 Subject: [PATCH 2/5] test fixes --- .../vmware_export_vm_as_ovf/tasks/main.yml | 41 ------------------ .../run.yml | 10 +---- .../runme.sh | 0 .../tasks/main.yml | 43 +++++++++++++++++++ .../vars/main.yml | 0 5 files changed, 45 insertions(+), 49 deletions(-) delete mode 100644 tests/integration/targets/vmware_export_vm_as_ovf/tasks/main.yml rename tests/integration/targets/{vmware_export_vm_as_ovf => vmware_export_vm_as_ovf_test}/run.yml (59%) rename tests/integration/targets/{vmware_export_vm_as_ovf => vmware_export_vm_as_ovf_test}/runme.sh (100%) create mode 100644 tests/integration/targets/vmware_export_vm_as_ovf_test/tasks/main.yml rename tests/integration/targets/{vmware_export_vm_as_ovf => vmware_export_vm_as_ovf_test}/vars/main.yml (100%) diff --git a/tests/integration/targets/vmware_export_vm_as_ovf/tasks/main.yml b/tests/integration/targets/vmware_export_vm_as_ovf/tasks/main.yml deleted file mode 100644 index 6466ed5e..00000000 --- a/tests/integration/targets/vmware_export_vm_as_ovf/tasks/main.yml +++ /dev/null @@ -1,41 +0,0 @@ ---- -- name: Create Test VM - community.vmware.vmware_guest: - hostname: "{{ export_vm_as_ovf_hostname }}" - username: "{{ export_vm_as_ovf_username }}" - password: "{{ export_vm_as_ovf_password }}" - validate_certs: false - name: "{{ export_vm_as_ovf_vm_name }}" - state: poweredoff - # networks: - # - name: "VM Network" - # device_type: "vmxnet3" - # type: "dhcp" - disk: - - size_gb: 5 - type: thin - datastore: "datastore1" - # provision_vm_hardware: - # memory_mb: 2000 - # num_cpus: 4 - # boot_firmware: efi - # secure_boot: true - guest_id: rhel9_64Guest - -- name: Test Export VM As OVF - ansible.builtin.import_role: - name: cloud.vmware_ops.export_vm_as_ovf - -- name: Destroy Test VM - community.vmware.vmware_guest: - hostname: "{{ export_vm_as_ovf_hostname }}" - username: "{{ export_vm_as_ovf_username }}" - password: "{{ export_vm_as_ovf_password }}" - validate_certs: false - name: "{{ export_vm_as_ovf_vm_name }}" - state: absent - -- name: Delete Exported OVF - ansible.builtin.file: - state: absent - file: "{{ export_vm_as_ovf_export_dir }}" diff --git a/tests/integration/targets/vmware_export_vm_as_ovf/run.yml b/tests/integration/targets/vmware_export_vm_as_ovf_test/run.yml similarity index 59% rename from tests/integration/targets/vmware_export_vm_as_ovf/run.yml rename to tests/integration/targets/vmware_export_vm_as_ovf_test/run.yml index e3fc7b08..ecea1eb2 100644 --- a/tests/integration/targets/vmware_export_vm_as_ovf/run.yml +++ b/tests/integration/targets/vmware_export_vm_as_ovf_test/run.yml @@ -10,14 +10,8 @@ file: ../../integration_config.yml tags: eco-vcenter-ci - - name: Import simulator vars - ansible.builtin.include_vars: - file: vars.yml - tags: integration-ci - - - name: Import provision VM role + - name: Import Export VM As OVF role ansible.builtin.import_role: - name: export_vm_as_ovf + name: vmware_export_vm_as_ovf_test tags: - eco-vcenter-ci - - integration-ci diff --git a/tests/integration/targets/vmware_export_vm_as_ovf/runme.sh b/tests/integration/targets/vmware_export_vm_as_ovf_test/runme.sh similarity index 100% rename from tests/integration/targets/vmware_export_vm_as_ovf/runme.sh rename to tests/integration/targets/vmware_export_vm_as_ovf_test/runme.sh diff --git a/tests/integration/targets/vmware_export_vm_as_ovf_test/tasks/main.yml b/tests/integration/targets/vmware_export_vm_as_ovf_test/tasks/main.yml new file mode 100644 index 00000000..9f555613 --- /dev/null +++ b/tests/integration/targets/vmware_export_vm_as_ovf_test/tasks/main.yml @@ -0,0 +1,43 @@ +--- +- name: Test + tags: + - eco-vcenter-ci + block: + - name: Create Test VM + community.vmware.vmware_guest: + hostname: "{{ export_vm_as_ovf_hostname }}" + username: "{{ export_vm_as_ovf_username }}" + password: "{{ export_vm_as_ovf_password }}" + validate_certs: false + name: "{{ export_vm_as_ovf_vm_name }}" + state: poweredoff + folder: /Eco-Datacenter/vm/e2e-qe + datacenter: "{{ export_vm_as_ovf_datacenter_name }}" + # networks: + # - name: "VM Network" + # device_type: "vmxnet3" + # type: "dhcp" + disk: + - size_gb: 5 + type: thick + datastore: "datastore1" + hardware: + memory_mb: 2000 + num_cpus: 2 + guest_id: rhel9_64Guest + - name: Test Export VM As OVF + ansible.builtin.import_role: + name: cloud.vmware_ops.export_vm_as_ovf + always: + - name: Destroy Test VM + community.vmware.vmware_guest: + hostname: "{{ export_vm_as_ovf_hostname }}" + username: "{{ export_vm_as_ovf_username }}" + password: "{{ export_vm_as_ovf_password }}" + validate_certs: false + name: "{{ export_vm_as_ovf_vm_name }}" + state: absent + - name: Delete Exported OVF + ansible.builtin.file: + state: absent + path: "{{ export_vm_as_ovf_export_dir }}" diff --git a/tests/integration/targets/vmware_export_vm_as_ovf/vars/main.yml b/tests/integration/targets/vmware_export_vm_as_ovf_test/vars/main.yml similarity index 100% rename from tests/integration/targets/vmware_export_vm_as_ovf/vars/main.yml rename to tests/integration/targets/vmware_export_vm_as_ovf_test/vars/main.yml From 58e0a114823897018efee04040fae577fa04a84f Mon Sep 17 00:00:00 2001 From: Mike Morency Date: Tue, 11 Jun 2024 08:55:59 -0400 Subject: [PATCH 3/5] test fixes --- .../targets/vmware_export_vm_as_ovf_test/tasks/main.yml | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/tests/integration/targets/vmware_export_vm_as_ovf_test/tasks/main.yml b/tests/integration/targets/vmware_export_vm_as_ovf_test/tasks/main.yml index 9f555613..38744efb 100644 --- a/tests/integration/targets/vmware_export_vm_as_ovf_test/tasks/main.yml +++ b/tests/integration/targets/vmware_export_vm_as_ovf_test/tasks/main.yml @@ -13,14 +13,10 @@ state: poweredoff folder: /Eco-Datacenter/vm/e2e-qe datacenter: "{{ export_vm_as_ovf_datacenter_name }}" - # networks: - # - name: "VM Network" - # device_type: "vmxnet3" - # type: "dhcp" disk: - size_gb: 5 type: thick - datastore: "datastore1" + autoselect_datastore: True hardware: memory_mb: 2000 num_cpus: 2 From b582da47c2262937c351991cf808b9a405e42c81 Mon Sep 17 00:00:00 2001 From: Mike Morency Date: Thu, 13 Jun 2024 08:35:57 -0400 Subject: [PATCH 4/5] rename test and changelog --- changelogs/fragments/55__mm-feature_export_vm_as_ovf.yml | 3 +++ roles/export_vm_as_ovf/README.md | 6 +++--- .../run.yml | 0 .../runme.sh | 0 .../tasks/main.yml | 0 .../vars/main.yml | 0 6 files changed, 6 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/55__mm-feature_export_vm_as_ovf.yml rename tests/integration/targets/{vmware_export_vm_as_ovf_test => vmware_ops_export_vm_as_ovf_test}/run.yml (100%) rename tests/integration/targets/{vmware_export_vm_as_ovf_test => vmware_ops_export_vm_as_ovf_test}/runme.sh (100%) rename tests/integration/targets/{vmware_export_vm_as_ovf_test => vmware_ops_export_vm_as_ovf_test}/tasks/main.yml (100%) rename tests/integration/targets/{vmware_export_vm_as_ovf_test => vmware_ops_export_vm_as_ovf_test}/vars/main.yml (100%) diff --git a/changelogs/fragments/55__mm-feature_export_vm_as_ovf.yml b/changelogs/fragments/55__mm-feature_export_vm_as_ovf.yml new file mode 100644 index 00000000..e31deda7 --- /dev/null +++ b/changelogs/fragments/55__mm-feature_export_vm_as_ovf.yml @@ -0,0 +1,3 @@ +--- +minor_changes: + - export_vm_as_ovf - added role, playbook, tests to export an exisiting VM from VCenter or ESXi as an OVF diff --git a/roles/export_vm_as_ovf/README.md b/roles/export_vm_as_ovf/README.md index fb0d624b..58ccf75e 100644 --- a/roles/export_vm_as_ovf/README.md +++ b/roles/export_vm_as_ovf/README.md @@ -21,13 +21,13 @@ N/A - Allows connection when SSL certificates are not valid. Set to false when certificates are not trusted. - **export_vm_as_ovf_cluster_name**: - - The name of the cluster in vSphere vCenter to configure. + - The name of the cluster in vSphere vCenter that contains the VM. - **export_vm_as_ovf_datacenter_name**: - - The name of the datacenter in vSphere vCenter which contains the cluster to configure. + - The name of the datacenter in vSphere vCenter which contains the VM. - **export_vm_as_ovf_port**: - - str or int, The port to use to authenticate to the vSphere vCenter which contains the cluster to configure. + - str or int, The port to use to authenticate to the vSphere vCenter which contains the VM. ### VM Options - **export_vm_as_ovf_vm_datacenter**: diff --git a/tests/integration/targets/vmware_export_vm_as_ovf_test/run.yml b/tests/integration/targets/vmware_ops_export_vm_as_ovf_test/run.yml similarity index 100% rename from tests/integration/targets/vmware_export_vm_as_ovf_test/run.yml rename to tests/integration/targets/vmware_ops_export_vm_as_ovf_test/run.yml diff --git a/tests/integration/targets/vmware_export_vm_as_ovf_test/runme.sh b/tests/integration/targets/vmware_ops_export_vm_as_ovf_test/runme.sh similarity index 100% rename from tests/integration/targets/vmware_export_vm_as_ovf_test/runme.sh rename to tests/integration/targets/vmware_ops_export_vm_as_ovf_test/runme.sh diff --git a/tests/integration/targets/vmware_export_vm_as_ovf_test/tasks/main.yml b/tests/integration/targets/vmware_ops_export_vm_as_ovf_test/tasks/main.yml similarity index 100% rename from tests/integration/targets/vmware_export_vm_as_ovf_test/tasks/main.yml rename to tests/integration/targets/vmware_ops_export_vm_as_ovf_test/tasks/main.yml diff --git a/tests/integration/targets/vmware_export_vm_as_ovf_test/vars/main.yml b/tests/integration/targets/vmware_ops_export_vm_as_ovf_test/vars/main.yml similarity index 100% rename from tests/integration/targets/vmware_export_vm_as_ovf_test/vars/main.yml rename to tests/integration/targets/vmware_ops_export_vm_as_ovf_test/vars/main.yml From 2b47b85927ba3db56091bd38b7fa48c188b6bfe5 Mon Sep 17 00:00:00 2001 From: Mike Morency Date: Thu, 13 Jun 2024 08:36:56 -0400 Subject: [PATCH 5/5] test update --- .../targets/vmware_ops_export_vm_as_ovf_test/run.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/targets/vmware_ops_export_vm_as_ovf_test/run.yml b/tests/integration/targets/vmware_ops_export_vm_as_ovf_test/run.yml index ecea1eb2..699bb862 100644 --- a/tests/integration/targets/vmware_ops_export_vm_as_ovf_test/run.yml +++ b/tests/integration/targets/vmware_ops_export_vm_as_ovf_test/run.yml @@ -12,6 +12,6 @@ - name: Import Export VM As OVF role ansible.builtin.import_role: - name: vmware_export_vm_as_ovf_test + name: vmware_ops_export_vm_as_ovf_test tags: - eco-vcenter-ci