diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c482b094..7243c7f2 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -18,10 +18,7 @@ jobs: run: | set -euxo pipefail sudo apt-get update - - # recent version of podman has a bug. Fix is waiting deployment - # see https://github.com/actions/runner-images/issues/7753 - sudo apt-get install podman=3.4.4+ds1-1ubuntu1 --allow-downgrades + sudo apt-get install podman - name: Checkout repo uses: actions/checkout@v4 diff --git a/changelogs/fragments/120-feature-upload-iso-to-datastore.yml b/changelogs/fragments/120-feature-upload-iso-to-datastore.yml new file mode 100644 index 00000000..7bbf511c --- /dev/null +++ b/changelogs/fragments/120-feature-upload-iso-to-datastore.yml @@ -0,0 +1,3 @@ +--- +minor_changes: + - upload_iso_to_datastore - Created role intended for uploading local ISO files to a Datastore \ No newline at end of file diff --git a/playbooks/upload_iso_to_datastore.yml b/playbooks/upload_iso_to_datastore.yml new file mode 100644 index 00000000..c3ec775b --- /dev/null +++ b/playbooks/upload_iso_to_datastore.yml @@ -0,0 +1,7 @@ +--- +- name: Upload local ISO file to Datastore + hosts: all + gather_facts: false + + roles: + - role: cloud.vmware_ops.upload_iso_to_datastore diff --git a/roles/upload_iso_to_datastore/README.md b/roles/upload_iso_to_datastore/README.md new file mode 100644 index 00000000..c4f1d521 --- /dev/null +++ b/roles/upload_iso_to_datastore/README.md @@ -0,0 +1,111 @@ +# upload_iso_to_datastore +A role intended for uploading local ISO files to a Datastore. + + +## Dependencies + +N/A + + +## Role Variables + +### Auth + +- **upload_iso_to_datastore_hostname** (str, required) + - The hostname or IP address of the vSphere vCenter or ESXi host. + - If this variable is not set, the collection level variable `vmware_ops_hostname` will be used. If that variable is not set, the environment variable `VMWARE_HOST` will be used. At least one of these variables must be set to use this role. + - See the [authentication documentation](https://github.com/redhat-cop/cloud.vmware_ops/blob/main/docs/authentication.md) for examples. + +- **upload_iso_to_datastore_username** (str, required) + - The vSphere vCenter or ESXi host username. + - If this variable is not set, the collection level variable `vmware_ops_username` will be used. If that variable is not set, the environment variable `VMWARE_USER` will be used. At least one of these variables must be set to use this role. + - See the [authentication documentation](https://github.com/redhat-cop/cloud.vmware_ops/blob/main/docs/authentication.md) for examples. + +- **upload_iso_to_datastore_password** (str, required) + - The vSphere vCenter or ESXi host password. + - If this variable is not set, the collection level variable `vmware_ops_password` will be used. If that variable is not set, the environment variable `VMWARE_PASSWORD` will be used. At least one of these variables must be set to use this role. + - See the [authentication documentation](https://github.com/redhat-cop/cloud.vmware_ops/blob/main/docs/authentication.md) for examples. + +- **upload_iso_to_datastore_validate_certs** (bool) + - Allows connection when SSL certificates are not valid. Set to false when certificates are not trusted. + - If this variable is not set, the collection level variable `vmware_ops_validate_certs` will be used. If that variable is not set, the environment variable `VMWARE_VALIDATE_CERTS` will be used. + - See the [authentication documentation](https://github.com/redhat-cop/cloud.vmware_ops/blob/main/docs/authentication.md) for examples. + +- **upload_iso_to_datastore_port** (int or str) + - The port used to authenticate to the vSphere vCenter or ESXi host. + - If this variable is not set, the collection level variable `vmware_ops_port` will be used. If that variable is not set, the environment variable `VMWARE_PORT` will be used. + - See the [authentication documentation](https://github.com/redhat-cop/cloud.vmware_ops/blob/main/docs/authentication.md) for examples. + +### Proxy + +- **upload_iso_to_datastore_proxy_host** (str) + - The hostname of a proxy host that should be used for all HTTPs communication by the role. + - The format is a hostname or an IP. + - If this variable is not set, the collection level variable `vmware_ops_proxy_host` will be used. If that variable is not set, the environment variable `VMWARE_PROXY_HOST` will be used. + - See the [authentication documentation](https://github.com/redhat-cop/cloud.vmware_ops/blob/main/docs/authentication.md) for examples. + +- **upload_iso_to_datastore_proxy_port** (str or int) + - The port of a proxy host that should be used for all HTTPs communication by the role + - If this variable is not set, the collection level variable `vmware_ops_proxy_host` will be used. If that variable is not set, the environment variable `VMWARE_PROXY_PORT` will be used. + - See the [authentication documentation](https://github.com/redhat-cop/cloud.vmware_ops/blob/main/docs/authentication.md) for examples. + +### Uploading an ISO file + +- **upload_iso_to_datastore_src** (str, required) + - Absolute path to the ISO file to upload. + - This parameter is case sensitive. + +- **upload_iso_to_datastore_datacenter** (str, required) + - The name of the datacenter where the Datastore resides. + - This parameter is case sensitive. + +- **upload_iso_to_datastore_datastore** (str, required) + - The ISO will be uploaded into this Datastore. + - Please see the examples for more usage. + +- **upload_iso_to_datastore_dst** (str, required) + - The ISO file will be uploaded into this destination path within the Datastore. + - This parameter is case sensitive. + +## Examples + +All the variables defined in section [Role Variables](#role-variables) can be defined inside the ``vars.yml`` file. + +Create a ``playbook.yml`` file like this: + +```yaml +--- +- hosts: localhost + gather_facts: true + + tasks: + - name: Upload ISO to Datastore + ansible.builtin.import_role: + name: cloud.vmware_ops.upload_iso_to_datastore + vars: + upload_iso_to_datastore_hostname: "test" + upload_iso_to_datastore_username: "test" + upload_iso_to_datastore_password: "test" + upload_iso_to_datastore_validate_certs: false + upload_iso_to_datastore_datacenter: "DC0" + upload_iso_to_datastore_port: "8989" + upload_iso_to_datastore_datastore: "eco-iscsi-ds1" + upload_iso_to_datastore_src: "/tmp/test.iso" + upload_iso_to_datastore_dst: "/test.iso" +``` + +Run the playbook: + +```shell +ansible-playbook playbook.yml -e "@vars.yml" +``` + +## License + +GNU General Public License v3.0 or later + +See [LICENSE](https://github.com/ansible-collections/cloud.vmware_ops/blob/main/LICENSE) to see the full text. + +## Author Information + +- Ansible Cloud Content Team diff --git a/roles/upload_iso_to_datastore/defaults/main.yml b/roles/upload_iso_to_datastore/defaults/main.yml new file mode 100644 index 00000000..2fa9463f --- /dev/null +++ b/roles/upload_iso_to_datastore/defaults/main.yml @@ -0,0 +1,9 @@ +--- +upload_iso_to_datastore_hostname: "{{ vmware_ops_collection_hostname }}" +upload_iso_to_datastore_username: "{{ vmware_ops_collection_username }}" +upload_iso_to_datastore_password: "{{ vmware_ops_collection_password }}" +upload_iso_to_datastore_validate_certs: "{{ vmware_ops_collection_validate_certs }}" +upload_iso_to_datastore_port: "{{ vmware_ops_collection_port }}" + +upload_iso_to_datastore_proxy_host: "{{ vmware_ops_collection_proxy_host }}" +upload_iso_to_datastore_proxy_port: "{{ vmware_ops_collection_proxy_port }}" diff --git a/roles/upload_iso_to_datastore/meta/main.yml b/roles/upload_iso_to_datastore/meta/main.yml new file mode 100644 index 00000000..a3ce207f --- /dev/null +++ b/roles/upload_iso_to_datastore/meta/main.yml @@ -0,0 +1,12 @@ +--- +galaxy_info: + author: Ansible Cloud Content Team (@redhat-cop) + description: Upload an ISO file to a Datastore in vCenter + company: Red Hat, Inc. + license: GPL-3.0-or-later + min_ansible_version: "2.13" + galaxy_tags: + - infrastructure + - vmware + +dependencies: [] diff --git a/roles/upload_iso_to_datastore/tasks/main.yml b/roles/upload_iso_to_datastore/tasks/main.yml new file mode 100644 index 00000000..69588e8d --- /dev/null +++ b/roles/upload_iso_to_datastore/tasks/main.yml @@ -0,0 +1,28 @@ +--- +- name: Import Collection Level Vars + ansible.builtin.include_vars: + file: "{{ role_path }}/../../vars/main.yml" + +- name: Check Mandatory Variables Are Defined + ansible.builtin.assert: + that: + - upload_iso_to_datastore_hostname is defined + - upload_iso_to_datastore_username is defined + - upload_iso_to_datastore_password is defined + - upload_iso_to_datastore_src is defined + - upload_iso_to_datastore_dst is defined + - upload_iso_to_datastore_datastore is defined + - upload_iso_to_datastore_datacenter is defined + quiet: true + fail_msg: Variable must be set when using this role. + +- name: Upload local ISO file to Datastore + community.vmware.vsphere_copy: + hostname: "{{ upload_iso_to_datastore_hostname }}" + username: "{{ upload_iso_to_datastore_username }}" + password: "{{ upload_iso_to_datastore_password }}" + src: "{{ upload_iso_to_datastore_src }}" + datacenter: "{{ upload_iso_to_datastore_datacenter }}" + datastore: "{{ upload_iso_to_datastore_datastore }}" + path: "{{ upload_iso_to_datastore_dst }}" + validate_certs: "{{ upload_iso_to_datastore_validate_certs }}" diff --git a/tests/integration/targets/vmware_ops_upload_iso_to_datastore_test/run.yml b/tests/integration/targets/vmware_ops_upload_iso_to_datastore_test/run.yml new file mode 100644 index 00000000..572c6fee --- /dev/null +++ b/tests/integration/targets/vmware_ops_upload_iso_to_datastore_test/run.yml @@ -0,0 +1,15 @@ +--- +- hosts: localhost + gather_facts: no + + tasks: + - name: Import eco-vcenter credentials + ansible.builtin.include_vars: + file: ../../integration_config.yml + tags: eco-vcenter-ci + + - name: Import Upload ISO to Datastore role + ansible.builtin.import_role: + name: vmware_ops_upload_iso_to_datastore_test + tags: + - eco-vcenter-ci diff --git a/tests/integration/targets/vmware_ops_upload_iso_to_datastore_test/tasks/main.yml b/tests/integration/targets/vmware_ops_upload_iso_to_datastore_test/tasks/main.yml new file mode 100644 index 00000000..5b1fe8ad --- /dev/null +++ b/tests/integration/targets/vmware_ops_upload_iso_to_datastore_test/tasks/main.yml @@ -0,0 +1,57 @@ +--- +- name: Import common vars + ansible.builtin.include_vars: + file: ../group_vars.yml + +- name: Create ISO Mocks + block: + - name: Mock VCSA Binary + ansible.builtin.include_tasks: mock_vcsa_binary.yml + always: + - name: Clean Up Temp Files + ansible.builtin.file: + path: "{{ upload_iso_to_datastore_test_mock_binary_root }}" + state: absent + +- name: Test ISO Upload to Datastore + block: + - name: Upload ISO to Datastore + ansible.builtin.import_role: + name: cloud.vmware_ops.upload_iso_to_datastore + + - name: Attempt to recreate ISO file + community.vmware.vsphere_file: + hostname: '{{ vmware_ops_hostname }}' + username: '{{ vmware_ops_username }}' + password: '{{ vmware_ops_password }}' + datacenter: "{{ upload_iso_to_datastore_datacenter }}" + datastore: "{{ upload_iso_to_datastore_datastore }}" + path: "{{ upload_iso_to_datastore_dst }}" + state: file + validate_certs: "{{ vmware_ops_validate_certs }}" + ignore_errors: true + register: file_result + + - name: Check ISO was copied to Datastore + ansible.builtin.assert: + that: + - file_result is not changed + quiet: true + fail_msg: Copy of ISO was unsuccessful + + always: + - name: Clean Up Temp ISO + ansible.builtin.file: + path: "{{ upload_iso_to_datastore_src }}" + state: absent + + - name: Delete ISO file from Datastore + community.vmware.vsphere_file: + hostname: "{{ vmware_ops_hostname }}" + username: "{{ vmware_ops_username }}" + password: "{{ vmware_ops_password }}" + state: absent + datacenter: "{{ upload_iso_to_datastore_datacenter }}" + datastore: "{{ upload_iso_to_datastore_datastore }}" + path: "{{ upload_iso_to_datastore_dst }}" + validate_certs: false \ No newline at end of file diff --git a/tests/integration/targets/vmware_ops_upload_iso_to_datastore_test/tasks/mock_vcsa_binary.yml b/tests/integration/targets/vmware_ops_upload_iso_to_datastore_test/tasks/mock_vcsa_binary.yml new file mode 100644 index 00000000..9995b220 --- /dev/null +++ b/tests/integration/targets/vmware_ops_upload_iso_to_datastore_test/tasks/mock_vcsa_binary.yml @@ -0,0 +1,12 @@ +--- +- name: Create Mock VCSA Deployment Binary Path + ansible.builtin.file: + path: "{{ upload_iso_to_datastore_test_mock_binary_root }}/lin64" + state: directory + +- name: Create Mock VCSA ISO file + community.general.iso_create: + src_files: + - "{{ upload_iso_to_datastore_test_mock_binary_root }}" + dest_iso: "{{ upload_iso_to_datastore_src }}" + interchange_level: 4 diff --git a/tests/integration/targets/vmware_ops_upload_iso_to_datastore_test/vars/main.yml b/tests/integration/targets/vmware_ops_upload_iso_to_datastore_test/vars/main.yml new file mode 100644 index 00000000..e967685a --- /dev/null +++ b/tests/integration/targets/vmware_ops_upload_iso_to_datastore_test/vars/main.yml @@ -0,0 +1,10 @@ +--- +run_on_simulator: false + +upload_iso_to_datastore_datacenter: "{{ vcenter_datacenter }}" +upload_iso_to_datastore_datastore: "{{ shared_storage_01 }}" +upload_iso_to_datastore_src: "/tmp/{{ tiny_prefix }}-test.iso" +folder_dst: "/upload_iso_to_datastore" +upload_iso_to_datastore_dst: "{{ folder_dst }}/{{ tiny_prefix }}-test.iso" + +upload_iso_to_datastore_test_mock_binary_root: /tmp/vcsa-cli-installer \ No newline at end of file