1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

vmware_vm_facts: fix the support with regular ESXi

Ensure the module still work with the ESXi where CustomFieldsManager
does not exist.

From: https://www.vmware.com/support/developer/converter-sdk/conv60_apireference/vim.CustomFieldsManager.html

    The CustomFieldsManager object is used to add and remove custom fields to
    managed entities.
    The custom fields values set on managed entities are available through the
    customValue property and through the summary objects for VirtualMachine
    and HostSystem. They are not available directly through this managed object.
    This functionality is only available through VirtualCenter.

Fixes: #56071
This commit is contained in:
Gonéri Le Bouder 2019-05-03 18:08:42 -04:00
parent 21d4e239b4
commit cf78759f5b
7 changed files with 33 additions and 18 deletions

View file

@ -807,6 +807,9 @@ class PyVmomi(object):
self.si = None self.si = None
self.current_vm_obj = None self.current_vm_obj = None
self.content = connect_to_api(self.module) self.content = connect_to_api(self.module)
self.custom_field_mgr = []
if self.content.customFieldsManager: # not an ESXi
self.custom_field_mgr = self.content.customFieldsManager.field
def is_vcenter(self): def is_vcenter(self):
""" """

View file

@ -91,7 +91,6 @@ except ImportError:
class VmAttributeDefManager(PyVmomi): class VmAttributeDefManager(PyVmomi):
def __init__(self, module): def __init__(self, module):
super(VmAttributeDefManager, self).__init__(module) super(VmAttributeDefManager, self).__init__(module)
self.custom_field_mgr = self.content.customFieldsManager.field
def remove_custom_def(self, field): def remove_custom_def(self, field):
changed = False changed = False

View file

@ -139,7 +139,6 @@ from ansible.module_utils.vmware import PyVmomi, vmware_argument_spec
class VmAttributeManager(PyVmomi): class VmAttributeManager(PyVmomi):
def __init__(self, module): def __init__(self, module):
super(VmAttributeManager, self).__init__(module) super(VmAttributeManager, self).__init__(module)
self.custom_field_mgr = self.content.customFieldsManager.field
def set_custom_field(self, vm, user_fields): def set_custom_field(self, vm, user_fields):
result_fields = dict() result_fields = dict()

View file

@ -18,9 +18,9 @@ ANSIBLE_METADATA = {
DOCUMENTATION = r''' DOCUMENTATION = r'''
--- ---
module: vmware_vm_facts module: vmware_vm_facts
short_description: Return basic facts pertaining to a vSphere virtual machine guest short_description: Return basic facts pertaining to a VMware machine guest
description: description:
- Return basic facts pertaining to a vSphere virtual machine guest. - Return basic facts pertaining to a vSphere or ESXi virtual machine guest.
- Cluster name as fact is added in version 2.7. - Cluster name as fact is added in version 2.7.
version_added: '2.0' version_added: '2.0'
author: author:
@ -28,7 +28,7 @@ author:
- Abhijeet Kasurde (@Akasurde) - Abhijeet Kasurde (@Akasurde)
- Fedor Vompe (@sumkincpp) - Fedor Vompe (@sumkincpp)
notes: notes:
- Tested on vSphere 5.5 and vSphere 6.5 - Tested on ESXi 6.7, vSphere 5.5 and vSphere 6.5
- From 2.8 and onwards, facts are returned as list of dict instead of dict. - From 2.8 and onwards, facts are returned as list of dict instead of dict.
requirements: requirements:
- python >= 2.6 - python >= 2.6
@ -165,7 +165,6 @@ from ansible.module_utils.vmware import PyVmomi, get_all_objs, vmware_argument_s
class VmwareVmFacts(PyVmomi): class VmwareVmFacts(PyVmomi):
def __init__(self, module): def __init__(self, module):
super(VmwareVmFacts, self).__init__(module) super(VmwareVmFacts, self).__init__(module)
self.custom_field_mgr = self.content.customFieldsManager.field
def get_vm_attributes(self, vm): def get_vm_attributes(self, vm):
return dict((x.name, v.value) for x in self.custom_field_mgr return dict((x.name, v.value) for x in self.custom_field_mgr

View file

@ -447,6 +447,8 @@ class InventoryModule(BaseInventoryPlugin, Cacheable):
'runtime.maxMemoryUsage', 'runtime.maxMemoryUsage',
'customValue', 'customValue',
] ]
field_mgr = []
if self.content.customFieldsManager:
field_mgr = self.pyv.content.customFieldsManager.field field_mgr = self.pyv.content.customFieldsManager.field
for vm_prop in vm_properties: for vm_prop in vm_properties:

View file

@ -3,6 +3,15 @@
# Copyright, (c) 2018, Fedor Vompe <f.vompe@comptek.ru> # 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) # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
- when: vcsim is not defined
block:
- name: Get facts from a given ESXi
vmware_vm_facts:
validate_certs: false
hostname: '{{ esxi1 }}'
username: '{{ hostvars[esxi1].ansible_user }}'
password: '{{ hostvars[esxi1].ansible_password }}'
- import_role: - import_role:
name: prepare_vmware_tests name: prepare_vmware_tests
vars: vars:
@ -20,20 +29,22 @@
register: vm_facts_0001 register: vm_facts_0001
- debug: var=vm_facts_0001 - debug: var=vm_facts_0001
- set_fact:
first_vm: "{{ vm_facts_0001['virtual_machines'][0] }}"
- &vm_fact_check - &vm_fact_check
name: Verify if VM facts exist name: Verify if VM facts exist
assert: assert:
that: that:
- first_vm.esxi_hostname is defined - "item.esxi_hostname is defined"
- first_vm.guest_fullname is defined - "item.guest_fullname is defined"
- first_vm.ip_address is defined - "item.ip_address is defined"
- first_vm.mac_address is defined - "item.mac_address is defined"
- first_vm.power_state is defined - "item.power_state is defined"
- first_vm.uuid is defined - "item.uuid is defined"
- first_vm.vm_network is defined - "item.vm_network is defined"
with_items:
- "{{ vm_facts_0001.virtual_machines | json_query(query) }}"
vars:
query: "[?guest_name=='DC0_H0_VM0']"
- <<: *vm_data - <<: *vm_data
name: Get facts about available vms in check mode name: Get facts about available vms in check mode
@ -48,7 +59,7 @@
hostname: "{{ vcenter_hostname }}" hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}" username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}" password: "{{ vcenter_password }}"
name: "{{ first_vm.guest_name }}" name: "DC0_H0_VM0"
register: folder_path_info register: folder_path_info
- set_fact: - set_fact:

View file

@ -89,7 +89,9 @@ class FakeAnsibleModule:
def fake_connect_to_api(module): def fake_connect_to_api(module):
pass class MyContent():
customFieldsManager = None
return MyContent
def test_pyvmomi_lib_exists(mocker, fake_ansible_module): def test_pyvmomi_lib_exists(mocker, fake_ansible_module):