From 6f92f28c68cc68109a4166b6316964d510915a28 Mon Sep 17 00:00:00 2001 From: Jiri Tyr Date: Mon, 18 Nov 2019 12:02:46 +0000 Subject: [PATCH] Allow to change VM params --- README.md | 3 +++ defaults/main.yaml | 3 +++ meta/main.yaml | 2 +- tasks/main.yaml | 56 +++++++++++++++++++++++++++++++++++++++++----- 4 files changed, 57 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index a8a97a4..a9a7d18 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/defaults/main.yaml b/defaults/main.yaml index 793e9b9..edeff83 100644 --- a/defaults/main.yaml +++ b/defaults/main.yaml @@ -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 diff --git a/meta/main.yaml b/meta/main.yaml index 5ce1353..53468fe 100644 --- a/meta/main.yaml +++ b/meta/main.yaml @@ -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: diff --git a/tasks/main.yaml b/tasks/main.yaml index 411c85f..8c1cc6b 100644 --- a/tasks/main.yaml +++ b/tasks/main.yaml @@ -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)) }}" @@ -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)) }}" @@ -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)) }}" @@ -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