Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vmware_vm_info: Add allocated Storage, CPU and Memory to output #1283

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
mariolenz marked this conversation as resolved.
Show resolved Hide resolved
- 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