Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding export vm as ovf role #55

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelogs/fragments/55__mm-feature_export_vm_as_ovf.yml
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions playbooks/export_vm_as_ovf.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- name: Export VM as OVF File
hosts: all
gather_facts: false

roles:
- role: cloud.vmware_ops.export_vm_as_ovf
102 changes: 102 additions & 0 deletions roles/export_vm_as_ovf/README.md
Original file line number Diff line number Diff line change
@@ -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 that contains the VM.

- **export_vm_as_ovf_datacenter_name**:
- 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 VM.

### 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
35 changes: 35 additions & 0 deletions roles/export_vm_as_ovf/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -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
Copy link
Contributor

@bardielle bardielle Jun 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the vCenter UI this operation is called: deploy ovf template... maybe it is better to use the UI operation name. let's ask the others @machacekondra @shellymiron @elsapassaro

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think that is the opposite operation, deploying ovf template means create a VM from an OVF
https://docs.vmware.com/en/VMware-vSphere/7.0/com.vmware.vsphere.vm_admin.doc/GUID-17BEDA21-43F6-41F4-8FB2-E01D275FE9B4.html

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) }}"
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
- 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 Export VM As OVF role
ansible.builtin.import_role:
name: vmware_ops_export_vm_as_ovf_test
tags:
- eco-vcenter-ci
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
- 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 }}"
disk:
- size_gb: 5
type: thick
autoselect_datastore: True
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 }}"
Original file line number Diff line number Diff line change
@@ -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
Loading