Skip to content

Commit

Permalink
Provisioning VM role
Browse files Browse the repository at this point in the history
  • Loading branch information
bardielle committed Feb 28, 2024
1 parent 85a8c25 commit 7ecdc35
Show file tree
Hide file tree
Showing 17 changed files with 472 additions and 20 deletions.
20 changes: 0 additions & 20 deletions playbooks/provision_vm/provision_vm.yml

This file was deleted.

20 changes: 20 additions & 0 deletions playbooks/provision_vm/provisioning_vm_from_scratch.yml
Original file line number Diff line number Diff line change
@@ -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:

Check failure on line 19 in playbooks/provision_vm/provisioning_vm_from_scratch.yml

View workflow job for this annotation

GitHub Actions / ansible-lint

syntax-check[specific]

unexpected parameter type in action: <class 'ansible.parsing.yaml.objects.AnsibleSequence'>
- role: redhat_cop.vmware_ops.provisioning_vm
20 changes: 20 additions & 0 deletions playbooks/provision_vm/provisioning_vm_from_template.yml
Original file line number Diff line number Diff line change
@@ -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:

Check failure on line 19 in playbooks/provision_vm/provisioning_vm_from_template.yml

View workflow job for this annotation

GitHub Actions / ansible-lint

syntax-check[specific]

unexpected parameter type in action: <class 'ansible.parsing.yaml.objects.AnsibleSequence'>
- role: redhat_cop.vmware_ops.provisioning_vm
20 changes: 20 additions & 0 deletions playbooks/provision_vm/provisioning_vm_from_vm.yml
Original file line number Diff line number Diff line change
@@ -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:

Check failure on line 19 in playbooks/provision_vm/provisioning_vm_from_vm.yml

View workflow job for this annotation

GitHub Actions / ansible-lint

syntax-check[specific]

unexpected parameter type in action: <class 'ansible.parsing.yaml.objects.AnsibleSequence'>
- role: redhat_cop.vmware_ops.provisioning_vm
69 changes: 69 additions & 0 deletions roles/create_vm_template/README.md
Original file line number Diff line number Diff line change
@@ -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
------------------

- ??
3 changes: 3 additions & 0 deletions roles/create_vm_template/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
vcenter_validate_certs: true
guest_os:
Empty file.
Empty file.
14 changes: 14 additions & 0 deletions roles/create_vm_template/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
- name: set connection info

Check failure on line 2 in roles/create_vm_template/tasks/main.yml

View workflow job for this annotation

GitHub Actions / ansible-lint

syntax-check[specific]

'ansible.builtin.set_fact' is not a valid attribute for a Block
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"

Original file line number Diff line number Diff line change
@@ -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
Empty file.
69 changes: 69 additions & 0 deletions roles/provision_vm/README.md
Original file line number Diff line number Diff line change
@@ -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
------------------

- ??
3 changes: 3 additions & 0 deletions roles/provision_vm/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
provision_vm_clone_from_template: false
provision_vm_clone_from_vm: false
82 changes: 82 additions & 0 deletions roles/provision_vm/tasks/clone_an_existing_vm.yml
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit 7ecdc35

Please sign in to comment.