From 7ecdc35b8e390085f97ab92a56e64d21e29b12ec Mon Sep 17 00:00:00 2001 From: Danielle Barda Date: Thu, 15 Feb 2024 15:24:09 +0200 Subject: [PATCH] Provisioning VM role --- playbooks/provision_vm/provision_vm.yml | 20 ----- .../provisioning_vm_from_scratch.yml | 20 +++++ .../provisioning_vm_from_template.yml | 20 +++++ .../provision_vm/provisioning_vm_from_vm.yml | 20 +++++ roles/create_vm_template/README.md | 69 ++++++++++++++++ roles/create_vm_template/defaults/main.yml | 3 + .../tasks/clone_template_to_template.yml | 0 .../tasks/clone_vm_to_template.yml | 0 roles/create_vm_template/tasks/main.yml | 14 ++++ .../validation/fail_if_vm_already_exists.yml | 19 +++++ .../validation/fail_if_vm_does_not_exist.yml | 0 roles/provision_vm/README.md | 69 ++++++++++++++++ roles/provision_vm/defaults/main.yml | 3 + .../tasks/clone_an_existing_vm.yml | 82 +++++++++++++++++++ roles/provision_vm/tasks/create_new_vm.yml | 59 +++++++++++++ .../tasks/deploy_from_template.yml | 65 +++++++++++++++ roles/provision_vm/tasks/main.yml | 29 +++++++ 17 files changed, 472 insertions(+), 20 deletions(-) delete mode 100644 playbooks/provision_vm/provision_vm.yml create mode 100644 playbooks/provision_vm/provisioning_vm_from_scratch.yml create mode 100644 playbooks/provision_vm/provisioning_vm_from_template.yml create mode 100644 playbooks/provision_vm/provisioning_vm_from_vm.yml create mode 100644 roles/create_vm_template/README.md create mode 100644 roles/create_vm_template/defaults/main.yml create mode 100644 roles/create_vm_template/tasks/clone_template_to_template.yml create mode 100644 roles/create_vm_template/tasks/clone_vm_to_template.yml create mode 100644 roles/create_vm_template/tasks/main.yml create mode 100644 roles/create_vm_template/tasks/validation/fail_if_vm_already_exists.yml create mode 100644 roles/create_vm_template/tasks/validation/fail_if_vm_does_not_exist.yml create mode 100644 roles/provision_vm/README.md create mode 100644 roles/provision_vm/defaults/main.yml create mode 100644 roles/provision_vm/tasks/clone_an_existing_vm.yml create mode 100644 roles/provision_vm/tasks/create_new_vm.yml create mode 100644 roles/provision_vm/tasks/deploy_from_template.yml create mode 100644 roles/provision_vm/tasks/main.yml diff --git a/playbooks/provision_vm/provision_vm.yml b/playbooks/provision_vm/provision_vm.yml deleted file mode 100644 index 1b7f7d77..00000000 --- a/playbooks/provision_vm/provision_vm.yml +++ /dev/null @@ -1,20 +0,0 @@ -- name: Playbook to provision a VM on VMware - hosts: localhost - gather_facts: false - - tasks: - - name: set connection info - ansible.builtin.set_fact: - connection_args: - vcenter_hostname: "" - vcenter_username: "" - vcenter_password: "" - vcenter_validate_certs: "false" - - - name: Test urlEncoded issue with list datasoruces - vmware.vmware_rest.vcenter_datacenter_info: - vcenter_hostname: "{{ connection_args.vcenter_hostname }}" - vcenter_username: "{{ connection_args.vcenter_username }}" - vcenter_password: "{{ connection_args.vcenter_password }}" - vcenter_validate_certs: "false" - register: create_vsphere_vm_datacenter diff --git a/playbooks/provision_vm/provisioning_vm_from_scratch.yml b/playbooks/provision_vm/provisioning_vm_from_scratch.yml new file mode 100644 index 00000000..83a9157b --- /dev/null +++ b/playbooks/provision_vm/provisioning_vm_from_scratch.yml @@ -0,0 +1,20 @@ +--- +- name: Playbook to provision a new VM on VMware + hosts: all + gather_facts: false + + vars: + clone_from_template: false + clone_from_vm: false + + tasks: + - name: set connection info + ansible.builtin.set_fact: + connection_args: + vcenter_hostname: "{{ vcenter_hostname }}" + vcenter_username: "{{ vcenter_username }}" + vcenter_password: "{{ vcenter_password }}" + vcenter_validate_certs: "false" + + - roles: + - role: redhat_cop.vmware_ops.provisioning_vm diff --git a/playbooks/provision_vm/provisioning_vm_from_template.yml b/playbooks/provision_vm/provisioning_vm_from_template.yml new file mode 100644 index 00000000..94500970 --- /dev/null +++ b/playbooks/provision_vm/provisioning_vm_from_template.yml @@ -0,0 +1,20 @@ +--- +- name: Playbook to provision a new VM from template on VMware + hosts: all + gather_facts: false + + vars: + clone_from_template: true + clone_from_vm: false + + tasks: + - name: set connection info + ansible.builtin.set_fact: + connection_args: + vcenter_hostname: "{{ vcenter_hostname }}" + vcenter_username: "{{ vcenter_username }}" + vcenter_password: "{{ vcenter_password }}" + vcenter_validate_certs: "false" + + - roles: + - role: redhat_cop.vmware_ops.provisioning_vm diff --git a/playbooks/provision_vm/provisioning_vm_from_vm.yml b/playbooks/provision_vm/provisioning_vm_from_vm.yml new file mode 100644 index 00000000..492faa4a --- /dev/null +++ b/playbooks/provision_vm/provisioning_vm_from_vm.yml @@ -0,0 +1,20 @@ +--- +- name: Playbook to provision a new VM from an existing VM on VMware + hosts: all + gather_facts: false + + vars: + clone_from_template: false + clone_from_vm: true + + tasks: + - name: set connection info + ansible.builtin.set_fact: + connection_args: + vcenter_hostname: "{{ vcenter_hostname }}" + vcenter_username: "{{ vcenter_username }}" + vcenter_password: "{{ vcenter_password }}" + vcenter_validate_certs: "false" + + - roles: + - role: redhat_cop.vmware_ops.provisioning_vm diff --git a/roles/create_vm_template/README.md b/roles/create_vm_template/README.md new file mode 100644 index 00000000..2da805a7 --- /dev/null +++ b/roles/create_vm_template/README.md @@ -0,0 +1,69 @@ +Provision virtual machine +================ + +A role to provision a virtual machine, create associated resources if they don’t exist (subnets, vCPU, memory configuration, storage configuration, etc) +This includes cloning and building from VM templates +Boot a VM from these types: +* Windows Server +* RHEL8 +* RHEL9 + + +Requirements +------------ + +vCenter logged in permission + +Role Variables +-------------- + +* **vcenter_hostname** (str): (Required) +* **vcenter_username** (str): (Required) +* **vcenter_password** (str): (Required) +* **vcenter_validate_certs** (str): (Required) +* **provision_virtual_machine_operation** (str): (Required) Operation to perform. Valid values are: + clone_an_existing_vm, clone_template_to_template, clone_vm_to_template, create_new_vm, create_vm_from_template, + deploy_from_template. + +Dependencies +------------ + +N/A + +Example Playbook +---------------- + +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: + +``` +--- +- hosts: localhost + gather_facts: true + + tasks: + - name: Provision a VM + ansible.builtin.import_role: + name: cloud.vmware_ops.provision_virtual_machine + vars: + aa: "{{ }}" +``` + +Run the playbook: + +```shell +ansible-playbook playbook.yml -e "@vars.yml" +``` + +License +------- + +GNU General Public License v3.0 or later + +See [LICENCE](https://github.com/ansible-collections/cloud.vmware_ops/blob/main/LICENSE) to see the full text. + +Author Information +------------------ + +- ?? \ No newline at end of file diff --git a/roles/create_vm_template/defaults/main.yml b/roles/create_vm_template/defaults/main.yml new file mode 100644 index 00000000..5fd60a70 --- /dev/null +++ b/roles/create_vm_template/defaults/main.yml @@ -0,0 +1,3 @@ +--- +vcenter_validate_certs: true +guest_os: \ No newline at end of file diff --git a/roles/create_vm_template/tasks/clone_template_to_template.yml b/roles/create_vm_template/tasks/clone_template_to_template.yml new file mode 100644 index 00000000..e69de29b diff --git a/roles/create_vm_template/tasks/clone_vm_to_template.yml b/roles/create_vm_template/tasks/clone_vm_to_template.yml new file mode 100644 index 00000000..e69de29b diff --git a/roles/create_vm_template/tasks/main.yml b/roles/create_vm_template/tasks/main.yml new file mode 100644 index 00000000..ce9c4189 --- /dev/null +++ b/roles/create_vm_template/tasks/main.yml @@ -0,0 +1,14 @@ +--- +- name: set connection info + ansible.builtin.set_fact: + connection_args: + vcenter_hostname: "{{ vcenter_hostname }}" + vcenter_username: "{{ vcenter_username }}" + vcenter_password: "{{ vcenter_password }}" + vcenter_validate_certs: "{{ vcenter_validate_certs }}" + + block: + # vm name or vm template + - name: Create VM or template + ansible.builtin.include_tasks: "clone_vm_to_template.yml" + diff --git a/roles/create_vm_template/tasks/validation/fail_if_vm_already_exists.yml b/roles/create_vm_template/tasks/validation/fail_if_vm_already_exists.yml new file mode 100644 index 00000000..b2794828 --- /dev/null +++ b/roles/create_vm_template/tasks/validation/fail_if_vm_already_exists.yml @@ -0,0 +1,19 @@ +--- + + +- name: Retrieve all the details about the new VM + vmware.vmware_rest.vcenter_vm_info: + #vmware.vmware_rest.vcenter_vm: + vcenter_hostname: "{{ connection_args.vcenter_hostname }}" + vcenter_username: "{{ connection_args.vcenter_username }}" + vcenter_password: "{{ connection_args.vcenter_password }}" + vcenter_validate_certs: "{{ connection_args.vcenter_validate_certs }}" + names: + - "{{ vm_name }}" + register: vms + + +- name: Fail when the VM is already exists + ansible.builtin.fail: + msg: The VM with the same name already exists. Please provide a different VM name + when: vms.value | length > 0 \ No newline at end of file diff --git a/roles/create_vm_template/tasks/validation/fail_if_vm_does_not_exist.yml b/roles/create_vm_template/tasks/validation/fail_if_vm_does_not_exist.yml new file mode 100644 index 00000000..e69de29b diff --git a/roles/provision_vm/README.md b/roles/provision_vm/README.md new file mode 100644 index 00000000..2da805a7 --- /dev/null +++ b/roles/provision_vm/README.md @@ -0,0 +1,69 @@ +Provision virtual machine +================ + +A role to provision a virtual machine, create associated resources if they don’t exist (subnets, vCPU, memory configuration, storage configuration, etc) +This includes cloning and building from VM templates +Boot a VM from these types: +* Windows Server +* RHEL8 +* RHEL9 + + +Requirements +------------ + +vCenter logged in permission + +Role Variables +-------------- + +* **vcenter_hostname** (str): (Required) +* **vcenter_username** (str): (Required) +* **vcenter_password** (str): (Required) +* **vcenter_validate_certs** (str): (Required) +* **provision_virtual_machine_operation** (str): (Required) Operation to perform. Valid values are: + clone_an_existing_vm, clone_template_to_template, clone_vm_to_template, create_new_vm, create_vm_from_template, + deploy_from_template. + +Dependencies +------------ + +N/A + +Example Playbook +---------------- + +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: + +``` +--- +- hosts: localhost + gather_facts: true + + tasks: + - name: Provision a VM + ansible.builtin.import_role: + name: cloud.vmware_ops.provision_virtual_machine + vars: + aa: "{{ }}" +``` + +Run the playbook: + +```shell +ansible-playbook playbook.yml -e "@vars.yml" +``` + +License +------- + +GNU General Public License v3.0 or later + +See [LICENCE](https://github.com/ansible-collections/cloud.vmware_ops/blob/main/LICENSE) to see the full text. + +Author Information +------------------ + +- ?? \ No newline at end of file diff --git a/roles/provision_vm/defaults/main.yml b/roles/provision_vm/defaults/main.yml new file mode 100644 index 00000000..9e0c251f --- /dev/null +++ b/roles/provision_vm/defaults/main.yml @@ -0,0 +1,3 @@ +--- +provision_vm_clone_from_template: false +provision_vm_clone_from_vm: false diff --git a/roles/provision_vm/tasks/clone_an_existing_vm.yml b/roles/provision_vm/tasks/clone_an_existing_vm.yml new file mode 100644 index 00000000..32d908ea --- /dev/null +++ b/roles/provision_vm/tasks/clone_an_existing_vm.yml @@ -0,0 +1,82 @@ +--- +- name: Fail if the VM is not exists + block: + - name: Retrieve all the details about the new VM + vmware.vmware_rest.vcenter_vm_info: + vcenter_hostname: "{{ connection_args.vcenter_hostname }}" + vcenter_username: "{{ connection_args.vcenter_username }}" + vcenter_password: "{{ connection_args.vcenter_password }}" + vcenter_validate_certs: "{{ connection_args.vcenter_validate_certs }}" + names: + - "{{ vm_name }}" + register: vms + + - name: Fail when the VM is already exists + ansible.builtin.fail: + msg: The VM "{{ template }}" from is not exist + when: vms.value | length <1 + +- name: Create a new VM + block: + - name: Create or update the VM + community.vmware.vmware_guest: + hostname: "{{ connection_args.vcenter_hostname }}" + username: "{{ connection_args.vcenter_username }}" + password: "{{ connection_args.vcenter_password }}" + validate_certs: "{{ omit if connection_args.vcenter_validate_certs is not defined else connection_args.vcenter_validate_certs}}" + + port: "{{ omit if port is not defined else port }}" + proxy_host: "{{ omit if proxy_host is not defined else proxy_host }}" + proxy_port: "{{ omit if proxy_port is not defined else proxy_port }}" + + name: "{{ vm_name }}" + uuid: "{{ omit if uuid is not defined else uuid }}" + + # compute resource: + cluster: "{{ omit if cluster not defined else cluster }}" + esxi_hostname: "{{ omit if esxi_hostname is not defined else esxi_hostname}}" + datacenter: "{{ omit if datacenter is not defined else datacenter}}" + folder: "{{ omit if folder is not defined else folder}}" + datastore: "{{ omit if datastore is not defined else datastore}}" + resource_pool: "{{ omit if resource_pool is not defined else lookup('vmware.vmware_rest.resource_pool_moid', {{ datacenter_name }}/host/{{ cluster_name }}/Resources/{{ resource_pool_name }}, **connection_args) }}" + + # for cloning VM + template: "{{ template }}" + convert: "{{ omit if convert is not defined else convert }}" + linked_clone: "{{ omit if linked_clone is not defined else linked_clone }}" + snapshot_src: "{{ omit if snapshot_src is not defined else snapshot_src }}" + + # optional: + advanced_settings: "{{ omit if advanced_settings is not defined else advanced_settings}}" + annotation: "{{ omit if annotation is not defined else annotation}}" + cdrom: "{{ omit if cdrom is not defined else cdrom }}" + customization: "{{ omit if customization is not defined else customization }}" + customization_spec: "{{ omit if customization_spec is not defined else customization_spec }}" + customvalues: "{{ omit if customvalues is not defined else customvalues }}" + delete_from_inventory: "{{ omit if delete_from_inventory is not defined else delete_from_inventory }}" + disk: "{{ omit if disk is not defined else disk }}" + encryption: "{{ omit if encryption is not defined else encryption }}" + force: "{{ omit if force is not defined else force }}" + guest_id: "{{ omit if guest_id is not defined else guest_id }}" + hardware: "{{ omit if hardware is not defined else hardware }}" + state: "{{ omit if state is not defined else state}}" + state_change_timeout: "{{ omit if state_change_timeout is not defined else state_change_timeout}}" + vapp_properties: "{{ omit if vapp_properties is not defined else vapp_properties}}" + wait_for_customization: "{{ omit if wait_for_customization is not defined else wait_for_customization}}" + wait_for_customization_timeout: "{{ omit if wait_for_customization_timeout is not defined else wait_for_customization_timeout}}" + wait_for_ip_address: "{{ omit if wait_for_ip_address is not defined else wait_for_ip_address}}" + wait_for_ip_address_timeout: "{{ omit if wait_for_ip_address_timeout is not defined else wait_for_ip_address_timeout}}" + networks: "{{ omit if networks is not defined else networks }}" + nvdimm: "{{ omit if nvdimm is not defined else nvdimm }}" + use_instance_uuid : "{{ omit if use_instance_uuid is not defined else use_instance_uuid }}" + name_match: "{{ omit if name_match is not defined else name_match }}" + + is_template: false + register: my_vm + + # power on if power_on = true: + + + - name: Print VM information + ansible.builtin.debug: + var: my_vm \ No newline at end of file diff --git a/roles/provision_vm/tasks/create_new_vm.yml b/roles/provision_vm/tasks/create_new_vm.yml new file mode 100644 index 00000000..704a72c1 --- /dev/null +++ b/roles/provision_vm/tasks/create_new_vm.yml @@ -0,0 +1,59 @@ +--- +- name: Create a new VM + block: + - name: Create or update the VM + community.vmware.vmware_guest: + hostname: "{{ connection_args.vcenter_hostname | mandatory}}" + username: "{{ connection_args.vcenter_username | mandatory }}" + password: "{{ connection_args.vcenter_password | mandatory }}" + vcenter_validate_certs: "{{ omit if connection_args.vcenter_validate_certs is not defined else connection_args.vcenter_validate_certs}}" + + name: "{{ vm_name }}" + uuid: "{{ omit if uuid is not defined else uuid }}" + + # compute resource: + cluster: "{{ omit if cluster not defined else cluster }}" + esxi_hostname: "{{ omit if esxi_hostname is not defined else esxi_hostname}}" + datacenter: "{{ omit if datacenter is not defined else datacenter}}" + folder: "{{ omit if folder is not defined else folder}}" + datastore: "{{ omit if datastore is not defined else datastore}}" + resource_pool: "{{ omit if resource_pool is not defined else lookup('vmware.vmware_rest.resource_pool_moid', {{ datacenter_name }}/host/{{ cluster_name }}/Resources/{{ resource_pool_name }}, **connection_args) }}" + + port: "{{ omit if port is not defined else port }}" + proxy_host: "{{ omit if proxy_host is not defined else proxy_host }}" + proxy_port: "{{ omit if proxy_port is not defined else proxy_port }}" + + # optional + advanced_settings: "{{ omit if advanced_settings is not defined else advanced_settings}}" + annotation: "{{ omit if annotation is not defined else annotation}}" + cdrom: "{{ omit if cdrom is not defined else cdrom }}" + customization: "{{ omit if customization is not defined else customization }}" + customization_spec: "{{ omit if customization_spec is not defined else customization_spec }}" + customvalues: "{{ omit if customvalues is not defined else customvalues }}" + delete_from_inventory: "{{ omit if delete_from_inventory is not defined else delete_from_inventory }}" + disk: "{{ omit if disk is not defined else disk }}" + encryption: "{{ omit if encryption is not defined else encryption }}" + force: "{{ omit if force is not defined else force }}" + guest_id: "{{guest_id | string | mandatory }}" + hardware: "{{ omit if hardware is not defined else hardware }}" + state: "{{ omit if state is not defined else state}}" + state_change_timeout: "{{ omit if state_change_timeout is not defined else state_change_timeout}}" + vapp_properties: "{{ omit if vapp_properties is not defined else vapp_properties}}" + wait_for_customization: "{{ omit if wait_for_customization is not defined else wait_for_customization}}" + wait_for_customization_timeout: "{{ omit if wait_for_customization_timeout is not defined else wait_for_customization_timeout}}" + wait_for_ip_address: "{{ omit if wait_for_ip_address is not defined else wait_for_ip_address}}" + wait_for_ip_address_timeout: "{{ omit if wait_for_ip_address_timeout is not defined else wait_for_ip_address_timeout}}" + networks: "{{ omit if networks is not defined else networks }}" + nvdimm: "{{ omit if nvdimm is not defined else nvdimm }}" + use_instance_uuid : "{{ omit if use_instance_uuid is not defined else use_instance_uuid }}" + name_match: "{{ omit if name_match is not defined else name_match }}" + + is_template: false + register: my_vm + + # power on if power_on = true: + + + - name: Print VM information + ansible.builtin.debug: + var: my_vm \ No newline at end of file diff --git a/roles/provision_vm/tasks/deploy_from_template.yml b/roles/provision_vm/tasks/deploy_from_template.yml new file mode 100644 index 00000000..0e0870dc --- /dev/null +++ b/roles/provision_vm/tasks/deploy_from_template.yml @@ -0,0 +1,65 @@ +--- +- name: Create a new VM + block: + - name: Create or update the VM + community.vmware.vmware_guest: + hostname: "{{ connection_args.vcenter_hostname }}" + username: "{{ connection_args.vcenter_username }}" + password: "{{ connection_args.vcenter_password }}" + validate_certs: "{{ omit if connection_args.vcenter_validate_certs is not defined else connection_args.vcenter_validate_certs}}" + + port: "{{ omit if port is not defined else port }}" + proxy_host: "{{ omit if proxy_host is not defined else proxy_host }}" + proxy_port: "{{ omit if proxy_port is not defined else proxy_port }}" + + name: "{{ vm_name }}" + uuid: "{{ omit if uuid is not defined else uuid }}" + + # compute resource: + cluster: "{{ omit if cluster not defined else cluster }}" + esxi_hostname: "{{ omit if esxi_hostname is not defined else esxi_hostname}}" + datacenter: "{{ omit if datacenter is not defined else datacenter}}" + folder: "{{ omit if folder is not defined else folder}}" + datastore: "{{ omit if datastore is not defined else datastore}}" + resource_pool: "{{ omit if resource_pool is not defined else lookup('vmware.vmware_rest.resource_pool_moid', {{ datacenter_name }}/host/{{ cluster_name }}/Resources/{{ resource_pool_name }}, **connection_args) }}" + + # for deploying from template + template : "{{ template }}" + convert: "{{ omit if convert is not defined else convert }}" + linked_clone: "{{ omit if linked_clone is not defined else linked_clone }}" + snapshot_src: "{{ omit if snapshot_src is not defined else snapshot_src }}" + + # optional: + advanced_settings: "{{ omit if advanced_settings is not defined else advanced_settings}}" + annotation: "{{ omit if annotation is not defined else annotation}}" + cdrom: "{{ omit if cdrom is not defined else cdrom }}" + customization: "{{ omit if customization is not defined else customization }}" + customization_spec: "{{ omit if customization_spec is not defined else customization_spec }}" + customvalues: "{{ omit if customvalues is not defined else customvalues }}" + delete_from_inventory: "{{ omit if delete_from_inventory is not defined else delete_from_inventory }}" + disk: "{{ omit if disk is not defined else disk }}" + encryption: "{{ omit if encryption is not defined else encryption }}" + force: "{{ omit if force is not defined else force }}" + guest_id: "{{ omit if guest_id is not defined else guest_id }}" + hardware: "{{ omit if hardware is not defined else hardware }}" + state: "{{ omit if state is not defined else state}}" + state_change_timeout: "{{ omit if state_change_timeout is not defined else state_change_timeout}}" + vapp_properties: "{{ omit if vapp_properties is not defined else vapp_properties}}" + wait_for_customization: "{{ omit if wait_for_customization is not defined else wait_for_customization}}" + wait_for_customization_timeout: "{{ omit if wait_for_customization_timeout is not defined else wait_for_customization_timeout}}" + wait_for_ip_address: "{{ omit if wait_for_ip_address is not defined else wait_for_ip_address}}" + wait_for_ip_address_timeout: "{{ omit if wait_for_ip_address_timeout is not defined else wait_for_ip_address_timeout}}" + networks: "{{ omit if networks is not defined else networks }}" + nvdimm: "{{ omit if nvdimm is not defined else nvdimm }}" + use_instance_uuid : "{{ omit if use_instance_uuid is not defined else use_instance_uuid }}" + name_match: "{{ omit if name_match is not defined else name_match }}" + + is_template: false + register: my_vm + + # power on if power_on = true: + + + - name: Print VM information + ansible.builtin.debug: + var: my_vm \ No newline at end of file diff --git a/roles/provision_vm/tasks/main.yml b/roles/provision_vm/tasks/main.yml new file mode 100644 index 00000000..a8471e19 --- /dev/null +++ b/roles/provision_vm/tasks/main.yml @@ -0,0 +1,29 @@ +--- +- name: set connection info + ansible.builtin.set_fact: + connection_args: + vcenter_hostname: "{{ vcenter_hostname }}" + vcenter_username: "{{ vcenter_username }}" + vcenter_password: "{{ vcenter_password }}" + vcenter_validate_certs: "{{ vcenter_validate_certs }}" + + block: + - name: Fail if cluster and esxi_hostname both weren't set or both were set + when: ("{{ cluster }}" is defined and "{{ esxi_hostname }}" is defined) or ("{{ cluster }}" is not defined and "{{ esxi_hostname }}" is not defined) + ansible.builtin.fail: + msg: esxi_hostname and cluster are mutually exclusive parameters + + - name: Create VM from template + when: "{{ clone_from_template }}" + ansible.builtin.include_tasks: + file: deploy_from_template.yml + + - name: Create VM from VM + when: "{{ clone_from_vm }}" + ansible.builtin.include_tasks: + file: clone_an_existing_vm.yml + + - name: Create VM from scratch + when: "{{ not clone_from_vm and not clone_from_template }}" + ansible.builtin.include_tasks: + file: create_new_vm.yml \ No newline at end of file