parent
3e87429365
commit
904e8434db
3 changed files with 92 additions and 2 deletions
|
@ -265,6 +265,12 @@ options:
|
|||
This is specifically the case for removing a powered on the virtual machine when C(state) is set to C(absent).'
|
||||
default: 'no'
|
||||
type: bool
|
||||
delete_from_inventory:
|
||||
description:
|
||||
- Whether to delete Virtual machine from inventory or delete from disk.
|
||||
default: False
|
||||
type: bool
|
||||
version_added: '2.10'
|
||||
datacenter:
|
||||
description:
|
||||
- Destination datacenter for the deploy operation.
|
||||
|
@ -537,6 +543,17 @@ EXAMPLES = r'''
|
|||
state: absent
|
||||
delegate_to: localhost
|
||||
|
||||
- name: Remove a virtual machine from inventory
|
||||
vmware_guest:
|
||||
hostname: "{{ vcenter_hostname }}"
|
||||
username: "{{ vcenter_username }}"
|
||||
password: "{{ vcenter_password }}"
|
||||
validate_certs: no
|
||||
name: vm_name
|
||||
delete_from_inventory: True
|
||||
state: absent
|
||||
delegate_to: localhost
|
||||
|
||||
- name: Manipulate vApp properties
|
||||
vmware_guest:
|
||||
hostname: "{{ vcenter_hostname }}"
|
||||
|
@ -887,12 +904,22 @@ class PyVmomiHelper(PyVmomi):
|
|||
def gather_facts(self, vm):
|
||||
return gather_vm_facts(self.content, vm)
|
||||
|
||||
def remove_vm(self, vm):
|
||||
def remove_vm(self, vm, delete_from_inventory=False):
|
||||
# https://www.vmware.com/support/developer/converter-sdk/conv60_apireference/vim.ManagedEntity.html#destroy
|
||||
if vm.summary.runtime.powerState.lower() == 'poweredon':
|
||||
self.module.fail_json(msg="Virtual machine %s found in 'powered on' state, "
|
||||
"please use 'force' parameter to remove or poweroff VM "
|
||||
"and try removing VM again." % vm.name)
|
||||
# Delete VM from Inventory
|
||||
if delete_from_inventory:
|
||||
try:
|
||||
vm.UnregisterVM()
|
||||
except (vim.fault.TaskInProgress,
|
||||
vmodl.RuntimeFault) as e:
|
||||
return {'changed': self.change_applied, 'failed': True, 'msg': e.msg, 'op': 'UnregisterVM'}
|
||||
self.change_applied = True
|
||||
return {'changed': self.change_applied, 'failed': False}
|
||||
# Delete VM from Disk
|
||||
task = vm.Destroy()
|
||||
self.wait_for_task(task)
|
||||
if task.info.state == 'error':
|
||||
|
@ -2748,6 +2775,7 @@ def main():
|
|||
vapp_properties=dict(type='list', default=[]),
|
||||
datastore=dict(type='str'),
|
||||
convert=dict(type='str', choices=['thin', 'thick', 'eagerzeroedthick']),
|
||||
delete_from_inventory=dict(type='bool', default=False),
|
||||
)
|
||||
|
||||
module = AnsibleModule(argument_spec=argument_spec,
|
||||
|
@ -2782,7 +2810,7 @@ def main():
|
|||
if module.params['force']:
|
||||
# has to be poweredoff first
|
||||
set_vm_power_state(pyv.content, vm, 'poweredoff', module.params['force'])
|
||||
result = pyv.remove_vm(vm)
|
||||
result = pyv.remove_vm(vm, module.params['delete_from_inventory'])
|
||||
elif module.params['state'] == 'present':
|
||||
if module.check_mode:
|
||||
result.update(
|
||||
|
|
|
@ -30,3 +30,4 @@ vmware_guest_test_playbooks:
|
|||
# - template_d1_c1_f0.yml
|
||||
- vapp_d1_c1_f0.yml
|
||||
- reconfig_vm_to_latest_version.yml
|
||||
- remove_vm_from_inventory.yml
|
||||
|
|
|
@ -0,0 +1,61 @@
|
|||
# Test code for the vmware_guest module.
|
||||
# Copyright: (c) 2019, Pavan Bidkar <pbidkar@vmware.com>
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
- name: Create VM to unregister
|
||||
vmware_guest:
|
||||
validate_certs: False
|
||||
hostname: "{{ vcenter_hostname }}"
|
||||
username: "{{ vcenter_username }}"
|
||||
password: "{{ vcenter_password }}"
|
||||
name: test_vm1
|
||||
guest_id: centos64Guest
|
||||
datacenter: "{{ dc1 }}"
|
||||
folder: F0
|
||||
hardware:
|
||||
num_cpus: 4
|
||||
num_cpu_cores_per_socket: 2
|
||||
memory_mb: 512
|
||||
disk:
|
||||
- size: 1gb
|
||||
type: thin
|
||||
autoselect_datastore: True
|
||||
state: present
|
||||
register: create_vm_for_test
|
||||
|
||||
- name: assert that changes were made
|
||||
assert:
|
||||
that:
|
||||
- create_vm_for_test is changed
|
||||
|
||||
- name: Remove VM from Inventory
|
||||
vmware_guest:
|
||||
validate_certs: False
|
||||
hostname: "{{ vcenter_hostname }}"
|
||||
username: "{{ vcenter_username }}"
|
||||
password: "{{ vcenter_password }}"
|
||||
name: test_vm1
|
||||
delete_from_inventory: True
|
||||
state: absent
|
||||
register: remove_vm_from_inventory
|
||||
|
||||
- name: assert that changes were made
|
||||
assert:
|
||||
that:
|
||||
- remove_vm_from_inventory is changed
|
||||
|
||||
- name: Remove VM again from Inventory
|
||||
vmware_guest:
|
||||
validate_certs: False
|
||||
hostname: "{{ vcenter_hostname }}"
|
||||
username: "{{ vcenter_username }}"
|
||||
password: "{{ vcenter_password }}"
|
||||
name: test_vm1
|
||||
delete_from_inventory: True
|
||||
state: absent
|
||||
register: remove_again_vm_from_inventory
|
||||
|
||||
- name: assert that changes were made
|
||||
assert:
|
||||
that:
|
||||
- not (remove_again_vm_from_inventory is changed)
|
Loading…
Reference in a new issue