Skip to content

Commit

Permalink
vmware_vm_info: Add allocated Storage, CPU and Memory to output (#1283)
Browse files Browse the repository at this point in the history
vmware_vm_info: Add allocated Storage, CPU and Memory to output

SUMMARY
Add allocated Storage, CPU and Memory to output
ISSUE TYPE
Feature Pull Request
COMPONENT NAME
vmware_vm_info.py
ADDITIONAL INFORMATION

Reviewed-by: Mario Lenz <[email protected]>
Reviewed-by: Abhijeet Kasurde <None>
Reviewed-by: None <None>
  • Loading branch information
Nina2244 authored May 11, 2022
1 parent c7317f8 commit 3c6358d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
minor_changes:
- vmware_vm_info - Add the feature to get the output of allocated storage, cpu und memory.
(https://github.com/ansible-collections/community.vmware/pull/1283)
32 changes: 27 additions & 5 deletions plugins/modules/vmware_vm_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@
- Tags related to virtual machine are shown if set to C(True).
default: False
type: bool
show_allocated:
version_added: '2.5.0'
description:
- Allocated storage in byte and memory in MB are shown if it set to True.
default: False
type: bool
vm_name:
description:
- Name of the virtual machine to get related configurations information from.
Expand Down Expand Up @@ -227,7 +233,12 @@
"name": "tag_0001"
}
],
"moid": "vm-24"
"moid": "vm-24",
"allocated": {
"storage": 500000000,
"cpu": 2,
"memory": 16
},
}
]
'''
Expand All @@ -238,10 +249,8 @@
pass

from ansible.module_utils.basic import AnsibleModule
from ansible_collections.community.vmware.plugins.module_utils.vmware import (
PyVmomi, get_all_objs,
vmware_argument_spec, _get_vm_prop,
get_parent_datacenter, find_vm_by_name)
from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi, \
get_all_objs, vmware_argument_spec, _get_vm_prop, get_parent_datacenter, find_vm_by_name
from ansible_collections.community.vmware.plugins.module_utils.vmware_rest_client import VmwareRestClient


Expand Down Expand Up @@ -326,6 +335,17 @@ def get_virtual_machines(self):
if self.module.params.get('show_tag'):
vm_tags = self.get_tag_info(vm)

allocated = {}
if self.module.params.get('show_allocated'):
storage_allocated = 0
for device in vm.config.hardware.device:
if isinstance(device, vim.vm.device.VirtualDisk):
storage_allocated += device.capacityInBytes
allocated = {
"storage": storage_allocated,
"cpu": vm.config.hardware.numCPU,
"memory": vm.config.hardware.memoryMB}

vm_folder = PyVmomi.get_vm_path(content=self.content, vm_name=vm)
datacenter = get_parent_datacenter(vm)
datastore_url = list()
Expand All @@ -349,6 +369,7 @@ def get_virtual_machines(self):
"folder": vm_folder,
"moid": vm._moId,
"datastore_url": datastore_url,
"allocated": allocated
}

vm_type = self.module.params.get('vm_type')
Expand All @@ -368,6 +389,7 @@ def main():
vm_type=dict(type='str', choices=['vm', 'all', 'template'], default='all'),
show_attribute=dict(type='bool', default='no'),
show_tag=dict(type='bool', default=False),
show_allocated=dict(type='bool', default=False),
folder=dict(type='str'),
vm_name=dict(type='str')
)
Expand Down

0 comments on commit 3c6358d

Please sign in to comment.