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 4 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)
27 changes: 21 additions & 6 deletions plugins/modules/vmware_vm_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@

RETURN = r'''
virtual_machines:
description: list of dictionary of virtual machines and their information
description: list of dictionary of virtual machines and their information. (allocated storage in byte and memory in MB)
returned: success
type: list
sample: [
Expand Down Expand Up @@ -227,7 +227,12 @@
"name": "tag_0001"
}
],
"moid": "vm-24"
"moid": "vm-24",
"allocated": {
"storage": 500000000,
"cpu": 2,
"memory": 16
},
}
]
'''
Expand All @@ -238,10 +243,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 +329,17 @@ def get_virtual_machines(self):
if self.module.params.get('show_tag'):
vm_tags = self.get_tag_info(vm)

storage_allocated = 0
for device in vm.config.hardware.device:
if isinstance(device, vim.vm.device.VirtualDisk):
storage_allocated += device.capacityInBytes

allocated = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a flag to return these facts optionally (for example - show_allocated ) rather than always returning facts.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I will do this.

"storage": storage_allocated,
"cpu": vm.config.hardware.numCPU,
"memory": vm.config.hardware.memoryMB
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ERROR: plugins/modules/vmware_vm_info.py:340:17: E123: closing bracket does not match indentation of opening bracket's line


vm_folder = PyVmomi.get_vm_path(content=self.content, vm_name=vm)
datacenter = get_parent_datacenter(vm)
datastore_url = list()
Expand All @@ -349,6 +363,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 Down