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_facts: custom attributes support #45773

Merged
merged 2 commits into from
Mar 13, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 36 additions & 3 deletions lib/ansible/modules/cloud/vmware/vmware_vm_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

# Copyright: (c) 2015, Joseph Callen <jcallen () csc.com>
# Copyright: (c) 2018, Ansible Project
# Copyright: (c) 2018, Fedor Vompe <f.vompe () comptek.ru>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function
Expand All @@ -25,6 +26,7 @@
author:
- Joseph Callen (@jcpowermac)
- Abhijeet Kasurde (@Akasurde)
- Fedor Vompe (@sumkincpp)
notes:
- Tested on vSphere 5.5 and vSphere 6.5
- From 2.8 and onwards, facts are returned as list of dict instead of dict.
Expand All @@ -41,6 +43,13 @@
default: 'all'
choices: [ all, vm, template ]
version_added: 2.5
type: str
show_attribute:
description:
- Attributes related to VM guest shown in facts only when this is set C(true).
default: no
type: bool
version_added: 2.8
extends_documentation_fragment: vmware.documentation
'''

Expand Down Expand Up @@ -114,7 +123,17 @@
],
"power_state": "poweredOff",
"uuid": "4207072c-edd8-3bd5-64dc-903fd3a0db04",
"vm_network": {}
"vm_network": {
"00:50:56:87:a5:9a": {
"ipv4": [
"10.76.33.228"
],
"ipv6": []
}
},
"attributes": {
"job": "backup-prepare"
}
}
]
'''
Expand All @@ -131,6 +150,11 @@
class VmwareVmFacts(PyVmomi):
def __init__(self, module):
super(VmwareVmFacts, self).__init__(module)
self.custom_field_mgr = self.content.customFieldsManager.field

def get_vm_attributes(self, vm):
return dict((x.name, v.value) for x in self.custom_field_mgr
for v in vm.customValue if x.key == v.key)

# https://github.com/vmware/pyvmomi-community-samples/blob/master/samples/getallvms.py
def get_all_virtual_machines(self):
Expand Down Expand Up @@ -177,6 +201,10 @@ def get_all_virtual_machines(self):
if esxi_parent and isinstance(esxi_parent, vim.ClusterComputeResource):
cluster_name = summary.runtime.host.parent.name

vm_attributes = dict()
if self.module.params.get('show_attribute'):
vm_attributes = self.get_vm_attributes(vm)

virtual_machine = {
"guest_name": summary.config.name,
"guest_fullname": summary.config.guestFullName,
Expand All @@ -187,6 +215,7 @@ def get_all_virtual_machines(self):
"vm_network": net_dict,
"esxi_hostname": esxi_hostname,
"cluster": cluster_name,
"attributes": vm_attributes
}

vm_type = self.module.params.get('vm_type')
Expand All @@ -204,9 +233,13 @@ def main():
argument_spec = vmware_argument_spec()
argument_spec.update(
vm_type=dict(type='str', choices=['vm', 'all', 'template'], default='all'),
show_attribute=dict(type='bool', default='no'),
)

module = AnsibleModule(
argument_spec=argument_spec,
supports_check_mode=True
)
module = AnsibleModule(argument_spec=argument_spec,
supports_check_mode=True)

vmware_vm_facts = VmwareVmFacts(module)
_virtual_machines = vmware_vm_facts.get_all_virtual_machines()
Expand Down
2 changes: 2 additions & 0 deletions test/integration/targets/vmware_vm_facts/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Test code for the vmware_vm_facts module
# Copyright: (c) 2017, Abhijeet Kasurde <akasurde@redhat.com>
# Copyright, (c) 2018, Fedor Vompe <f.vompe@comptek.ru>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

- name: store the vcenter container ip
Expand Down Expand Up @@ -73,6 +74,7 @@
- "item.power_state is defined"
- "item.uuid is defined"
- "item.vm_network is defined"
- "item.attributes is defined"
with_items:
- "{{ vm_facts_0001.virtual_machines | json_query(query) }}"
vars:
Expand Down