Skip to content

Commit

Permalink
Allow to change VM params
Browse files Browse the repository at this point in the history
  • Loading branch information
jtyr committed Nov 18, 2019
1 parent 75fe768 commit 6f92f28
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 7 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ Examples
num_cpus: 2
memory_mb: 16384
nested_virt: yes
# This will power off the VM in order to be able to set newly set
# properties (e.g. increased RAM, CPUs, ...)
state: changed

# Third VM
- name: test03
Expand Down
3 changes: 3 additions & 0 deletions defaults/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,6 @@ vmware_vm_provisioning_vms: []

# SCSI controller ID used for the disk detachment
#vmware_vm_provisioning_scsi_controller: 0

# Power off VM before provisioning it when state is set to 'changed'
vmware_vm_provisioning_changed_poweroff: yes
2 changes: 1 addition & 1 deletion meta/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ galaxy_info:
author: Jiri Tyr
description: Role which helps to provision VMs on VMware vCenter.
license: MIT
min_ansible_version: 2.8
min_ansible_version: 2.9
platforms:
- name: EL
versions:
Expand Down
56 changes: 50 additions & 6 deletions tasks/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
- block:
- name: Check if the VM already exists
local_action:
module: vmware_guest_facts
module: vmware_guest_info
datacenter: "{{ vmware_vm_provisioning_vm_tmp.datacenter | default(vmware_vm_provisioning_datacenter | default(omit)) }}"
folder: "{{ vmware_vm_provisioning_vm_tmp.folder | default(vmware_vm_provisioning_folder | default(omit)) }}"
hostname: "{{ vmware_vm_provisioning_vm_tmp.hostname | default(vmware_vm_provisioning_hostname | default(omit)) }}"
Expand Down Expand Up @@ -52,15 +52,28 @@
when: >
'instance' in vmware_vm_provisioning_exists and (
'state' in vmware_vm_provisioning_vm and
vmware_vm_provisioning_vm.state in ['absent', 'rebuilt'] or
vmware_vm_provisioning_state in ['absent', 'rebuilt']
)
vmware_vm_provisioning_vm.state in ['absent', 'changed', (
'rebuilt'
if (
vmware_vm_provisioning_changed_poweroff or (
'changed_poweroff' in vmware_vm_provisioning_vm and
vmware_vm_provisioning_vm.changed_poweroff))
else
'no_state')] or
vmware_vm_provisioning_state in ['absent', 'changed', (
'rebuilt'
if (
vmware_vm_provisioning_changed_poweroff or (
'changed_poweroff' in vmware_vm_provisioning_vm and
vmware_vm_provisioning_vm.changed_poweroff))
else
'no_state')])
failed_when: false

- block:
- name: Get disk facts
local_action:
module: vmware_guest_disk_facts
module: vmware_guest_disk_info
datacenter: "{{ vmware_vm_provisioning_vm.datacenter | default(vmware_vm_provisioning_datacenter | default(omit)) }}"
folder: "{{ vmware_vm_provisioning_vm.folder | default(vmware_vm_provisioning_folder | default(omit)) }}"
hostname: "{{ vmware_vm_provisioning_vm.hostname | default(vmware_vm_provisioning_hostname | default(omit)) }}"
Expand Down Expand Up @@ -172,7 +185,7 @@
port: "{{ vmware_vm_provisioning_vm.port | default(vmware_vm_provisioning_port | default(omit)) }}"
resource_pool: "{{ vmware_vm_provisioning_vm.resource_pool | default(vmware_vm_provisioning_resource_pool | default(omit)) }}"
snapshot_src: "{{ vmware_vm_provisioning_vm.snapshot_src | default(vmware_vm_provisioning_snapshot_src | default(omit)) }}"
state: "{{ omit if 'state' in vmware_vm_provisioning_vm and vmware_vm_provisioning_vm.state == 'rebuilt' or vmware_vm_provisioning_state == 'rebuilt' else vmware_vm_provisioning_vm.state | default(vmware_vm_provisioning_state | default(omit)) }}"
state: present
state_change_timeout: "{{ vmware_vm_provisioning_vm.state_change_timeout | default(vmware_vm_provisioning_state_change_timeout | default(omit)) }}"
template: "{{ vmware_vm_provisioning_vm.template | default(vmware_vm_provisioning_template | default(omit)) }}"
username: "{{ vmware_vm_provisioning_vm.username | default(vmware_vm_provisioning_username | default(omit)) }}"
Expand All @@ -185,5 +198,36 @@
'state' not in vmware_vm_provisioning_vm or
vmware_vm_provisioning_vm.state != 'absent'
) and vmware_vm_provisioning_state != 'absent'
- name: Change VM state
local_action:
module: vmware_guest
cluster: "{{ vmware_vm_provisioning_vm.cluster | default(vmware_vm_provisioning_cluster | default(omit)) }}"
datacenter: "{{ vmware_vm_provisioning_vm.datacenter | default(vmware_vm_provisioning_datacenter | default(omit)) }}"
esxi_hostname: "{{ vmware_vm_provisioning_vm.esxi_hostname | default(vmware_vm_provisioning_esxi_hostname | default(omit)) }}"
folder: "{{ vmware_vm_provisioning_vm.folder | default(vmware_vm_provisioning_folder | default(omit)) }}"
force: "{{ vmware_vm_provisioning_vm.force | default(vmware_vm_provisioning_force | default(omit)) }}"
hostname: "{{ vmware_vm_provisioning_vm.hostname | default(vmware_vm_provisioning_hostname | default(omit)) }}"
name: "{{ vmware_vm_provisioning_vm.name }}"
name_match: "{{ vmware_vm_provisioning_vm.name_match | default(vmware_vm_provisioning_name_match | default(omit)) }}"
password: "{{ vmware_vm_provisioning_vm.password | default(vmware_vm_provisioning_password | default(omit)) }}"
port: "{{ vmware_vm_provisioning_vm.port | default(vmware_vm_provisioning_port | default(omit)) }}"
state_change_timeout: "{{ vmware_vm_provisioning_vm.state_change_timeout | default(vmware_vm_provisioning_state_change_timeout | default(omit)) }}"
state: "{{
'poweredon'
if (
'state' in vmware_vm_provisioning_vm and
vmware_vm_provisioning_vm.state in ['changed', 'rebuilt'] or
vmware_vm_provisioning_state in ['changed', 'rebuilt'])
else
vmware_vm_provisioning_vm.state | default(vmware_vm_provisioning_state | default('poweredon')) }}"
username: "{{ vmware_vm_provisioning_vm.username | default(vmware_vm_provisioning_username | default(omit)) }}"
validate_certs: "{{ vmware_vm_provisioning_vm.validate_certs | default(vmware_vm_provisioning_validate_certs | default(omit)) }}"
wait_for_ip_address: "{{ vmware_vm_provisioning_vm.wait_for_ip_address | default(vmware_vm_provisioning_wait_for_ip_address | default(omit)) }}"
when: >
(
'state' not in vmware_vm_provisioning_vm or
vmware_vm_provisioning_vm.state != 'absent'
) and vmware_vm_provisioning_state != 'absent'
when: >
vmware_vm_provisioning_vm_tmp is defined

0 comments on commit 6f92f28

Please sign in to comment.